I have a bar plot that looks sort of like this:
I am hoping to make six different bar plots (one per "season", eg MAM 16) based on the columns of my 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
The first column in dat
refers to a different ID which are the colored bars in the figure above and is not numeric. The rest of the values are numeric.
I know I can plot all the values together using something like this:
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()
To get subplots I imagine I could use a for-loop:
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()
The result should be a 2x3 matrix of subplots where each group of bar plots from the image (fig1) above is its own plot.
You want to use the subplots=True
and layout
argument of the plotting function. This allows to obtain a subplot grid of the plots.
The following is a runnable example (where I replaced the strings "mean" and "var" by something useful).
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()
To set the colors of the bars differently, you need to manually set them.
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.