简体   繁体   English

第二y刻度重复轴刻度

[英]Second y scale repeating axis ticks

I have some code below which plots 3 sets of random numbers by adding them to a plot (simulating real world data gathered from say a temperature sensor). 我下面有一些代码,该代码通过将3组随机数添加到绘图中(模拟从例如温度传感器收集的真实世界数据)来绘制3组随机数。 I am attempting to make 2 scales on the same plot. 我正在尝试在同一地块上制作2个比例尺。

Here, y2List is negative and this is the data set that I would like to create the second axis for. 在此, y2List为负,这是我要为其创建第二个轴的数据集。 I figured out how to do this using other questions on here. 我通过这里的其他问题弄清楚了如何做到这一点。

The problem is that when each data point is added, the second y axis ticks are shown again so that the second y axis is very crowded with numbers. 问题在于,当添加每个数据点时,将再次显示第二个y轴刻度,因此第二个y轴上挤满了数字。 I can get round this by setting a limit on the second y axis, which produces an image like this: 我可以通过在第二个y轴上设置一个限制来解决这个问题,该限制会产生如下图像:

在此处输入图片说明

The second y axis is slightly darker than the others, and this is because python is plotting the same numbers on top of the existing ones after each point is plotted (I can tell because the numbers get darker as each point is plotted) 第二个y轴比其他y轴稍暗,这是因为python在绘制每个点之后在现有的y轴上绘制相同的数字(我可以知道,因为绘制每个点时数字会变暗)

My question... is there a way to make the second y axis only plot the second scale only once? 我的问题...是否有办法使第二个y轴仅绘制第二个比例尺一次? This is obviously just to make the plot aesthetically pleasing but every little helps! 显然,这只是为了使情节具有美感,但一点点帮助!

My code is below: 我的代码如下:

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)

You should move the creation of the figure and the twin axes outside of your loop. 您应该将图形的创建和双轴移动到循环之外。 They only need to be done once. 他们只需要做一次。

Specifically, move fig1=plt.figure(1) , ax = fig1.add_subplot(111) and ax2 = ax.twinx() outside the loop. 具体来说,将fig1=plt.figure(1)ax = fig1.add_subplot(111)ax2 = ax.twinx()循环外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM