[英]Changing the color of the offset in scientific notation in matplotlib
我正在使用雙軸和科學符號繪制一些曲線。 我為標簽設置了一些顏色,但設置似乎不影響其軸的科學記數法的功率指示器。 有什么伎倆嗎?
這是我的代碼:
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')
這可能只是一個疏忽, tick_params
還沒有這樣做,但你可以簡單地手動設置它。
例如,只需將這兩行添加到示例代碼中:
ax1.yaxis.get_offset_text().set_color(plot_ax1.get_color())
ax2.yaxis.get_offset_text().set_color(plot_ax2.get_color())
作為一個更完整的示例,使用上面的代碼片段和一些隨機數據:
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.