Python Matplotlib:如何从列表中打印数据,添加两个y轴?

Python Matplotlib:如何从列表中打印数据,添加两个y轴?,python,numpy,matplotlib,plot,multiple-axes,Python,Numpy,Matplotlib,Plot,Multiple Axes,我需要使用matplotlib生成一些绘图,但我的绘图能力非常差。我有五个列表,每个列表包含100个值。它们的值根据以下情况而变化: 我希望能够从中生成两个线条和标记图表: 第一个绘图涉及列表1、2、3和4,并且具有两个y轴。列表1、2和3依赖于常规y轴,而列表4依赖于添加的y轴,如下所示: 第二个必须仅绘制列表4和5,但使用常规y轴 在继续之前,是否需要将每个列表转换为numpy数组?无论如何,我没有弄清楚如何使用matplotlib进行打印。任何帮助都将不胜感激。谢谢 只需制作一个轴,即

我需要使用
matplotlib
生成一些绘图,但我的绘图能力非常差。我有五个
列表,每个列表包含
100个
值。它们的值根据以下情况而变化:

我希望能够从中生成两个
线条和标记
图表:

  • 第一个绘图涉及列表1、2、3和4,并且具有两个y轴。列表1、2和3依赖于常规y轴,而列表4依赖于添加的y轴,如下所示:
  • 第二个必须仅绘制列表4和5,但使用常规y轴
  • 在继续之前,是否需要将每个
    列表
    转换为
    numpy数组
    ?无论如何,我没有弄清楚如何使用
    matplotlib
    进行打印。任何帮助都将不胜感激。谢谢

    只需制作一个轴,即可在单独的y轴上绘制
    列表4
    。你可以看到一个例子

    这里有一个简短的脚本来做你想做的事。在这种情况下,无需转换为
    numpy
    数组

    import matplotlib.pyplot as plt
    
    # Some sample lists
    l1 = [0.7,0.8,0.8,0.9,0.8,0.7,0.6,0.9,1.0,0.9]
    l2 = [0.2,0.3,0.1,0.0,0.2,0.1,0.3,0.1,0.2,0.1]
    l3 = [0.4,0.6,0.4,0.5,0.4,0.5,0.6,0.4,0.5,0.4]
    
    l4 = [78,87,77,65,89,98,74,94,85,73]
    l5 = [16,44,14,55,34,36,76,54,43,32]
    
    # Make a figure
    fig = plt.figure()
    
    # Make room for legend at bottom
    fig.subplots_adjust(bottom=0.2)
    
    # The axes for your lists 1-3
    ax1 = fig.add_subplot(211)
    # A twin axis for list 4. This shares the x axis, but has a separate y axis
    ax2 = ax1.twinx()
    
    # Plot lines 1-3
    line1 = ax1.plot(l1,'bo-',label='list 1')
    line2 = ax1.plot(l2,'go-',label='list 2')
    line3 = ax1.plot(l3,'ro-',label='list 3')
    
    # Plot line 4
    line4 = ax2.plot(l4,'yo-',label='list 4')
    
    # Some sensible y limits
    ax1.set_ylim(0,1)
    ax2.set_ylim(0,100)
    
    # Your second subplot, for lists 4&5
    ax3 = fig.add_subplot(212)
    
    # Plot lines 4&5
    ax3.plot(l4,'yo-',label='list 4')
    line5 = ax3.plot(l5,'mo-',label='list 5')
    
    # To get lines 1-5 on the same legend, we need to 
    # gather all the lines together before calling legend
    lines = line1+line2+line3+line4+line5
    labels = [l.get_label() for l in lines]
    
    # giving loc a tuple in axes-coords. ncol=5 for 5 columns
    ax3.legend(lines, labels, loc=(0,-0.4), ncol=5)
    
    ax3.set_xlabel('events')
    
    # Display the figure
    plt.show()
    

    太好了!如果我想将图例移动到x轴标签下,我称之为
    事件
    ,那该怎么办?只需在中编辑它。简而言之:在图例调用中添加
    loc=(0,-0.3),ncol=5
    。我也做了修改,使所有5行出现在同一个传奇非常有用!但我不需要它们出现在同一个图例上:两个图例可以,因为它们需要区分。但你可以保持原样。谢谢
    ax1.set_ylabel('Left hand label,top sublot')
    ax2.set_ylabel('Right hand label,top sublot')
    ax3.set_ylabel('Left hand label,bottom sublot')
    设置范围(1,len(list1)+1)
    ,然后打印时:
    line1=ax1.plot(x,l1,…)
    ,等等