Matplotlib - 标记每个 bin
我目前正在使用 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 轴刻度似乎仅限于 5 个刻度。无论我做什么,我似乎都无法改变这一点 - 即使我添加更多 xticklabels,它也只使用前五个。我不确定 Matplotlib 如何计算这个,但我假设它是从范围/数据自动计算的?
是否有某种方法可以提高 x-tick 标签的分辨率 - 甚至每个栏/箱都提高一个分辨率?
(理想情况下,我还希望以微秒/毫秒为单位重新格式化秒,但这是另一天的问题)。
其次,我想要每个单独的条形图标记 - 包含该垃圾箱中的实际数字以及所有垃圾箱总数的百分比。
最终输出可能如下所示:
Matplotlib 可能会出现类似情况吗?
干杯, 胜利者
I'm currently using Matplotlib to create a histogram:
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)
I'd like to make the x-axis labels a bit more meaningful.
Firstly, the x-axis ticks here seem to be limited to five ticks. No matter what I do, I can't seem to change this - even if I add more xticklabels, it only uses the first five. I'm not sure how Matplotlib calculates this, but I assume it's auto-calculated from the range/data?
Is there some way I can increase the resolution of x-tick labels - even to the point of one for each bar/bin?
(Ideally, I'd also like the seconds to be reformatted in micro-seconds/milli-seconds, but that's a question for another day).
Secondly, I'd like each individual bar labeled - with the actual number in that bin, as well as the percentage of the total of all bins.
The final output might look something like this:
Is something like that possible with Matplotlib?
Cheers,
Victor
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当然!要设置刻度,只需...设置刻度(请参阅
matplotlib.pyplot.xticks
或ax.set_xticks
)。 (此外,您不需要手动设置补丁的表面颜色。您只需传入关键字参数即可。)其余的,您需要使用标签做一些稍微更奇特的事情,但 matplotlib 可以做到这一点相当容易。
例如:
Sure! To set the ticks, just, well... Set the ticks (see
matplotlib.pyplot.xticks
orax.set_xticks
). (Also, you don't need to manually set the facecolor of the patches. You can just pass in a keyword argument.)For the rest, you'll need to do some slightly more fancy things with the labeling, but matplotlib makes it fairly easy.
As an example:
我想用“密度 = True”添加到直方图中的一件事是每个箱的相对频率值,搜索但我找不到可以做到这一点的函数。我制作的解决方案如下图所示:
功能:
One thing I wanted to add to the plots in the histogram with "density = True" was the relative frequency values for each bin, search but I couldn't find a function that would do that. A solution I made follows as image:
The function:
要将 SI 前缀添加到轴标签,您需要使用 QuantiPhy。事实上,在其文档中,它有一个示例展示了如何执行此操作: MatPlotLib 示例。
我想你应该在你的代码中添加这样的东西:
To add SI prefixes to your axis labels you want to use QuantiPhy. In fact, in its documentation it has an example that shows how to do this exact thing: MatPlotLib Example.
I think you would add something like this to your code: