2

所以我正在使用 matplotlib.animation 做一些动画。我绘制的所有图形都是圆圈,但是我的一个圆圈变得太小了,因为我不断地让事情变得更复杂。我环顾四周试图找出 pyplot 是否有像 pyplot.Circle 这样的十字线命令,但我没有成功。那里的任何人都知道pyplot内置的类似功能,还是我必须构建自己的功能来做到这一点?

4

1 回答 1

5

我不太清楚你在问什么。

由于我目前正在阅读您的问题,因此我无法确定您要的是哪些选项。

  1. 随鼠标移动的交互式“十字线”。
  2. 横跨轴延伸的静态“十字准线”。
  3. 要放置的“+”样式标记而不是圆形。

对于第一个选项,请查看matplotlib.widgets.Cursor. 这里有一个例子:http: //matplotlib.org/examples/widgets/cursor.html

from matplotlib.widgets import Cursor
import numpy as np
import matplotlib.pyplot as plt


fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, axisbg='#FFFFCC')

x, y = 4*(np.random.rand(2, 100)-.5)
ax.plot(x, y, 'o')
ax.set_xlim(-2, 2)
ax.set_ylim(-2, 2)

# set useblit = True on gtkagg for enhanced performance
cursor = Cursor(ax, useblit=True, color='red', linewidth=2 )

plt.show()

对于第二个,使用axhlineand axvline。例如:

import matplotlib.pyplot as plt

def cross_hair(x, y, ax=None, **kwargs):
    if ax is None:
        ax = plt.gca()
    horiz = ax.axhline(y, **kwargs)
    vert = ax.axvline(x, **kwargs)
    return horiz, vert

cross_hair(0.2, 0.3, color='red')
plt.show()

在此处输入图像描述


最后,如果你想用+标记代替圆圈,只需使用 useax.plotax.scatter

例如

fig, ax = plt.subplots()
marker, = ax.plot([0.2], [0.3], linestyle='none', marker='+')

或者:

fig, ax = plt.subplots()
marker = ax.scatter([0.2], [0.3], marker='+')

在此处输入图像描述

您可以手动构建标记(它最容易使用Line2D,但您也可以使用它matplotlib.markers.MarkerStyle('+').get_path()来获取原始路径,然后设置位置和大小以适应),但这通常比它的价值要麻烦得多。

于 2013-11-12T18:30:24.697 回答