mpld3图,注释问题 [英] mpld3 plot, annotations issues
问题描述
我正在使用 mpld3 在 Intranet 网站上显示图表.我使用的选项是将图形保存到dict,然后使用mpld3.js在客户端进行渲染.
除了我要使用注释时,图形呈现良好.那些明显抵消了.而且我不明白为什么,因为即使我将偏移量设置为 (0, 0),注释仍然很远.
为了说明这一点,我复制并粘贴了这篇文章中给出的示例:
这是用mpld3制作的:
请注意,使用上述链接中的代码同时生成两个图像.matplotlib一种是使用:
plt.show()
然后手动保存.
mpld3中的一个是使用:
graph_data = json.dumps(fig_to_dict(fig))
然后 graph_data
是在客户端使用 mpld3.js
生成的,除了使用批注外,该方法工作得很好.
您知道为什么会这样吗?最终我可以只显示直接从 matplotlib 生成的图像,但具有交互式图表是一个不错的奖励.
看来 plt.annotate
是 mpl
功能,尚不支持mpld3
.我已将其添加到缺少的功能列表.拉请求欢迎!
对于变通办法,您可以使用 plt.text
来获取单词,并使用 plt.plot
来进行其他修饰:
将 numpy 导入为 np导入matplotlib.pyplot作为pltnp.random.seed(12345) # 设置可重复性的种子N = 10数据 = np.random.random((N, 4))标签 = ['point{0}'.format(i) for i in range(N)]plt.subplots_adjust(底部= 0.1)plt.scatter(数据[:, 0], 数据[:, 1], 标记 = 'o', c = 数据[:, 2], s = 数据[:, 3]*1500,cmap = plt.get_cmap('光谱'))对于 zip(labels, data[:, 0], data[:, 1]) 中的标签, x, y:plt.text(x-.05,y + .05,标签,ha = '右', va = '底部')plt.plot([x-.05,x], [y+.05,y], 'k-')
这是一个笔记本,显示了它的实际效果.您可能还会对 HTML工具提示插件感兴趣.
I'm using mpld3 to display graphs on an intranet website. I'm using the option of saving the graph to a dict and render it on the client side using mpld3.js.
The graph renders fine except when I want to use annotations. Those ones are clearly offset. And I don't understand why, because even if I set the offset to (0, 0), the annotations are still way off.
To illustrate this, I have copied and pasted the example given in this post: Matplotlib: How to put individual tags for a scatter plot
Here is the image as correctly generated by matplotlib:
Here is the one made with mpld3:
Note that both image are generated at the same time using the code in the link mentioned above. The matplotlib one is made using:
plt.show()
and then manually saved.
The one from mpld3 is made using:
graph_data = json.dumps(fig_to_dict(fig))
Then graph_data
is generated on the client side using mpld3.js
, which works perfectly fine except when using annotations.
Do you have any idea why it behaves like this? Eventually I could just display the images generated directly from matplotlib, but the have interactive chart is a nice bonus.
It appears that plt.annotate
is a mpl
feature that is not yet supported in mpld3
. I've added it to the list of missing features. Pull requests welcome!
For a hacky work-around, you can use plt.text
to get the words and plt.plot
to make any additional embellishments:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(12345) # set seed for reproducibility
N = 10
data = np.random.random((N, 4))
labels = ['point{0}'.format(i) for i in range(N)]
plt.subplots_adjust(bottom = 0.1)
plt.scatter(
data[:, 0], data[:, 1], marker = 'o', c = data[:, 2], s = data[:, 3]*1500,
cmap = plt.get_cmap('Spectral'))
for label, x, y in zip(labels, data[:, 0], data[:, 1]):
plt.text(x-.05, y+.05,
label,
ha = 'right', va = 'bottom')
plt.plot([x-.05,x], [y+.05,y], 'k-')
Here is a notebook that shows it in action. You might also be interested in the HTML Tooltips plugin.
这篇关于mpld3图,注释问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!