pandas 基于数据框中的列创建6个条形子图

xyhw6mcr  于 2023-08-01  发布在  其他
关注(0)|答案(1)|浏览(66)

我有一个条形图,看起来有点像这样:

的数据
我希望制作六个不同的条形图(每个“季节”一个,eidogg。MAM 16)基于我的dataframe dat的列。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

labels = {'pod','MAM-16',  'MAM-17',  'JJAS-16', 'JJAS-17', 'OND-15','OND-16'}
rf = [('22','343.7','467.4', '157', '251', '100','142.5'),
('72',  '82',  '184.4',   '143.3',  '12.7',  '120',  '152.4'),
('79',  '76.5',    '167.4',   '118.1',   '185.4',  '190', '145'),
('86',  '993.4',   '66.5',    '198.9',   '14', '78', '84.8'),
('87',  '206.2',   '178.1',   '121.4',   '285.2',  '89' ,'65'),
('88',  '209.3',   '280.4',  '138.4',   '279.9', '84',  '141'),
('90' , '134.9',   '137.9',   '92.7',    '224', '111', '133.1'),
('93',  '180.8',   '113.8',   '179.6',   '108.2', '184',  '211.8'),
('95',  '329.7',   '176.5',   '168.9',   '64', '75','103.6'),
('96', '270.5',   '158.5',   '196.6',   '363', '128','152.4'),
('97',  '167.9',   '103.1',   '184.4',   '117.1',  '132', '104.1'),
dat = pd.DataFrame.from_records(rf, columns=labels); dat

字符串
dat中的第一列引用不同的ID,它是上图中的彩色条,不是数字。其余的值都是数值。
我知道我可以用这样的东西把所有的值都标在一起:

ax = dat.plot(kind='bar',rot=0,lw=2,colormap='jet',figsize=(10,4),
             title='Sample title')
x1 = [0,1,2,3,4,5]
labels = ['MAM 16', 'MAM 17', 'JJAS 16','JJAS 17', 'OND 15','OND 16']
ax1.set_xticks(x1)
ax1.set_xticklabels(labels, minor=False, rotation=45)
ax.set_xticklabels(labels)
plt.show()


为了得到子情节,我想我可以使用for循环:

for col in dat:    
    fig = plt.figure(figsize=(5.0, 6.0))

    axes1 = fig.add_subplot(3, 2, 1)
    axes2 = fig.add_subplot(3, 2, 2)
    axes3 = fig.add_subplot(3, 2, 3)

    axes1.set_ylabel('rainfall / mm')
    axes1.plot(???)

    axes2.set_ylabel('total rainfall / mm')
    axes2.plot(???)

    axes3.set_ylabel('total rainfall / mm')
    axes3.plot(???)

    fig.tight_layout()
    plt.show()


结果应该是一个2x3的子图矩阵,其中上图(图1)中的每组条形图都是自己的图。

zxlwwiss

zxlwwiss1#

您希望使用绘图函数的subplots=Truelayout参数。这允许获得图的子图网格。
下面是一个可运行的示例(我用一些有用的东西替换了字符串“mean”和“var”)。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

labels = {'pod','MAM-16',  'MAM-17',  'JJAS-16', 'JJAS-17', 'OND-15','OND-16'}
rf = [('22','343.7','467.4', '157', '251', '100','142.5'),
('72',  '82',  '184.4',   '143.3',  '12.7',  '120',  '152.4'),
('79',  '76.5',    '167.4',   '118.1',   '185.4',  '190', '145'),
('86',  '993.4',   '66.5',    '198.9',   '14', '78', '84.8'),
('87',  '206.2',   '178.1',   '121.4',   '285.2',  '89' ,'65'),
('88',  '209.3',   '280.4',  '138.4',   '279.9', '84',  '141'),
('90' , '134.9',   '137.9',   '92.7',    '224', '111', '133.1'),
('93',  '180.8',   '113.8',   '179.6',   '108.2', '184',  '211.8'),
('95',  '329.7',   '176.5',   '168.9',   '64', '75','103.6'),
('96', '270.5',   '158.5',   '196.6',   '363', '128','152.4'),
('97',  '167.9',   '103.1',   '184.4',   '117.1',  '132', '104.1'),
('98', '394', '204.6',   '53.6',    '332.5', '85',  '103.4'),
('99', '243', '103.6',   '33.2',    '112.5', '25',  '37.9')]   
dat = pd.DataFrame.from_records(rf, columns=labels).astype(float)

labels = ['MAM 16', 'MAM 17', 'JJAS 16','JJAS 17', 'OND 15','OND 16']
dat = dat[['MAM-16',  'MAM-17',  'JJAS-16', 'JJAS-17', 'OND-15','OND-16']]
axes = dat.plot(kind='bar',rot=0,lw=2,colormap='jet',figsize=(10,4),
             title='Sample title', subplots=True, layout=(3,2))

plt.show()

字符串


的数据
若要设置不同的条形颜色,需要手动设置。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

labels = {'pod','MAM-16',  'MAM-17',  'JJAS-16', 'JJAS-17', 'OND-15','OND-16'}
rf = [('22','343.7','467.4', '157', '251', '100','142.5'),
('72',  '82',  '184.4',   '143.3',  '12.7',  '120',  '152.4'),
('79',  '76.5',    '167.4',   '118.1',   '185.4',  '190', '145'),
('86',  '993.4',   '66.5',    '198.9',   '14', '78', '84.8'),
('87',  '206.2',   '178.1',   '121.4',   '285.2',  '89' ,'65'),
('88',  '209.3',   '280.4',  '138.4',   '279.9', '84',  '141'),
('90' , '134.9',   '137.9',   '92.7',    '224', '111', '133.1'),
('93',  '180.8',   '113.8',   '179.6',   '108.2', '184',  '211.8'),
('95',  '329.7',   '176.5',   '168.9',   '64', '75','103.6'),
('96', '270.5',   '158.5',   '196.6',   '363', '128','152.4'),
('97',  '167.9',   '103.1',   '184.4',   '117.1',  '132', '104.1'),
('98', '394', '204.6',   '53.6',    '332.5', '85',  '103.4'),
('99', '243', '103.6',   '33.2',    '112.5', '25',  '37.9')]   
dat = pd.DataFrame.from_records(rf, columns=labels).astype(float)

labels = ['MAM 16', 'MAM 17', 'JJAS 16','JJAS 17', 'OND 15','OND 16']
dat = dat[['MAM-16',  'MAM-17',  'JJAS-16', 'JJAS-17', 'OND-15','OND-16']]

axes = dat.plot(kind='bar',rot=0,lw=2, figsize=(10,4), legend=False,
             title='Sample title', subplots=True, layout=(3,2))
colors = plt.cm.jet(np.linspace(0,1,len(dat)))
for ax in axes.flat:
    for i,bar in enumerate(ax.patches):
        bar.set_color(colors[i])
plt.show()


相关问题