Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change x axes scale in matplotlib

I created this plot using Matlab

enter image description here

Using matplotlib, the x-axies draws large numbers such as 100000, 200000, 300000. I would like to have something like 1, 2, 3 and a 10^5 to indicate that it's actually 100000, 200000, 300000.

Is there a simple way to create such scale in matplotlib?

like image 639
Eagle Avatar asked Jul 20 '12 10:07

Eagle


2 Answers

Try using matplotlib.pyplot.ticklabel_format:

import matplotlib.pyplot as plt ... plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0)) 

This applies scientific notation (i.e. a x 10^b) to your x-axis tickmarks

like image 190
Chris Avatar answered Oct 24 '22 00:10

Chris


This is not so much an answer to your original question as to one of the queries you had in the body of your question.

A little preamble, so that my naming doesn't seem strange:

import matplotlib from matplotlib import rc from matplotlib.figure import Figure ax = self.figure.add_subplot( 111 ) 

As has been mentioned you can use ticklabel_format to specify that matplotlib should use scientific notation for large or small values:

ax.ticklabel_format(style='sci',scilimits=(-3,4),axis='both') 

You can affect the way that this is displayed using the flags in rcParams (from matplotlib import rcParams) or by setting them directly. I haven't found a more elegant way of changing between '1e' and 'x10^' scientific notation than:

ax.xaxis.major.formatter._useMathText = True 

This should give you the more Matlab-esc, and indeed arguably better appearance. I think the following should do the same:

rc('text', usetex=True) 
like image 34
Weir_Doe Avatar answered Oct 23 '22 23:10

Weir_Doe