gpt4 book ai didi

python - 第二个 y 刻度重复轴刻度

转载 作者:太空宇宙 更新时间:2023-11-03 16:55:44 24 4
gpt4 key购买 nike

下面有一些代码,通过将 3 组随机数添加到绘图中来绘制它们(模拟从温度传感器收集的真实世界数据)。我正在尝试在同一个地 block 上制作两个比例。

这里,y2List 是负数,这是我想为其创建第二个轴的数据集。我想出了如何使用这里的其他问题来做到这一点。

问题是,当添加每个数据点时,第二个 y 轴刻度会再次显示,因此第二个 y 轴上充满了数字。我可以通过在第二个 y 轴上设置限制来解决这个问题,这会生成如下图像:

enter image description here

第二个 y 轴比其他轴稍暗,这是因为在绘制每个点后,python 在现有数字的顶部绘制相同的数字(我可以看出,因为绘制每个点时数字会变暗)

我的问题...有没有办法让第二个 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()在循环之外。

关于python - 第二个 y 刻度重复轴刻度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35434423/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com