Matplotlib pyplot axes formatter

user3397243

I have an image:

enter image description here

Here in the y-axis I would like to get 5x10^-5 4x10^-5 and so on instead of 0.00005 0.00004.

What I have tried so far is:

fig = plt.figure()
ax = fig.add_subplot(111)
y_formatter = matplotlib.ticker.ScalarFormatter(useOffset=True)
ax.yaxis.set_major_formatter(y_formatter)

ax.plot(m_plot,densities1,'-ro',label='0.0<z<0.5')
ax.plot(m_plot,densities2, '-bo',label='0.5<z<1.0')


ax.legend(loc='best',scatterpoints=1)
plt.legend()
plt.show() 

This does not seem to work. The document page for tickers does not seem to provide a direct answer.

Ffisegydd

You can use matplotlib.ticker.FuncFormatter to choose the format of your ticks with a function as shown in the example code below. Effectively all the function is doing is converting the input (a float) into exponential notation and then replacing the 'e' with 'x10^' so you get the format that you want.

import matplotlib.pyplot as plt
import matplotlib.ticker as tick
import numpy as np

x = np.linspace(0, 10, 1000)
y = 0.000001*np.sin(10*x)

fig = plt.figure()
ax = fig.add_subplot(111)

ax.plot(x, y)

def y_fmt(x, y):
    return '{:2.2e}'.format(x).replace('e', 'x10^')

ax.yaxis.set_major_formatter(tick.FuncFormatter(y_fmt))

plt.show()

image

If you're willing to use exponential notation (i.e. 5.0e-6.0) however then there is a much tidier solution where you use matplotlib.ticker.FormatStrFormatter to choose a format string as shown below. The string format is given by the standard Python string formatting rules.

...

y_fmt = tick.FormatStrFormatter('%2.2e')
ax.yaxis.set_major_formatter(y_fmt)

...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

make axes the same length in matplotlib pyplot

From Dev

Definition of matplotlib.pyplot.axes.bbox

From Dev

make axes the same length in matplotlib pyplot

From Dev

matplotlib pyplot 2 plots with different axes in same figure

From Dev

Scale matplotlib.pyplot.Axes.scatter markersize by x-scale

From Dev

matplotlib Axes.plot() vs pyplot.plot()

From Dev

pyplot axes title not showing

From Dev

Figure and axes methods in matplotlib

From Dev

On matplotlib logarithmic axes labels

From Dev

matplotlib pyplot: subplot size

From Dev

custom labels pyplot matplotlib

From Dev

Matplotlib PyPlot Lines in Histogram

From Dev

How to get a list of axes for a figure in pyplot?

From Dev

Pyplot: Shared axes and no space between subplots

From Dev

Subplots with equal and shared axes in Pyplot.jl

From Dev

What is subplot_adjust() doing to pyplot axes?

From Dev

Subplots with equal and shared axes in Pyplot.jl

From Dev

Matplotlib cursor value with two axes

From Dev

Matplotlib: get and set axes position

From Dev

Matplotlib savefig does not save axes

From Dev

Matplotlib: Colour multiple twinx() axes

From Dev

matplotlib cancel legend on axes object

From Dev

matplotlib add rectangle to Figure not to Axes

From Dev

Adding + sign to exponent in matplotlib axes

From Dev

Strange error with matplotlib axes labels

From Dev

label matplotlib imshow axes with strings

From Dev

Matplotlib's Figure and Axes explanation

From Dev

How to transform the coordinate axes in matplotlib

From Dev

What exactly is a matplotlib axes object?