[英]Create 6 bar subplots based on columns in dataframe
我希望根据数据框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,这些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的子图矩阵,其中上图(fig1)中的每组条形图都是其自己的图。
您要使用绘图功能的subplots=True
和layout
参数。 这允许获得绘图的子绘图网格。
以下是一个可运行的示例(在这里我用了一些有用的东西替换了字符串“ 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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.