2011-02-14 24 views
7

Estoy trazando algunas curvas usando la notación científica y de doble eje. He puesto un poco de color en la etiqueta, pero la configuración no parece afectar el indicador de potencia de la notación científica de su eje. ¿Hay algún truco?Cambiando el color del desplazamiento en notación científica en matplotlib

Example

Aquí está mi código:

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') 
+0

Puede echar un vistazo a la lista de correo de matplotlib. http://sourceforge.net/mailarchive/forum.php?forum_name=matplotlib-users –

+0

Creo que tick_params se ha agregado recientemente a matplotlib: http://matplotlib.sourceforge.net/users/whats_new.html#tick-params It Parece que los "colores" deberían cambiar los colores de las marcas y los colores de las etiquetas, pero no es así. ¿Intentó establecer explícitamente 'labelcolor'? – Andrew

Respuesta

10

Es probable que sólo un descuido que tick_params aún no hace esto, sino que simplemente puede establecer de forma manual.

Por ejemplo, sólo tiene que añadir estas dos líneas a su código de ejemplo:

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

Como un ejemplo más completo, utilizando el fragmento de código anterior y algunos datos aleatorios:

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

+0

Muchas gracias por su respuesta. Funciona bien ... – TazgerO

Cuestiones relacionadas