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()

 

 

pythong 绘图 ParasiteAxes() 和 twinx() 对比_第1张图片

                                                  图1

 

pythong 绘图 ParasiteAxes() 和 twinx() 对比_第2张图片

图2

pythong 绘图 ParasiteAxes() 和 twinx() 对比_第3张图片

图3 

python 包的镜像安装

pip install -i "镜像目录 "  包名

你可能感兴趣的:(学习,经验)