[英]Second y scale repeating axis ticks
我下面有一些代碼,該代碼通過將3組隨機數添加到繪圖中(模擬從例如溫度傳感器收集的真實世界數據)來繪制3組隨機數。 我正在嘗試在同一地塊上制作2個比例尺。
在此, y2List
為負,這是我要為其創建第二個軸的數據集。 我通過這里的其他問題弄清楚了如何做到這一點。
問題在於,當添加每個數據點時,將再次顯示第二個y軸刻度,因此第二個y軸上擠滿了數字。 我可以通過在第二個y軸上設置一個限制來解決這個問題,該限制會產生如下圖像:
第二個y軸比其他y軸稍暗,這是因為python在繪制每個點之后在現有的y軸上繪制相同的數字(我可以知道,因為繪制每個點時數字會變暗)
我的問題...是否有辦法使第二個y軸僅繪制第二個比例尺一次? 顯然,這只是為了使情節具有美感,但一點點幫助!
我的代碼如下:
plt.ion() # enable interactivity
def makeFig():
ax.plot(xList, yList, color='blue', label='something1' if x == 0 else '')
ax.plot(xList, y1List, color='red', label='something2' if x == 0 else '')
ax2 = ax.twinx()
ax2.plot(xList, y2List, color='orange', label='something else' if x == 0 else '')
ax2.set_ylim(-20,0)
xList=list()
yList=list()
y1List=list()
y2List=list()
x=0
while x<11:
fig1=plt.figure(1)
ax = fig1.add_subplot(111)
x_1 = datetime.datetime.now()
date_formatter = DateFormatter('%H:%M:%S')
y=np.random.random()
y1=np.random.random() *3
y2=np.random.random() *(-13)
xList.append(x_1)
yList.append(y)
y1List.append(y1)
y2List.append(y2)
makeFig()
plt.gcf().autofmt_xdate()
ax = plt.gca()
ax.xaxis.set_major_formatter(date_formatter)
max_xticks = 10
xloc = plt.MaxNLocator(max_xticks)
ax.xaxis.set_major_locator(xloc)
plt.get_current_fig_manager().window.wm_geometry("940x700+5+0")
plt.draw()
plt.legend(loc=2, bbox_to_anchor=(1, 0.5), prop={'size':10})
x+=1
plt.pause(0.5)
您應該將圖形的創建和雙軸移動到循環之外。 他們只需要做一次。
具體來說,將fig1=plt.figure(1)
, ax = fig1.add_subplot(111)
和ax2 = ax.twinx()
循環外。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.