繁体   English   中英

Matplotlib具有变化的x跨度和较小尺寸的标记

[英]Matplotlib with varying x span and lower size markers

我创建了一个图,但是正如您所看到的,x轴上有重叠的条目,我希望不同的x轴标记之间的跨度很明显,以看到数字1,5,10不同。

同样,标绘值的标记也较重。 如何缩小尺寸?

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');

您可以手动绘制一件事并添加另一件事作为标签。 例如(我指出我已更改的部分作为注释):

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

结果是:

在matplotlib中手动变形X轴

但这显然与自动进行的工作相去甚远。 您可以使用此方法构建自己的“变形Axis函数”来绘制图,也可以探索一下matplotlib转换 我不知道如何使用后者,所以我不愿发表评论(我什至不确定它是否可以达到这种效果)。 希望您能以更好的方法得到答案。 同时希望这会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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