2016-08-26 41 views
0

我正在針對固定x軸繪製兩個numpy陣列,分別稱爲line1和line2。一個陣列將被繪製在對數圖中,使用半對數並且另一個陣列被繪製在相同x軸上的基本線性圖中。如果一個繪圖是線性的,另一個是對數,則在雙Y軸繪圖中兩個y軸的刻度間隔均爲對數

這兩個y軸的縮放格式似乎都很好。然而,右側線性圖(第2行)的y軸刻度間隔和刻度似乎是對數(間隔較近的刻度),我似乎無法找到改變它的方法。

有沒有辦法將下圖中的右側y軸更改爲線性?

這裏是我的代碼:

import numpy as np 
import matplotlib.pyplot as plt 

x = np.arange(0.01, 10.0, 0.01) 
y = np.exp(x) 
y2 = np.sin(2*np.pi*x) 

fig1 = plt.figure() 
ax1 = fig1.add_subplot(111) 
line1 = ax1.semilogy(x,y) 
ax2 = fig1.add_subplot(111, sharex=ax1, frameon=False) 
line2 = ax2.plot(x,y2,"r")  

#MATPLOTLIB BUG? THE SECOND Y AXES HAS A LOG SCALE AND THE TICK LABELS CAN'T BE CHANGED 
ax2.set_yscale('linear') 
ax2.yaxis.tick_right() 

plt.show() 

enter image description here

回答

1

使用ax2 = ax1.twinx()而不是add_subplot一次。這將把新軸上的y-tick和舊軸上的y-tick分開。從該文檔:

twinx()

創建軸的雙用於生成與sharex x軸,但獨立的Y軸的曲線圖。自身的y軸將在左側有刻度線,返回的軸將在右側有刻度線。

您還那麼不需要設置yscalelinear,或將蜱的權利,因爲這是由twinx

import numpy as np 
import matplotlib.pyplot as plt 

x = np.arange(0.01, 10.0, 0.01) 
y = np.exp(x) 
y2 = np.sin(2*np.pi*x) 

fig1 = plt.figure() 
ax1 = fig1.add_subplot(111) 
line1 = ax1.semilogy(x,y) 
ax2 = ax1.twinx()  # <-- Note the change to twinx here 
line2 = ax2.plot(x,y2,"r")  

plt.show() 

enter image description here

自動完成
相關問題