在mpld3中设置刻度标签

2024-04-29 14:16:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用一些数据创建一个简单的条形图(这里是硬编码的,但我会在某个时候从文件中读入它)。到目前为止,我可以得到条形图,但我希望属性“Feature Name”出现在每个条下面。现在,我只知道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()

编辑:如果我用表演(),我可以看到ticklabels,但当我尝试在mpld3中执行相同的操作时,它不起作用。还想知道为什么工具提示没有出现。在


Tags: nameimportlabelsaxfeature条形图setintl
2条回答

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

由于PointLabelTooltip()函数接受matplotlib集合或Line2D对象作为输入,而plt.bar()返回BarContainer对象,所以不会出现工具提示。在

UPD:Tick标签是用0.2&0.3git版本测试的。在

我想答案已经太迟了,不过也许这可以帮助其他人。在

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

还有一个额外的人员要处理ticks循环(在mpld3中也不支持)。也许可以编写自定义插件来实现这一点。如果有人需要这个,我会更新答案。在

相关问题 更多 >