Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the color of the offset in scientific notation in matplotlib

I am plotting some curves using twin-axis and also scientific notation. I have set some color to the label but the setting don't seem to affect the power indicator of the scientific notation of its axis. Is there any trick?

Example

Here is my code:

fig = pylab.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()

# Plotting the data
plot_ax1, = ax1.plot()
plot_ax2, = ax2.plot()

# Setting the label colors
ax2.yaxis.set_offset_position('right') # To set the power indicator of ax2 
ax1.yaxis.label.set_color(plot_ax1.get_color())
ax2.yaxis.label.set_color(plot_ax2.get_color())

# Setting the ticker properties     
tkw = dict(size=4, width=1.5)
ax1.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
ax2.ticklabel_format(style='sci', scilimits=(0,0), axis='y')        
ax1.tick_params(axis='y', colors=plot_ax1.get_color(), **tkw)
ax2.tick_params(axis='y', colors=plot_ax2.get_color(), **tkw)
ax1.tick_params(axis='x', **tkw)

# Setting the legend
lines = [plot_ax1, plot_ax2]
ax1.legend(lines, [l.get_label() for l in lines],'upper left')
like image 700
TazgerO Avatar asked Feb 14 '11 10:02

TazgerO


People also ask

How do I assign a line color in matplotlib?

The usual way to set the line color in matplotlib is to specify it in the plot command. This can either be done by a string after the data, e.g. "r-" for a red line, or by explicitely stating the color argument.

How do you enter scientific notation in matplotlib?

To show decimal places and scientific notation on the axis of a matplotlib, we can use scalar formatter by overriding _set_format() method.


1 Answers

It's probably just an oversight that tick_params doesn't already do this, but you can simply set it manually.

For example, just add these two lines to your example code:

ax1.yaxis.get_offset_text().set_color(plot_ax1.get_color())
ax2.yaxis.get_offset_text().set_color(plot_ax2.get_color())

As a more complete example, using your code snippet above and some random data:

import matplotlib.pyplot as plt
import numpy as np

numdata = 100
t = np.linspace(0.05, 0.11, numdata)
x1 = np.cumsum(np.random.random(numdata) - 0.5) * 40000
x2 = np.cumsum(np.random.random(numdata) - 0.5) * 0.002

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()

# Plotting the data
plot_ax1, = ax1.plot(t, x1, 'r-', label='x1')
plot_ax2, = ax2.plot(t, x2, 'g-', label='x2')

# Setting the label colors
ax2.yaxis.set_offset_position('right') # To set the power indicator of ax2 
ax1.yaxis.label.set_color(plot_ax1.get_color())
ax2.yaxis.label.set_color(plot_ax2.get_color())

# Setting the ticker properties     
tkw = dict(size=4, width=1.5)
ax1.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
ax2.ticklabel_format(style='sci', scilimits=(0,0), axis='y')        
ax1.tick_params(axis='y', colors=plot_ax1.get_color(), **tkw)
ax2.tick_params(axis='y', colors=plot_ax2.get_color(), **tkw)

ax1.yaxis.get_offset_text().set_color(plot_ax1.get_color())
ax2.yaxis.get_offset_text().set_color(plot_ax2.get_color())

ax1.tick_params(axis='x', **tkw)

# Setting the legend
lines = [plot_ax1, plot_ax2]
ax1.legend(lines, [l.get_label() for l in lines],'upper left')

plt.show()

enter image description here

like image 68
Joe Kington Avatar answered Nov 09 '22 00:11

Joe Kington