matplotlib 多个图表。错误或糟糕的外表

matplotlib multiple charts. wrong or bad apperance

rloggdp = np.log(riogdp['GDP (millions of US$)']) # logarithm of gdp for countries played at rio
sloggdp = np.log(sochigdp['GDP (millions of US$)']) # logarithm of gdp for countries played at sochi

# --- the plots
fig, axes = plt.subplots(4, 1, figsize=(50, 100))
#x = np.arange(0,len(count['countries']),1)
# plot 0
axes[0].scatter(rloggdp, riogdp['Total'], 5, color='blue')
# plot 1
axes[1].scatter(sloggdp, sochigdp['Total'], 5, color='blue')
# plot 2
axes[2].scatter(riosochi['Totaltoprio'], riosochi['Totaltopsochi'], 5, color='blue')
# plot 3
xlocs = np.arange(len(sortrs.index))
axes[3].bar(xlocs-0.4, sortrs['Totaltoprio'], 0.4,
 color='blue', label='Medals won at Rio')
axes[3].bar(xlocs, sortrs['Totaltopsochi'], 0.4,
 color='green', label='Medals won at Sochi')

# --- pretty-up and save
# plot 0
xrange = np.arange(0, rloggdp.max(), 1)
axes[0].set_xticks(xrange)
axes[0].set_xticklabels(xrange)
axes[0].tick_params(axis='both', which='major', length=5, width=20, labelsize=25,)
axes[0].grid(True, which='both')
#axes[0].yaxis.grid(True)
#axes.legend(loc='best', fontsize=25)
axes[0].set_ylabel('Total Medal Count', labelpad=20, fontsize=25)
axes[0].set_xlabel('GDP of a country', labelpad=20, fontsize=25)
axes[0].set_yticks(np.arange(0, toprio['Total'].max(), 5))
#plt.ylim([0,2*countT['total_medals'][0]])
m, b  = np.polyfit(rloggdp,riogdp['Total'], 1)
axes[0].plot(x, m*x + b, '-', color ='darkorchid', linewidth=2)
axes[0].set_title('GDP vs Total medal counts won at Rio', fontsize=25)
# plot 1
xrange = np.arange(0, sloggdp.max(), 1)
axes[1].set_xticks(xrange)
axes[1].set_xticklabels(xrange)
axes[1].tick_params(axis='both', which='major', labelsize=25,)
axes[1].yaxis.grid(True)
#axes.legend(loc='best', fontsize=25)
axes[1].set_ylabel('Total Medal Count', labelpad=20, fontsize=25)
axes[1].set_xlabel('GDP of a country', labelpad=20, fontsize=25)
axes[1].set_yticks(np.arange(0, topsochi['Total'].max(), 5))
#plt.ylim([0,2*countT['total_medals'][0]])
m, b  = np.polyfit(sloggdp,sochigdp['Total'], 1)
axes[1].plot(x, m*x + b, '-', color ='darkorchid', linewidth=2)
axes[1].set_title('GDP vs Total medal counts won at Sochi', fontsize=25)
# plot 2
xrange = np.arange(0, riosochi['Totaltoprio'].max(), riosochi['Totaltoprio'].max()/40)
axes[2].set_xticks(xrange)
axes[2].set_xticklabels(xrange, rotation=90)
axes[2].tick_params(axis='both', which='major', labelsize=20,)
axes[2].yaxis.grid(True)
#axes.legend(loc='best', fontsize=25)
axes[2].set_ylabel('Total Medal Count won at Sochi', labelpad=20, fontsize=25)
axes[2].set_xlabel('Total Medal Count won at Rio', labelpad=20, fontsize=25)
axes[2].set_yticks(np.arange(0, topsochi['Total'].max(), 5))
#plt.ylim([0,2*countT['total_medals'][0]])
m, b  = np.polyfit(riosochi['Totaltoprio'], riosochi['Totaltopsochi'], 1)
axes[2].plot(x, m*x + b, '-', color ='darkorchid', linewidth=2)
axes[2].set_title('Total medal counts won at Rio vs Total medal counts won at Sochi', fontsize=25)
# plot 3
axes[3].set_xticks(xlocs)
axes[3].set_xticklabels(sortr.index, rotation=90)
axes[3].tick_params(axis='both', which='major', labelsize=25)
axes[3].yaxis.grid(True)
axes[3].legend(loc='best', fontsize=25)
axes[3].set_ylabel('Total Medal counts', labelpad=20, fontsize=25)
axes[3].set_xlabel('Countries', labelpad=20, fontsize=20)
axes[3].set_yticks(np.arange(0, max(riosochi['Totaltoprio'].max(), riosochi['Totaltopsochi'].max()), 5))
axes[3].set_title('Total medal counts won at Rio and total medal counts won at Sochi for all countries', fontsize=25)

fig.suptitle("Olympic Medals and GDP Charts", y=1.03, fontsize=35)
#fig.tight_layout(pad=2)
fig.savefig('ctm.png', dpi=125)

这是一个由多个图表垂直依次显示的图表。无法清楚地显示所有这些。 为什么前两个图的轴标签 and/or 刻度被挤压而图的其余部分是空的?我还能做些什么来使图表看起来更好?谢谢

您的 polyfit 线太长,matplotlib 试图为它创建 space。您可以使用 axis.set_autoscale_on(False):

禁用较长绘图的自动缩放
import numpy as np
import matplotlib.pyplot as plt

f, x = plt.subplots(1, 1)
x.plot(np.arange(10), np.arange(10) ** 2)
x.set_autoscale_on(False)                    # disable autoscale so that all following plots don't change limits
x.plot(np.arange(100), 2 ** np.arange(100))  # this plot will be too wide and high
plt.show()

或者,您可以使用 axis.get_xlim()/axis.get_ylim() 获取当前限制,稍后使用 axis.set_xlim()/axis.set_ylim():

强制执行它们
import numpy as np
import matplotlib.pyplot as plt

f, x = plt.subplots(1, 1)
x.plot(np.arange(10), np.arange(10) ** 2)
xlims = x.get_xlim()                         # get current x limits
ylims = x.get_ylim()                         # get current y limits
x.plot(np.arange(100), 2 ** np.arange(100))  # this plot will be too wide and high
x.set_xlim(xlims)                            # set previous x limits
x.set_ylim(ylims)                            # set previous y limits
plt.show()