matplotlib的交互式数据游标

mpldatacursor的Python项目详细描述


mpldatacursor提供交互式的“数据游标”(可单击的注释 框)用于matplotlib。

v0.6的主要变化

版本0.6增加了:

  • 更好地处理日期格式的轴。
  • 右击可交互隐藏“弹出”文本框 (通过hide_buttondisplay_buttonkwargs可控制)。
  • 双轴的适当支撑。
  • 格式化程序函数更好的Unicode支持。注意这使得 mpldatacursor与早期3.x版本(3.0、3.1和3.2)不兼容。 但是,它仍然兼容python>;=3.3(例如3.3、3.4和3.5) 以及2.6和2.7。
  • 默认情况下,注释框将在图形中保持可见。 指定keep_inside=False以禁用此功能。
  • 增加了提取三维艺术家z值的基本支持。
  • 使默认X&Y格式的精度取决于 轴线。
  • 通过nbagg后端完全支持交互式ipython笔记本。 注意,nbagg的性能可能非常差。
  • 完全支持matplotlib 1.5.x、python 3.5和qt5(在v0.6.2中)。
  • 无数的错误修复(感谢所有人的报告!).

基本用法

mpldatacursor通过 datacursor函数。

例如,这将在 批注框:

import matplotlib.pyplot as plt
import numpy as np
from mpldatacursor import datacursor

data = np.outer(range(10), range(1, 5))

fig, ax = plt.subplots()
lines = ax.plot(data)
ax.set_title('Click somewhere on a line')

datacursor(lines)

plt.show()
http://joferkington.github.com/mpldatacursor/images/basic.png

如果未指定艺术家或艺术家序列,则所有手动绘制的艺术家 在所有轴中,所有图形都将被激活。(仅限于 通过将axes对象或一系列轴传递给axes 夸尔格。)

例如(输出与第一个示例相同):

import matplotlib.pyplot as plt
import numpy as np
from mpldatacursor import datacursor

data = np.outer(range(10), range(1, 5))

plt.plot(data)
plt.title('Click somewhere on a line')

datacursor()

plt.show()

隐藏批注框和切换交互性

若要隐藏特定的批注框,请右键单击它(可通过 hide_buttonkwarg)。要隐藏所有批注框,请按 键盘。(想想“删除”。“h”是matplotlib的默认键 “home”。)要禁用或重新启用交互式数据游标,请按“t”(对于 “切换”)。这些密钥可以通过keybindingskwarg自定义。

控制显示的文本

显示的文本可以通过使用formatterkwarg, 它需要一个函数来接受任意的Kwargs序列和 返回要显示的字符串。通常,通过 format模板字符串的方法(例如 formatter="longitude:{x:.2f}\nlatitude{y:.2f}".format)。

例如,使用formatterkwarg只显示 艺术家而不是x,y坐标:

import numpy as np
import matplotlib.pyplot as plt
from mpldatacursor import datacursor

x = np.linspace(0, 10, 100)

fig, ax = plt.subplots()
ax.set_title('Click on a line to display its label')

# Plot a series of lines with increasing slopes...
for i in range(1, 20):
    ax.plot(x, i * x, label='$y = {}x$'.format(i))

# Use a DataCursor to interactively display the label for a selected line...
datacursor(formatter='{label}'.format)

plt.show()
http://joferkington.github.com/mpldatacursor/images/show_artist_labels.png

使用图像

datacursor还将在 形象。本例还演示了如何使用display="single"选项 每个轴仅显示一个数据光标,而不是一个。

import matplotlib.pyplot as plt
import numpy as np
from mpldatacursor import datacursor

data = np.arange(100).reshape((10,10))

fig, axes = plt.subplots(ncols=2)
axes[0].imshow(data, interpolation='nearest', origin='lower')
axes[1].imshow(data, interpolation='nearest', origin='upper',
                     extent=[200, 300, 400, 500])
datacursor(display='single')

fig.suptitle('Click anywhere on the image')

plt.show()
http://joferkington.github.com/mpldatacursor/images/image_example.png

可拖动的框

如果指定了draggable=True,则注释框可以交互 创建后拖动到新位置。

例如(这还演示了如何使用display='multiple'kwarg):

import matplotlib.pyplot as plt
import numpy as np
from mpldatacursor import datacursor

data = np.outer(range(10), range(1, 5))

fig, ax = plt.subplots()
ax.set_title('Try dragging the annotation boxes')
ax.plot(data)

datacursor(display='multiple', draggable=True)

plt.show()
http://joferkington.github.com/mpldatacursor/images/draggable_example.png

进一步定制

datacursor的其他关键字参数将传递给annotate。 这允许用户控制“弹出框”的外观和位置, 箭头等。请注意,为bbox和^{tt22}传入的属性$ Kwargs将与默认样式合并。因此,具体化 likebbox=dict(alpha=1)将生成一个不透明的、黄色的圆形框,而不是 MatplotLib的默认蓝色方形框。作为一个基本示例:

import matplotlib.pyplot as plt
import numpy as np
from mpldatacursor import datacursor

fig, axes = plt.subplots(ncols=2)

left_artist = axes[0].plot(range(11))
axes[0].set(title='No box, different position', aspect=1.0)

right_artist = axes[1].imshow(np.arange(100).reshape(10,10))
axes[1].set(title='Fancy white background')

# Make the text pop up "underneath" the line and remove the box...
dc1 = datacursor(left_artist, xytext=(15, -15), bbox=None)

# Make the box have a white background with a fancier connecting arrow
dc2 = datacursor(right_artist, bbox=dict(fc='white'),
                 arrowprops=dict(arrowstyle='simple', fc='white', alpha=0.5))

plt.show()
http://joferkington.github.com/mpldatacursor/images/change_popup_color.png

突出显示选定行

HighlightingDataCursor除了突出显示一个Line2D艺术家 显示选定的坐标。:

import numpy as np
import matplotlib.pyplot as plt
from mpldatacursor import HighlightingDataCursor

x = np.linspace(0, 10, 100)

fig, ax = plt.subplots()

# Plot a series of lines with increasing slopes...
lines = []
for i in range(1, 20):
    line, = ax.plot(x, i * x, label='$y = {}x$'.format(i))
    lines.append(line)

HighlightingDataCursor(lines)

plt.show()
http://joferkington.github.com/mpldatacursor/images/highlighting_example.png

安装

mpldatacursor可以安装来自pypi的ed使用 easy_install/pip/等(例如pip install mpldatacursor)或者 下载源代码并直接用python setup.py install安装。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
swing Java按钮/网格布局   java列出Google日历中的所有事件   java无效:单击API publisher test按钮后连接到后端时出错   带有内部赋值的java While循环导致checkstyle错误   java为什么trimToSize/ensureCapacity方法提供“公共”级访问?   文件输出流的java问题   ListIterator和并发修改异常的java问题   java如何使用两个URL映射   无法识别使用“./../”构造的字符串java相对路径,为什么?   首次写入remotelyclosedsocket不会触发异常,对吗?JAVA   java OneDrive REST API为文件上载提供了400个无效谓词   Java泛型、集合接口和对象类的问题   OpenSSL Java安全提供程序   jmeter java运行jmx禁用操作