效果:
代码:
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 10 17:40:48 2020
@author: abc2xyz
"""
from mpl_toolkits.axisartist.parasite_axes import HostAxes, ParasiteAxes
import matplotlib.pyplot as plt
def MultiPlot(figName='1',
figTitle='title',
figSize=[0.05, 0.05, 0.7, 0.9],
xData=[1,2,3],
yData=[[1,2,3],[1,30,4],[10,4,6],[1,6,17]],
labelX=None,
labelY=None,
limX=None,
limY=None,
color=None,
legend=False):
# figName : str
# figTitle: str
# figSize : fig size : 0 < list <1
# xData= list or np.array
# yData= list[ list or np.array ]
# labelX: str
# labelY: list [ str ] ex. ['y0','y1','y2','y3']
# limX : tuple (min,max) ex. limX=(0,4)
# limY : list [tuple (min,max) ] ex. [(0,10),(0,20),(0,30),(0,40)]
#color: list ['r','m','b','g'] ex. ['r','m','b','g']
# legend: True / False
fig = plt.figure(figName)
ax = HostAxes(fig, [0.1, 0.1, 0.6, 0.85])
ax.set_title(figTitle)
numY=len(yData)
if labelX==None:
labelX='x'
if labelY==None:
labelY=['y%d'%i for i in range(numY)]
else:
assert(len(labelY)==numY),'The number of labelY should equal to number of yData'
colorList=['k','r','b','g','c','m','y','lime','peru']
if color==None:
color=colorList[:numY]
else:
assert(len(color)==numY),'The number of color should equal to number of yData'
for iY in range(numY-1):
iAx=ParasiteAxes(ax, sharex=ax)
ax.parasites.append(iAx)
ax.axis['right'].set_visible(False)
ax.axis['top'].set_visible(False)
if numY>1:
ax.parasites[0].axis['right'].set_visible(True)
ax.parasites[0].axis['right'].major_ticklabels.set_visible(True)
ax.parasites[0].axis['right'].label.set_visible(True)
ax.set_xlabel(labelX)
ax.set_ylabel(labelY[0])
for iY in range(numY-1):
ax.parasites[iY].set_ylabel(labelY[iY+1])
axisline=[None]
for iY in range(1,numY-1):
axisline.append(ax.parasites[iY].get_grid_helper().new_fixed_axis)
for iY in range(1,numY-1):
ax.parasites[iY].axis['right%d'%(iY+1)] = axisline[iY](loc='right', axes=ax.parasites[iY], offset=(40*iY,0))
fig.add_axes(ax)
curveAx,=ax.plot(xData,yData[0],label=labelY[0],color=color[0])
curves=[]
for iY in range(numY-1):
curve_,=ax.parasites[iY].plot(xData,yData[iY+1],label=labelY[iY+1],color=color[iY+1])
curves.append(curve_)
if not limX is None:
ax.set_xlim(limX)
if not limY is None:
ax.set_ylim(limY[0])
for iY in range(numY-1):
ax.parasites[iY].set_ylim(limY[iY+1])
if legend:
ax.legend()
for iY in range(numY-1):
if iY==0:
ax.axis['left'].label.set_color(color[0])
ax.axis['left'].major_ticks.set_color(color[0])
ax.axis['left'].major_ticklabels.set_color(color[0])
ax.axis['left'].line.set_color(color[0])
ax.parasites[iY].axis['right'].label.set_color(color[1])
ax.parasites[iY].axis['right'].major_ticks.set_color(color[1])
ax.parasites[iY].axis['right'].major_ticklabels.set_color(color[1])
ax.parasites[iY].axis['right'].line.set_color(color[1])
else:
ax.parasites[iY].axis['right%d'%(iY+1)].label.set_color(color[iY+1])
ax.parasites[iY].axis['right%d'%(iY+1)].major_ticks.set_color(color[iY+1])
ax.parasites[iY].axis['right%d'%(iY+1)].major_ticklabels.set_color(color[iY+1])
ax.parasites[iY].axis['right%d'%(iY+1)].line.set_color(color[iY+1])
return fig
if __name__=='__main__':
import numpy as np
x=np.arange(0,15,0.02)
y1=np.sin(x)
y2=np.cos(x)
y3=np.tan(x)
y4=x
y5=np.arctan(x)
y6=np.arccos(x)
y7=np.arcsin(x)
xData=x
yData=[y1,y2,y3,y4,y5,y6,y7]
fig=MultiPlot(xData=xData,yData=yData)
fig.savefig('1.png')
plt.show()
附:
matplotlib常用颜色汇总: