2017-06-05 84 views
0

我創建了一個圖,但是您可以看到x軸具有重疊的條目,我希望不同的x軸標記之間的跨度很明顯,以查看數字1,5 ,10個不同。Matplotlib具有不同的x跨度和較小的標記

此外,繪製值的標記稍重一些。我怎樣才能減小尺寸?

import matplotlib.pyplot as plt; 
import numpy as np; 
from matplotlib import rc; 

filename = 'plot.pdf'; 

fig, ax1 = plt.subplots(frameon=False); 
rc('mathtext', default='regular'); 
rc('lines',lw=2); 
rc('lines',mew=2); 
rc('text', usetex=True); 

x = np.array([1,5,10,50,100,250,500]); 

t_err = np.array([106.50, 99.53, 94.70, 94.65, 93.40, 93.38, 92.81]); 
c_err = np.array([ 66.48, 65.11, 62.08, 60.09, 59.36, 60.32, 61.17]); 
lns1 = ax1.plot(x,t_err,'bs:', label="T"); 
lns2 = ax1.plot(x,c_err,'bs-',label="C"); 

ax1.set_ylabel('Error',color='b',size=12); 
ax1.set_ylim([50,100]); 
ax1.set_xlim([1,500]); 
ax1.set_xticks(x); 
ax1.tick_params(axis='y', which=u'both', length=0, labelsize=10, colors='b'); 
ax1.tick_params(axis='x', which=u'both', length=0, labelsize=10); 

lns = lns1 + lns2; 
labs = [l.get_label() for l in lns]; 

ax1.set_xlabel('# of Iterations',size=14); 
ax1.legend(lns, labs, bbox_to_anchor=(1.02,1.0), loc=2, borderaxespad=2.5, ncol = 1); 
fig.savefig(filename,format='pdf',transparent=True, bbox_inches='tight'); 
+0

我非常清楚這一點。但這是我習慣的風格。來自C/C++時代。 – Shew

+1

嘗試一個日誌規模?例如'ax.set_xscale('log',basex = 2)' – GWW

回答

1

您可以手動繪製一件東西並添加另一件作爲標籤。例如(我指出了評論我已經改變了部分):這將導致該

import matplotlib.pyplot as plt; 
import numpy as np; 
from matplotlib import rc; 

filename = 'plot.pdf'; 

fig, ax1 = plt.subplots(frameon=False); 
rc('mathtext', default='regular'); 
rc('lines',lw=2); 
rc('lines',mew=2); 
rc('text', usetex=False); # Change to False because I don't have Latex 

x = np.array([1,5,10,50,100,250,500]); 
xo = np.array([10,20,30,40,70,110,160]); # changed here 

t_err = np.array([106.50, 99.53, 94.70, 94.65, 93.40, 93.38, 92.81]); 
c_err = np.array([ 66.48, 65.11, 62.08, 60.09, 59.36, 60.32, 61.17]); 
lns1 = ax1.plot(xo ,t_err,'bs:', label="T"); # changed here 
lns2 = ax1.plot(xo ,c_err,'bs-',label="C"); # changed here 

ax1.set_ylabel('Error',color='b',size=12); 
ax1.set_ylim([50,100]); 
ax1.set_xlim([1,xo.max()]); #changed here 
ax1.set_xticks(xo) # changed here 
ax1.set_xticklabels([str(i) for i in x]); # Changed here 
ax1.tick_params(axis='y', which=u'both', length=0, labelsize=10, colors='b'); 
ax1.tick_params(axis='x', which=u'both', length=0, labelsize=10); 

lns = lns1 + lns2; 
labs = [l.get_label() for l in lns]; 

ax1.set_xlabel('# of Iterations',size=14); 
ax1.legend(lns, labs, bbox_to_anchor=(1.02,1.0), loc=2, borderaxespad=2.5, ncol = 1); 
plt.show() 

Manually deforming X axis in matplotlib

但很明顯這正好從自動東西很長的路要走。你可以建立你自己的「變形軸功能」來使用這種方法繪製一個圖,或者探索一下matplotlib transformations。我不知道如何使用後者,所以我寧願不發表評論(我甚至不確定它可以實現這種效果)。希望你能通過更好的方法得到答案。同時希望這有助於。