pythong 繪圖 ParasiteAxes() 和 twinx() 對比

matplotlib 繪製曲線的方法 ParasiteAxes() 和 twinx() 對比

調整90行 和91 行的註釋,可以看到圖1 ,圖 2 中的效果,取消47行註釋 看到圖 3

# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import host_subplot
from mpl_toolkits.axisartist.parasite_axes import ParasiteAxes, HostAxes
import numpy as np
import time


# 繪製數據
class DrawArray():
    """
    繪製調試數據
    """

    def __init__(self, Name):
        self.fig = plt.figure()
        self.fig.suptitle(Name)
        self.ax = HostAxes(self.fig, [0.08, 0.08, 0.8, 0.8])
        self.ax.axis["right"].set_visible(False)
        self.ax.axis["top"].set_visible(False)
        self.ax.legend()
        self.fig.add_axes(self.ax)
        self.color=0
        self.subAx = {}
        pass
    def AddAxis(self, data,datay, Name):
        self.subAx[Name] = [data, datay]
        pass

    def Show(self):
        i = 1
        for a in self.subAx:
            print(a)
            cl = "C%d" % i
            datax, datay = self.subAx[a]
            ax_Versine = ParasiteAxes(self.ax, sharex=self.ax)
            self.ax.parasites.append(ax_Versine)
            ax_Versine.axis['right'].set_visible(False)
            #ax_Versine.axis[a].set_visible(False)
            ax_Versineline = ax_Versine.get_grid_helper().new_fixed_axis
            ax_Versine.set_ylabel(a,color=cl)
            ax_Versine.axis['right'] = ax_Versineline(loc='left', axes=ax_Versine,  offset=(40*i, 0))
            ax_Versine.axis['right'].major_ticks.set_color(cl)
            ax_Versine.axis['right'].major_ticklabels.set_color(cl)
            ax_Versine.axis['right'].line.set_color(cl)
            ax_Versine.plot(datax, datay, color=cl, label=a)
            #ax_Versine. set_ylim(0,40) # 設置Y 比列
            #s,h=ax_Versine.get_ylim() # 獲取 Y比列 
            i+=1
        plt.legend(loc=0) 
        
        plt.show()


class DrawWArray():
    """
    繪製調試數據
    """
    def __init__(self, Name):
        self.fig = plt.figure()
        self.fig.suptitle(Name)
        self.ax = plt.subplot()
        #self.ax= (self.fig,[0.08,0.08,0.7,0.8])
        plt.subplots_adjust(right=0.7)
         
        self.subAx = {}
        
        pass

    def AddAxis(self, x,y, Name):
        self.subAx[Name] = [x, y]
        pass

    def Show(self):
        i = 1
        for a in self.subAx:
            print(a)
            cl = "C%d" % i
            datax, datay = self.subAx[a]
            x=plt.twinx()
            x.plot(datax, datay, color=cl, label=a)
            x.set_ylabel(a)
            # x.grid()
            #plt.legend(loc=0)
            i += 1
        self.fig.tight_layout()
        plt.legend(loc=0)
        plt.show()

#d = DrawWArray("TestW")
d = DrawArray("Test")
datax = []
datay = []
dataz = []
datam = []
for i in range(36):
    datax.append(i)
    datay.append(i*i)
    dataz.append(np.sin(i/6))
    datam.append(i*i*i)
d.AddAxis(datax, datay, "AAA")
d.AddAxis(datax, dataz, "BBB")
d.AddAxis(datax, datax, "CCC")
d.AddAxis(datax, datam, "DDDD")
d.Show()

 

 

                                                  圖1

 

圖2

圖3 

python 包的鏡像安裝

pip install -i "鏡像目錄 "  包名

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章