Delete axis scale

I spent some time finding the answer to my question, so I think the new question is in order. Consider this plot:

! [enter image description here

Axis labels use scientific notation. On the Y axis, everything is fine. However, I tried and could not get rid of the scaling factor that Python added in the lower right corner. I would like to either completely remove this factor, or simply indicate it with units in the axis header or multiply them by each tick mark. Everything will look better than this ugly 1e14 .

Here is the code:

 import numpy as np data_a = np.loadtxt('exercise_2a.txt') import matplotlib as mpl font = {'family' : 'serif', 'size' : 12} mpl.rc('font', **font) import matplotlib.pyplot as plt fig = plt.figure() subplot = fig.add_subplot(1,1,1) subplot.plot(data_a[:,0], data_a[:,1], label='$T(t)$', linewidth=2) subplot.set_yscale('log') subplot.set_xlabel("$t[10^{14}s]$",fontsize=14) subplot.set_ylabel("$T\,[K]$",fontsize=14) plt.xlim(right=max(data_a [:,0])) plt.legend(loc='upper right') plt.savefig('T(t).pdf', bbox_inches='tight') 

Update: incorporating the implementation of scientificNotation in my script, the graph now looks like

enter image description here

Much nicer if you ask me. Here is the complete code for those who want to accept part of it:

 import numpy as np data = np.loadtxt('file.txt') import matplotlib as mpl font = {'family' : 'serif', 'size' : 16} mpl.rc('font', **font) import matplotlib.pyplot as plt fig = plt.figure() subplot = fig.add_subplot(1,1,1) subplot.plot(data[:,0], data[:,1], label='$T(t)$', linewidth=2) subplot.set_yscale('log') subplot.set_xlabel("$t[s]$",fontsize=20) subplot.set_ylabel("$T\,[K]$",fontsize=20) plt.xlim(right=max(data [:,0])) plt.legend(loc='upper right') def scientificNotation(value): if value == 0: return '0' else: e = np.log10(np.abs(value)) m = np.sign(value) * 10 ** (e - int(e)) return r'${:.0f} \cdot 10^{{{:d}}}$'.format(m, int(e)) formatter = mpl.ticker.FuncFormatter(lambda x, p: scientificNotation(x)) plt.gca().xaxis.set_major_formatter(formatter) plt.savefig('T(t).pdf', bbox_inches='tight', transparent=True) 
+5
source share
3 answers

Just divide the x values ​​by 1e14 :

 subplot.plot(data_a[:,0] / 1e14, data_a[:,1], label='$T(t)$', linewidth=2) 

If you want to add a shortcut to each individual tick, you will need to provide a custom formatter , for example, in the response.

If you want it to look as good as the ticks on your y axis, you could provide a function to format it with LaTeX:

 def scientificNotation(value): if value == 0: return '0' else: e = np.log10(np.abs(value)) m = np.sign(value) * 10 ** (e - int(e)) return r'${:.0f} \times 10^{{{:d}}}$'.format(m, int(e)) # x is the tick value; p is the position on the axes. formatter = mpl.ticker.FuncFormatter(lambda x, p: scientificNotation(x)) plt.gca().xaxis.set_major_formatter(formatter) 

Of course, this will greatly complicate your X axis, so you may need to display them at an angle, for example.

+5
source

You can also change the format of the tick file with ticker .

An example is FormatStrFormatter :

 import matplotlib.pyplot as plt import matplotlib.ticker as ticker fig,ax = plt.subplots() ax.semilogy(np.linspace(0,5e14,50),np.logspace(3,7,50),'b-') ax.xaxis.set_major_formatter(ticker.FormatStrFormatter('%.0e')) 

enter image description here

Also see the answers here with lots of good ideas for ways to solve this problem.

+3
source

In addition to Will Wuzent's good answer, you can set what you write in your ticks using

 plt.xticks(range(6), range(6)) 

the first range(6) is the location, and the second is the label.

+2
source

Source: https://habr.com/ru/post/1234793/


All Articles