如何在python

时间:2018-08-26 13:52:19

标签: python matplotlib histogram

我是python的新生,我有一个如何在python中绘制直方图的问题。

首先,我有十个间隔,根据花瓣的长度从最小到最大平均分配。这样我就可以根据花瓣将花分成十个间隔。

花的种类是三,所以我想绘制一个直方图来描述不同种类(间隔)中不同种类花的分布。在同一个垃圾箱中,不同的花朵具有不同的颜色。

我知道Matplotlib中的hist函数,但是我不知道如何使用它来绘制图片,如下所示。

wanted histogram

数据为Lbins = [0.1 , 0.34, 0.58, 0.82, 1.06, 1.3 , 1.54, 1.78, 2.02, 2.26, 2.5 ],而Data_bins是形状的数组(花朵数为3)。

1 个答案:

答案 0 :(得分:0)

以下是一个使用Matplotlib中的hist的直方图示例,其中每个条带都有多个条形:

import numpy as np
import matplotlib.pyplot as plt

length_of_flowers = np.random.randn(100, 3)
Lbins = [0.1 , 0.34, 0.58, 0.82, 1.06, 1.3 , 1.54, 1.78, 2.02, 2.26, 2.5 ]
# Lbins could also simply the number of wanted bins

colors = ['red','yellow', 'blue']
labels = ['red flowers', 'yellow flowers', 'blue flowers']
plt.hist(length_of_flowers, Lbins,
         histtype='bar',
         stacked=False,  
         fill=True,
         label=labels,
         alpha=0.8, # opacity of the bars
         color=colors,
         edgecolor = "k")

# plt.xticks(Lbins) # to set the ticks according to the bins
plt.xlabel('flower length'); plt.ylabel('count');
plt.legend();
plt.show()

给出:

example hist

编辑:受this matplotlib demo启发的预合并数据的解决方案。每个条的位置是自定义计算的。我通过替换零值来验证对齐是否正确,从而对数据进行了一些修改。

import numpy as np
import matplotlib.pyplot as plt

binned_data = np.array([[41., 3., 3.], [ 8., 3., 3.], [ 1., 2., 2.], [ 2., 7., 3.],
                        [ 0., 20., 0.], [ 1., 21., 1.], [ 1., 2., 4.], [ 3., 4., 23.],
                        [ 0., 0., 9.], [ 3., 1., 14.]]).T

# The shape of the data array have to be:
#  (number of categories x number of bins)
print(binned_data.shape)  # >> (3, 10)

x_positions = np.array([0.1, 0.34, 0.58, 0.82, 1.06, 1.3, 1.54, 1.78, 2.02, 2.26])

number_of_groups = binned_data.shape[0]
fill_factor =  .8  # ratio of the groups width
                   # relatively to the available space between ticks
bar_width = np.diff(x_positions).min()/number_of_groups * fill_factor

colors = ['red','yellow', 'blue']
labels = ['red flowers', 'yellow flowers', 'blue flowers']

for i, groupdata in enumerate(binned_data): 
    bar_positions = x_positions - number_of_groups*bar_width/2 + (i + 0.5)*bar_width
    plt.bar(bar_positions, groupdata, bar_width,
            align='center',
            linewidth=1, edgecolor='k',
            color=colors[i], alpha=0.7,
            label=labels[i])

plt.xticks(x_positions);
plt.legend(); plt.xlabel('flower length'); plt.ylabel('count');

给出:

example with binned data