two (or more) graphs in one plot with different x-axis AND y-axis scales in python

前端 未结 3 2009
刺人心
刺人心 2020-12-13 21:23

I want 3 graphs on one axes object, for example:

#example x- and y-data
x_values1=[1,2,3,4,5]
y_values1=[1,2,3,4,5]

x_values2=[-1000,-800,-600,-400,-200]
y_         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-13 22:07

    The idea would be to create three subplots at the same position. In order to make sure, they will be recognized as different plots, their properties need to differ - and the easiest way to achieve this is simply to provide a different label, ax=fig.add_subplot(111, label="1").

    The rest is simply adjusting all the axes parameters, such that the resulting plot looks appealing. It's a little bit of work to set all the parameters, but the following should do what you need.

    import matplotlib.pyplot as plt
    
    x_values1=[1,2,3,4,5]
    y_values1=[1,2,2,4,1]
    
    x_values2=[-1000,-800,-600,-400,-200]
    y_values2=[10,20,39,40,50]
    
    x_values3=[150,200,250,300,350]
    y_values3=[10,20,30,40,50]
    
    
    fig=plt.figure()
    ax=fig.add_subplot(111, label="1")
    ax2=fig.add_subplot(111, label="2", frame_on=False)
    ax3=fig.add_subplot(111, label="3", frame_on=False)
    
    ax.plot(x_values1, y_values1, color="C0")
    ax.set_xlabel("x label 1", color="C0")
    ax.set_ylabel("y label 1", color="C0")
    ax.tick_params(axis='x', colors="C0")
    ax.tick_params(axis='y', colors="C0")
    
    ax2.scatter(x_values2, y_values2, color="C1")
    ax2.xaxis.tick_top()
    ax2.yaxis.tick_right()
    ax2.set_xlabel('x label 2', color="C1") 
    ax2.set_ylabel('y label 2', color="C1")       
    ax2.xaxis.set_label_position('top') 
    ax2.yaxis.set_label_position('right') 
    ax2.tick_params(axis='x', colors="C1")
    ax2.tick_params(axis='y', colors="C1")
    
    ax3.plot(x_values3, y_values3, color="C3")
    ax3.set_xticks([])
    ax3.set_yticks([])
    
    plt.show()
    

提交回复
热议问题