matplotlib多个图表。出现错误或不好

2024-10-08 18:22:46 发布

您现在位置:Python中文网/ 问答频道 /正文

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)

plotted graphs

这是多个图表的一个接一个垂直显示的图。不能全部显示。 为什么前两个绘图轴的标签和/或记号被压缩,而其余的绘图是空的?我还能做些什么让图表看起来更好?谢谢


Tags: plotnpmaxatcolortotalsetaxes
1条回答
网友
1楼 · 发布于 2024-10-08 18:22:46

您的polyfit线太长,matplotlib试图为其腾出空间。可以使用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()

相关问题 更多 >

    热门问题