Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove axis scale

I've spent some time fruitlessly searching for an answer to my question, so I think a new question is in order. Consider this plot:

![enter image description here

The axes labels use scientific notation. On the y-axis, all is well. However, I have tried and failed to get rid off the scaling factor that Python added in the lower-right corner. I would like to either remove this factor completely and simply indicate it by the units in the axis title or have it multiplied to every tick label. Everything would look better than this ugly 1e14.

Here's 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 Will's implementation of scientificNotation into my script, the plot now looks like

enter image description here

Much nicer if you ask me. Here's the complete code for anyone wanting to adopt some 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)
like image 451
Casimir Avatar asked Oct 29 '15 14:10

Casimir


1 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 the label to each individual tick, you'll have to provide a custom formatter, like in tom's answer.

If you want it to look like as nice 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 clutter your x-axis up quite a bit, so you might end up needing to display them at an angle, for example.

like image 102
Will Vousden Avatar answered Nov 07 '22 08:11

Will Vousden