简体   繁体   中英

Matplotlib with varying x span and lower size markers

I created a plot, but as you can see the x-axis has overlapping entries, I want the span between different x-axis markers to be apparent to see the numbers 1,5, 10 distinct.

Also, the markers for the plotted values are bit heavier. How can I reduce the size ?

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

You can manually plot one thing and add another as label. For example (I pointed out as comment the parts I've changed):

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

Which results in this:

在matplotlib中手动变形X轴

But obviously this goes a long way from something automatic. You can either build your own "deforming Axis function" to make a plot using this method or perhaps explore a bit the matplotlib transformations . I don't know how to use the latter so I rather not comment (I'm not even sure it can achieve this effect). Hopefully you'll get an answer with a better approach. In the mean time hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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