简体   繁体   中英

Four boxplots on one figure with matplotlib

So I have multiple data files, all of which I am using dataframes in order to easily process the information. All of the files are NetCDF files. I am trying to plot 4 boxplot graphs on the same figure, so that they all are easily able to be compared (I am looking at how averages across the same time selections peak at different times). They will have differing X and Y values as this is observational data that was taken at slightly different time intervals and the different variable numbers correspond to different time-dependent variables.

I have been trying to use subplots to make this happen using matplotlib, but it just spits out three empty graphs. How can I make this happen?

Here's the code:

plt.figure(1)
plt.subplot(2, 1, 1)
ds1 = xr.open_dataset(lfile1)
one_day1 = ds1[variable1].sel(time=slice(monthofi+startday+' '+starttime,monthofi+endday+' '+endtime))
df1 = one_day1.to_pandas().to_frame(name=variable1)
df1.index=df1.index-pd.DateOffset(hours=7)
df1.boxplot(column=variable1, by=df1.index.time, whis=[10, 90], sym='')
plt.xlabel('Time')
plt.ylabel('Temperature [degF]')
plt.title('Daily Cycle')
plt.suptitle('')

plt.subplot(2, 1, 2)
ds2 = xr.open_dataset(lfile2)
one_day2 = ds2[variable2].sel(time=slice(monthofi+startday+' '+starttime,monthofi+endday+' '+endtime))
df2 = one_day2.to_pandas().to_frame(name=variable2)
df2.index=df2.index-pd.DateOffset(hours=7)
df2.boxplot(column=variable2, by=df2.index.time, whis=[10, 90], sym='')
plt.xlabel('Time')
plt.ylabel('Average Wind Speed')
plt.title('Daily Cycle')
plt.suptitle('')

plt.subplot(2, 2, 1)
ds3 = xr.open_dataset(lfile3)
one_day3 = ds3[variable3].sel(time=slice(monthofi+startday+' '+starttime,monthofi+endday+' '+endtime))
df3 = one_day3.to_pandas().to_frame(name=variable3)
df3.index=df1.index-pd.DateOffset(hours=7)
df3.boxplot(column=variable3, by=df3.index.time, whis=[10, 90], sym='')
plt.xlabel('Time')
plt.ylabel('Wind  Direction')
plt.title('Daily Cycle')
plt.suptitle('')

plt.subplot(2, 2, 2)
ds4 = xr.open_dataset(lfile4)
one_day4 = ds4[variable4].sel(time=slice(monthofi+startday+' '+starttime,monthofi+endday+' '+endtime))
df4 = one_day4.to_pandas().to_frame(name=variable4)
df4.index=df4.index-pd.DateOffset(hours=7)
df4.boxplot(column=variable4, by=df4.index.time, whis=[10, 90], sym='')
plt.xlabel('Time')
plt.ylabel('Solar Radiation [W/m^2]')
plt.title('Daily Cycle')
plt.suptitle('')

plt.show()

What exactly is not happening?

Alright, Now, I edited it so that it follows like such:

plt.figure()
ax1 = plt.subplot(2, 2, 1)
df1.boxplot(column=variable1, by=df1.index.time, whis=[10, 90], sym='')
ax1.set_title('Daily Cycle')

ax2 = plt.subplot(2, 2, 2)
df2.boxplot(column=variable2, by=df2.index.time, whis=[10, 90], sym='')
ax2.set_title('Daily Cycle')

ax3 = plt.subplot(2, 2, 3)
df3.boxplot(column=variable3, by=df3.index.time, whis=[10, 90], sym='')
ax3.set_title('Daily Cycle')

ax4 = plt.subplot(2, 2, 4)
df4.boxplot(column=variable4, by=df4.index.time, whis=[10, 90], sym='')
ax4.set_title('Daily Cycle')

plt.show()

Now, I am getting five windows with five figures. One figure that takes up the entire window has all of the data that should be in the four subplots, while the other four figures have a subplot each in them in the positions I would like, but empty.

You are only getting three graphs because you are giving subplot the wrong arguments and overwriting axes you previously created. From the documentation:

subplot(nrows, ncols, plot_number)

nrows is the number of rows of subplots in figure and and ncols is the number of columns in the figure.

Here's a working example that uses the Pandas DataFrame boxplot method:

df1 = pd.DataFrame({'a':[1,2,3],'b':[6,8,2],'c':[9,1,7]})
df2 = pd.DataFrame({'a':[15,23,32],'b':[6,80,2],'c':[9,10,7]})
df3 = pd.DataFrame({'a':[0.2,0.5,0.5],'b':[18,5,2],'c':[9,7,7]})
df4 = pd.DataFrame({'a':[51,32,20],'b':[4,3,20],'c':[7,2,1]})

fig = plt.figure()
ax1 =fig.add_subplot(2,2,1)
df1.boxplot(ax = ax1)
ax2 =fig.add_subplot(2,2,2)
df2.boxplot(ax = ax2)
ax3 =fig.add_subplot(2,2,3)
df3.boxplot(ax = ax3)
ax4 =fig.add_subplot(2,2,4)
df4.boxplot(ax = ax4)
plt.show()

在此处输入图片说明 I'm not sure why your plots are all empty. I'd check the contents of the DataFrames you are plotting.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM