有两个Y轴的图:图例未显示所有曲线

时间:2020-10-13 15:24:57

标签: python matplotlib

考虑以下代码:

import constants
import numpy as np
import time 
from numpy import *
from shutil import copyfile
from matplotlib.pyplot import *
import matplotlib as mpl
mpl.rcParams['text.usetex'] = True
mpl.rcParams['text.latex.preamble'] = [r'\usepackage{amsmath}']
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import matplotlib.cbook as cbook
import matplotlib.gridspec as gridspec

from shutil import copyfile
import pickle
import itertools
#%% Parameters for the plots.
plt.rcParams['lines.linewidth'] = 3
plt.rcParams.update({'font.size': 60})
plt.rc('axes', labelsize=80)
plt.rc('xtick', labelsize=80) 
plt.rc('ytick', labelsize=60) 
rcParams["savefig.jpeg_quality"] = 40 
dpi_value=100

def plot_fct_param_2yticks(param,values_ax1,values_ax2,valueType,curveLabel_ax1,curveLabel_ax2,title,xLabel,yLabel_ax1,yLabel_ax2,xlog,ylog,posLegend,curveColors_ax1=None,curveColors_ax2=None,yticks_ax1=None,ytickx_ax2=None):
    fig,ax1=plt.subplots(figsize=(10*4, 5.4*4))
    ax1.set_xlabel(xLabel)
    ax1.set_ylabel(yLabel_ax1)
    marker = itertools.cycle(('P', '*', '.', 'v', '^'))
    if(curveColors_ax1==None):
        for ii in range(len(values_ax1)):
            ax1.plot(param,values_ax1[ii],'.', label=curveLabel_ax1[ii],markersize=60, marker=next(marker))
    else:
        for ii in range(len(values_ax1)):
            ax1.plot(param,values_ax1[ii],'.', label=curveLabel_ax1[ii],markersize=60, marker=next(marker), color=curveColors_ax1[ii])
    ax2=ax1.twinx()
    # make a plot with different y-axis using second axis object
    if(curveColors_ax2==None):
        for ii in range(len(values_ax2)):
            ax2.plot(param,values_ax2[ii],'.', label=curveLabel_ax2[ii],markersize=60, marker=next(marker))
    else:
        for ii in range(len(values_ax2)):
            ax2.plot(param,values_ax2[ii],'.', label=curveLabel_ax2[ii],markersize=60, marker=next(marker), color=curveColors_ax2[ii])

    if xlog==True:
        ax1.set_xscale('log')
        ax2.set_xscale('log')
    if ylog==True:
        ax1.set_yscale('log')
        ax2.set_yscale('log')
                
    plt.legend(loc=posLegend)
    figManager = plt.get_current_fig_manager()
    figManager.window.showMaximized()
    plt.show() 
    
length_example=20
param=list(range(length_example))
values_ax1=[[0.2]*length_example,[0.2]*length_example]
values_ax2=[[0.1]*length_example,[0.1]*length_example]
valueType="test"
curveLabel_ax1=["$A$","$B$"]
curveLabel_ax2=["$C$","$D$"]
title="blabla"
xLabel="$E$"
yLabel_ax1="$F$"
yLabel_ax2="$G$"
xlog=True
ylog=True
posLegend="upper left"
curveColors_ax1=["blue","red"]
curveColors_ax2=["blue","red"]
plot_fct_param_2yticks(param,values_ax1,values_ax2,valueType,curveLabel_ax1,curveLabel_ax2,title,xLabel,yLabel_ax1,yLabel_ax2,xlog,ylog,posLegend,curveColors_ax1,curveColors_ax2)

因此,这里的目标是绘制4条曲线(两个曲线的第一个具有给定的y轴,最后两个曲线的另一个具有y轴)。当我运行此脚本时,只有与最后两条曲线相关的图例。为什么其他图例没有出现??

事实上,在下面的图片中,只有与曲线C和D相关的图例出现...

enter image description here

0 个答案:

没有答案
相关问题