Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

pandas - Arrange two plots horizontally

As an exercise, I'm reproducing a plot from The Economist with matplotlib enter image description here

So far, I can generate a random data and produce two plots independently. I'm struggling now with putting them next to each other horizontally.

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

df1 = pd.DataFrame({"broadcast": np.random.randint(110, 150,size=8), 
                   "cable": np.random.randint(100, 250, size=8),
                   "streaming" : np.random.randint(10, 50, size=8)}, 
                   index=pd.Series(np.arange(2009,2017),name='year'))
df1.plot.bar(stacked=True)

df2 = pd.DataFrame({'usage': np.sort(np.random.randint(1,50,size=7)), 
                    'avg_hour': np.sort(np.random.randint(0,3, size=7) + np.random.ranf(size=7))},
                      index=pd.Series(np.arange(2009,2016),name='year'))

plt.figure()
fig, ax1 = plt.subplots()
ax1.plot(df2['avg_hour'])

ax2 = ax1.twinx()
ax2.bar(left=range(2009,2016),height=df2['usage'])

plt.show()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should try using subplots. First you create a figure by plt.figure(). Then add one subplot(121) where 1 is number of rows, 2 is number of columns and last 1 is your first plot. Then you plot the first dataframe, note that you should use the created axis ax1. Then add the second subplot(122) and repeat for the second dataframe. I changed your axis ax2 to ax3 since now you have three axis on one figure. The code below produces what I believe you are looking for. You can then work on aesthetics of each plot separately.

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

fig = plt.figure()
df1 = pd.DataFrame({"broadcast": np.random.randint(110, 150,size=8), 
                   "cable": np.random.randint(100, 250, size=8),
                   "streaming" : np.random.randint(10, 50, size=8)}, 
                   index=pd.Series(np.arange(2009,2017),name='year'))
ax1 = fig.add_subplot(121)
df1.plot.bar(stacked=True,ax=ax1)

df2 = pd.DataFrame({'usage': np.sort(np.random.randint(1,50,size=7)), 
                    'avg_hour': np.sort(np.random.randint(0,3, size=7) + np.random.ranf(size=7))},
                      index=pd.Series(np.arange(2009,2016),name='year'))

ax2 = fig.add_subplot(122)
ax2.plot(df2['avg_hour'])

ax3 = ax2.twinx()
ax3.bar(left=range(2009,2016),height=df2['usage'])

plt.show()

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...