【问题标题】:mpld3 tooltips fails when changing XTick locations更改 XTick 位置时,mpld3 工具提示失败
【发布时间】:2015-01-27 06:27:05
【问题描述】:

我在使用 python 3.3 的 mac osx 上的 ipython notebook 中运行以下命令时遇到了一些麻烦:

import mpld3
from mpld3 import plugins
mpld3.enable_notebook()

fig, ax = plt.subplots()
points = plt.scatter([0,3,5, 9], [2,4,6, 8])
labels= ['a', 'b', 'c']
#ax.xaxis.set_ticks([2,4])
tooltip = plugins.PointHTMLTooltip(points, labels,
                                   voffset=10, hoffset=10)
plugins.connect(fig, tooltip)
mpld3.display()

效果很好。但是,如果我取消注释:

ax.xaxis.set_ticks([2,4])

它因错误而崩溃:TypeError: 2 is not JSON serializable

这是一个错误吗?有解决办法吗?

谢谢!

【问题讨论】:

  • 显然,函数想要一个json对象,你给它一个列表,尝试传递json.dumps([2,4])作为参数。
  • 将其更改为: ax.xaxis.set_ticks(json.dumps([2,4]) ) 崩溃并显示错误消息: TypeError: unorderable types: numpy.ndarray()

标签: python matplotlib tooltip mpld3


【解决方案1】:

这是a bug,感谢您识别它。解决方法是使用 plt.xticks 并明确地为刻度提供标签:

plt.xticks([2,4], [2,4])

【讨论】:

  • 感谢您指出这一点!它肯定会在我正在尝试的事情上帮助我。我很想在某个时候尝试改进 mpld3 的这方面。
猜你喜欢
  • 2015-01-03
  • 2017-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-09
  • 2013-09-30
相关资源
最近更新 更多