[英]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.