根据数据框

时间:2017-11-13 16:18:11

标签: python pandas dataframe matplotlib plot

我有一个看起来像这样的条形图: fig1

我希望根据我的数据框dat的列,制作六个不同的条形图(每个"一个季节"例如MAM 16)。

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)都是它自己的图。

1 个答案:

答案 0 :(得分:4)

您想使用绘图功能的subplots=Truelayout参数。这允许获得图的子图网格。

以下是一个可运行的示例(其中我替换了字符串"意思是"和" 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()

enter image description here

要以不同方式设置条形图的颜色,您需要手动设置它们。

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

enter image description here