Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 第二个y比例重复轴记号_Python_Matplotlib - Fatal编程技术网

Python 第二个y比例重复轴记号

Python 第二个y比例重复轴记号,python,matplotlib,Python,Matplotlib,下面我有一些代码,它通过将3组随机数添加到一个图中(模拟从温度传感器收集的真实数据)来绘制3组随机数。 我试图在同一个图上绘制两个刻度 这里,y2List是负数,这是我想要为其创建第二个轴的数据集。我通过这里的其他问题了解了如何做到这一点 问题是,当添加每个数据点时,第二个y轴记号会再次显示,因此第二个y轴上的数字非常多。我可以通过在第二个y轴上设置一个限制来解决这个问题,这会产生如下图像: 第二个y轴比其他y轴稍暗,这是因为python在绘制每个点后在现有y轴的顶部绘制相同的数字(我可以说,

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

这里,
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)
plt.ion()#启用交互性
def makeFig():
ax.plot(xList,yList,color='blue',label='something1',如果x==0,则为其他“”)
ax.plot(xList,y1List,color='red',label='something2',如果x==0,则为其他“”)
ax2=ax.twinx()
ax2.plot(xList,y2List,color='orange',如果x==0,则标签='something'other')
ax2.set_ylim(-20,0)
xList=list()
yList=list()
y1List=list()
y2List=list()
x=0

当x时,应将图形和双轴的创建移动到循环之外。它们只需要做一次


具体来说,将
fig1=plt.figure(1)
ax=fig1.add_子批(111)
ax2=ax.twinx()
移动到循环外部。

尝试将
ax2=ax.twinx()移动到
makeFig
函数外部。你只需要做那件事就行了。我还需要将
fig1=plt.figure(1)
ax=fig1.add.subplot(111)
移动到while循环之外。谢谢你的帮助