Matplotlib-标记每个垃圾箱

我目前正在使用Matplotlib创建直方图:

import matplotlib

matplotlib.use('Agg')

import matplotlib.pyplot as pyplot

...

fig = pyplot.figure()

ax = fig.add_subplot(1,1,1,)

n, bins, patches = ax.hist(measurements, bins=50, range=(graph_minimum, graph_maximum), histtype='bar')


#ax.set_xticklabels([n], rotation='vertical')


for patch in patches:

    patch.set_facecolor('r')


pyplot.title('Spam and Ham')

pyplot.xlabel('Time (in seconds)')

pyplot.ylabel('Bits of Ham')

pyplot.savefig(output_filename)

我想使x轴标签更有意义。


首先,这里的x轴刻度线似乎仅限于五个刻度线。无论我做什么,我似乎都无法更改-即使添加更多xticklabel,它也只会使用前五个。我不确定Matplotlib是如何计算的,但是我认为它是根据范围/数据自动计算的?


有什么方法可以提高X-tick标签的分辨率 -甚至可以将每个小节/条提高到一个?


(理想情况下,我还希望将秒重新设置为微秒/毫秒,但这又是一个问题)。


其次,我想标记每个单独的条形 -该垃圾箱中的实际数字,以及所有垃圾箱总数的百分比。


最终输出可能看起来像这样:

Matplotlib有可能做到这一点吗?


干杯,维克多


料青山看我应如是
浏览 396回答 2
2回答

梦里花落0921

要将SI前缀添加到轴标签,您需要使用QuantiPhy。实际上,在其文档中有一个示例演示了如何执行此操作:MatPlotLib Example。我认为您可以在代码中添加如下内容:from matplotlib.ticker import FuncFormatterfrom quantiphy import Quantitytime_fmtr = FuncFormatter(lambda v, p: Quantity(v, 's').render(prec=2))ax.xaxis.set_major_formatter(time_fmtr)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python