2017-05-11 2 views
0
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 graphsmatplotlib mehrere Diagramme. Apperance falsch oder schlecht

Dies ist eine grafische Darstellung der mehrere Diagramme vertikal nacheinander angezeigt. Kann nicht alle deutlich anzeigen. Warum werden die Beschriftungen und/oder Teilstriche der Achse für die ersten beiden Diagramme gequetscht und der Rest des Diagramms ist leer? Was kann ich noch tun, um die Charts besser aussehen zu lassen? Danke

Antwort

0

Ihre Polyfit-Linie ist zu lang und matplotlib versucht, Platz dafür zu machen. Sie können die automatische Skalierung für die längere Grundstück deaktivieren axis.set_autoscale_on(False) mit:

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() 

Alternativ können Sie die aktuellen Grenzen erhalten axis.get_xlim()/axis.get_ylim() mit und erzwingen sie später axis.set_xlim()/axis.set_ylim() mit:

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() 
Verwandte Themen