How to set axis length unit in matplotlib?

For example, x = [1 ~ 180,000] When I draw it along the x axis, it shows: 1, 20,000, 40,000, ... 180,000 these 0s are really annoying

How to change the unit of length of the x axis to 1000 so that it shows: 1, 20, 40, ... 180 and also show that somewhere its unit is in 1000.

I know that I can do a linear transformation myself. But is there a function that does this in matplotlib?

+8
python matplotlib
source share
2 answers

If you are aiming to create publication quality indicators, you will need to precisely control the axis labels. One way to do this is to extract the label text and apply your own formatting:

import pylab as plt import numpy as np # Create some random data over a large interval N = 200 X = np.random.random(N) * 10 ** 6 Y = np.sqrt(X) # Draw the figure to get the current axes text fig, ax = plt.subplots() plt.scatter(X,Y) ax.axis('tight') plt.draw() # Edit the text to your liking label_text = [r"$%i \cdot 10^4$" % int(loc/10**4) for loc in plt.xticks()[0]] ax.set_xticklabels(label_text) # Show the figure plt.show() 

enter image description here

+6
source share

You can use pyplot.ticklabel_format to set the label style to scientific notation.

 import pylab as plt import numpy as np # Create some random data over a large interval N = 200 X = np.random.random(N) * 10 ** 6 Y = np.sqrt(X) # Draw the figure to get the current axes text fig, ax = plt.subplots() plt.scatter(X,Y) ax.axis('tight') plt.draw() plt.ticklabel_format(style='sci',axis='x',scilimits=(0,0)) # Edit the text to your liking #label_text = [r"$%i \cdot 10^4$" % int(loc/10**4) for loc in plt.xticks()[0]] #ax.set_xticklabels(label_text) # Show the figure plt.show() 

Output

+3
source share

All Articles