我正在尝试使用一些数据创建一个简单的条形图(这里是硬编码的,但我会在某个时候从文件中读取它)。到目前为止,我能够获得条形图,但我希望属性“功能名称”位于每个条形下方。现在,我只得到数字 1 到 16。我可以做些什么来使每个条形下的每个特征?

我的代码:

import matplotlib.pyplot as plt
import numpy as np
import mpld3

fig, ax = plt.subplots()

N = 17
feature_name = ('AccountLength','Intlact','Vmailact','Vmailnumber','day minutes','day calls','day charge','evening minutes','evening calls','evening charge','night minutes','night calls','night charge','intl minutes','intl calls','intl charge','cust calls')
importance = (0.0304,0.0835,0.0222,0.0301,0.1434,0.0315,0.1354,0.0677,0.0268,0.0669,0.0386,0.0286,0.0371,0.0417,0.0521,0.0434,0.1197)
ind = np.arange(N)
width = 0.20
rects = ax.bar(ind, importance, width, color = 'r')
ax.grid(color='white', linestyle='solid')

ax.set_title("Why are my customers churning?", size=20)
ax.set_ylabel('Importance in percentage')
ax.set_xlabel('Feature Name')
ax.set_xticklabels( (feature_name) )
labels = (feature_name)
tooltip = mpld3.plugins.PointLabelTooltip(rects, labels=labels)
mpld3.plugins.connect(fig, tooltip)

mpld3.show()

编辑:事实证明,如果我使用 plt.show(),我可以看到刻度标签,但是当我尝试在 mpld3 中执行相同操作时,它不起作用。还想知道为什么工具提示没有出现。

最佳答案

mpld3 不支持设置刻度标签和位置。见 issue 22

工具提示不会出现,因为 PointLabelTooltip() 函数接受 matplotlib Collection 或 Line2D 对象作为输入,而 plt.bar() 返回 BarContainer 对象。

UPD:刻度标签已使用 0.2 和 0.3git 版本进行测试。

关于python - 在 mpld3 中设置刻度标签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35446525/

10-16 22:51