如何在python中移动辅助x和y轴的刻度和标签位置

时间:2013-09-17 15:09:22

标签: python python-3.x matplotlib

我想在python中使用matplotlib绘制多个轴。我使用twinx()。twin(y)完成了它但是我无法使用新轴的刺来移动刻度位置和轴标签。请帮助我解决这个问题。

由于

import matplotlib.pyplot as plt
import numpy as np
def make_patch_spines_invisible(ax):
    ax.set_frame_on(True)
    ax.patch.set_visible(False)
    for sp in ax.spines.itervalues():
        sp.set_visible(False)
def MultiAxes(*xy):
        fig, host=plt.subplots()
        fig.subplots_adjust(right=0.75)

        new=xy[0]

        x=new[0]
        y=new[1]
        x1=new[2]
        y1=new[3]
        x2=new[4]
        y2=new[5]

        new_Axes=host.twinx().twiny()

        p1,= host.plot(x,y,"b-",label="X0-Y0 plot")

        p2,= host.plot(x1,y1,"r-",label="X1-Y1 Plot")

        host.set_xlim(0,20)
        host.set_ylim(0,30)

        host.set_xlabel("X0 Axis")
        host.set_ylabel("Y0 Axis")

        make_patch_spines_invisible(new_Axes)

        new_Axes.spines['left'].set_position(('data', 10))
        new_Axes.yaxis.set_ticks_position('left')
        new_Axes.spines['left'].set_visible(True)

        new_Axes.spines['bottom'].set_position(('data', 10))
        new_Axes.xaxis.set_ticks_position('bottom')
        new_Axes.spines['bottom'].set_visible(True)

        new_Axes.set_xlim(0,30)
        new_Axes.set_ylim(0,40)
        new_Axes.set_xlabel("X1 Axis")
        new_Axes.set_ylabel("Y1 Axis")

        p3,= new_Axes.plot(x2,y2,"g--",label="X2-Y2 Plot")

        lines = [p1, p2, p3]

        host.legend(lines, [l.get_label() for l in lines],loc="upper left")


        plt.show()

x=[2,4,6,8,9,10,12,15]
y=[0,3,5,6,8,9,10,15]
x1=[0,3,5,7,9,13,15,17]
y1=[1,4,5,8,11,15,17,19]
x2=[0,2,4,5,17,18,19,30]
y2=[0,2,4,5,17,18,19,30]
val=[x,y,x1,y1,x2,y2]
MultiAxes(val)

0 个答案:

没有答案