python动画中的十字准线

jib*_*abo 2 matplotlib python-2.7

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

Joe*_*ton 5

I can't quite tell what you're asking.

As I'm currently reading your question, I can't tell which of these options you're asking for.

  1. Interactive "cross hairs" that move with the mouse.
  2. A static "cross hair" that extends across the axis.
  3. A "+" style marker to be placed instead of a circle.

For the first option, have a look at matplotlib.widgets.Cursor. There's an example here: 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()
Run Code Online (Sandbox Code Playgroud)

For the second, use axhline and axvline. E.g.:

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()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


Finally, if you want a + marker in place of a circle, just use either use ax.plot or ax.scatter.

E.g.

fig, ax = plt.subplots()
marker, = ax.plot([0.2], [0.3], linestyle='none', marker='+')
Run Code Online (Sandbox Code Playgroud)

Or:

fig, ax = plt.subplots()
marker = ax.scatter([0.2], [0.3], marker='+')
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

You can manually build the marker (it's easiest to use Line2D, but you can also use matplotlib.markers.MarkerStyle('+').get_path() to get a raw path and then set the position and size to suit), but it's usually far more trouble than it's worth.