在mpld3

时间:2016-02-17 01:54:08

标签: python matplotlib mpld3

我试图用一些数据创建一个简单的条形图(这里是硬编码的,但我会在某个时候从文件中读取它)。到目前为止,我能够获得条形图,但我想要属性"功能名称"来到每个酒吧。现在,我只得到数字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(),我可以看到ticklabels,但是当我尝试在mpld3中做同样的事情时,它并不起作用。还想知道为什么工具提示不会出现。

2 个答案:

答案 0 :(得分:0)

mpld3不支持设置刻度标签和位置。请参阅issue 22

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

UPD:使用版本0.2& D测试刻度标签。 0.3git。

答案 1 :(得分:0)

我认为答案为时已晚,但也许这可能有助于其他人。

将文本设置为刻度标签似乎在 mpld3 包中确实存在一些问题,但是当将某些数据绘制到某个范围值时,我能够执行此操作,然后将刻度设置为此范围并最终设置勾选需要的标签。

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

fig, ax = plt.subplots()

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 = range(1, len(feature_name)+1)
width = 0.20

rects = ax.bar(ind, importance, width, color = 'r')

ax.grid(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_xticks(ind)
ax.set_xticklabels(feature_name)

mpld3.show()

结果:

mpld3 ticklabels example

还有一个额外的工作人员与刻度轮换(在 mpld3 中也不支持)。也许可以编写自定义插件来实现这一目标。如果有人需要这个,我会更新答案。