如何在python中绘制具有多个类别的直方图 [英] How to draw an histogram with multiple categories in python

查看:166
本文介绍了如何在python中绘制具有多个类别的直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

数据是 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).

解决方案

下面是一个使用

I am a freshman in python, and I have a problem of how to draw a histogram in python.

First of all, I have ten intervals that are divided evenly according to the length of flowers' petal, from min to max. Thus I can separate flowers into ten intervals based on petals.

The number of flowers' kind is three, so I want to draw a histogram to describe the distribution of different kinds of flowers in different intervals(bins). And in the same bin, different flowers have different colors.

I know the hist functions in Matplotlib, but I don't know how to use it to draw pictures like below.

The data are Lbins = [0.1 , 0.34, 0.58, 0.82, 1.06, 1.3 , 1.54, 1.78, 2.02, 2.26, 2.5 ] and Data_bins is an array of shape (number of flowers, 3).

解决方案

Here is an example of an histogram with multiple bars for each bins using hist from Matplotlib:

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()

which gives:

Edit: Solution for pre-binned data inspired from this matplotlib demo. The position of each bar is custom computed. I slightly modified the data by replacing zero values to verify correct alignment.

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');

which gives:

这篇关于如何在python中绘制具有多个类别的直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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