以清晰的方式在1个轴上显示3个直方图-Matplotlib [英] Displaying 3 histograms on 1 axis in a legible way - matplotlib

查看:39
本文介绍了以清晰的方式在1个轴上显示3个直方图-Matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我生成了 3 组数据,它们以 numpy 数组的形式组织.我有兴趣将这三组数据的概率分布绘制为标准化直方图.所有三个分布看起来应该几乎相同,因此为了便于比较,将所有三个分布绘制在同一轴上似乎是明智的.

I have produced 3 sets of data which are organised in numpy arrays. I'm interested in plotting the probability distribution of these three sets of data as normed histograms. All three distributions should look almost identical so it seems sensible to plot all three on the same axis for ease of comparison.

默认情况下,matplotlib 直方图绘制为条形图,这使得我想要的图像看起来非常凌乱.因此,我的问题是是否可以强制pyplot.hist仅绘制框/圆/三角形,其中条形图的顶部将采用默认形式,以便我可以在同一张图上清晰地显示所有三个分布,或者我是否可以必须计算直方图数据,然后将其单独绘制为散点图.

By default matplotlib histograms are plotted as bars which makes the image I want look very messy. Hence, my question is whether it is possible to force pyplot.hist to only draw a box/circle/triangle where the top of the bar would be in the default form so I can cleanly display all three distributions on the same graph or whether I have to calculate the histogram data and then plot it separately as a scatter graph.

谢谢.

推荐答案

有两种方法可以同时绘制三个直方图,但这两种方法都不是您所要求的.要执行您的要求,您必须计算直方图,例如通过使用 numpy.histogram,然后使用 plot 方法绘图.仅当要通过设置每个点的大小将其他信息与点关联时,才使用 scatter .

There are two ways to plot three histograms simultaniously, but both are not what you've asked for. To do what you ask, you must calculate the histogram, e.g. by using numpy.histogram, then plot using the plot method. Use scatter only if you want to associate other information with your points by setting a size for each point.

使用 hist 的第一种替代方法涉及将所有三个数据集一次传递给 hist 方法.然后, hist 方法调整每个条形的宽度和位置,以便清楚地显示所有三组.

The first alternative approach to using hist involves passing all three data sets at once to the hist method. The hist method then adjusts the widths and placements of each bar so that all three sets are clearly presented.

第二种方法是使用 histt​​ype ='step'选项,该选项可以为每组绘制清晰的图.

The second alternative is to use the histtype='step' option, which makes clear plots for each set.

这是一个演示此的脚本:

Here is a script demonstrating this:

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(101)
a = np.random.normal(size=1000)
b = np.random.normal(size=1000)
c = np.random.normal(size=1000)

common_params = dict(bins=20, 
                     range=(-5, 5), 
                     normed=True)

plt.subplots_adjust(hspace=.4)
plt.subplot(311)
plt.title('Default')
plt.hist(a, **common_params)
plt.hist(b, **common_params)
plt.hist(c, **common_params)
plt.subplot(312)
plt.title('Skinny shift - 3 at a time')
plt.hist((a, b, c), **common_params)
plt.subplot(313)
common_params['histtype'] = 'step'
plt.title('With steps')
plt.hist(a, **common_params)
plt.hist(b, **common_params)
plt.hist(c, **common_params)

plt.savefig('3hist.png')
plt.show()

这是结果图:

请记住,您也可以使用面向对象的界面来完成所有这些操作,例如制作单个子图,等等.

Keep in mind you could do all this with the object oriented interface as well, e.g. make individual subplots, etc.

这篇关于以清晰的方式在1个轴上显示3个直方图-Matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆