Search code examples
pythonmatplotlibplotline

Differentiating the line types in plotting in python


I'm having trouble choosing the line types for the figure below. It looks like some kind of intricate figure. Do you have any suggestions for determining the line types?

My script below is:

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax2.plot(months,bbprec070_vicoutput_monthly['OUT_PREC']['mean'],'bv-',alpha=0.5,markersize=marksize)
ax2.plot(months,bbprec080_vicoutput_monthly['OUT_PREC']['mean'],'b>-',alpha=0.5,markersize=marksize)
ax2.plot(months,bbprec090_vicoutput_monthly['OUT_PREC']['mean'],'b<-',alpha=0.5,markersize=marksize)
ax2.plot(months,bbb_vicoutput_monthly['OUT_PREC']['mean'],'ks-',alpha=0.5,markersize=12)
ax2.plot(months,bbprec110_vicoutput_monthly['OUT_PREC']['mean'],'b^-',alpha=0.5,markersize=marksize)
ax1.plot(months,bbprec070_vicoutput_monthly[' OUT_SWE']['mean'],'cv-',alpha=0.5,markersize=marksize)
ax1.plot(months,bbprec080_vicoutput_monthly[' OUT_SWE']['mean'],'c>-',alpha=0.5,markersize=marksize)
ax1.plot(months,bbprec090_vicoutput_monthly[' OUT_SWE']['mean'],'c<-',alpha=0.5,markersize=marksize)
ax1.plot(months,bbb_vicoutput_monthly[' OUT_SWE']['mean'],'kp-',alpha=0.5,markersize=12)
ax1.plot(months,bbprec110_vicoutput_monthly[' OUT_SWE']['mean'],'c^-',alpha=0.5,markersize=marksize)
ax2.plot(months,bbprec070_vicoutput_monthly[' OUT_EVAP']['mean'],'rv-',alpha=0.5,markersize=marksize)
ax2.plot(months,bbprec080_vicoutput_monthly[' OUT_EVAP']['mean'],'r>-',alpha=0.5,markersize=marksize)
ax2.plot(months,bbprec090_vicoutput_monthly[' OUT_EVAP']['mean'],'r<-',alpha=0.5,markersize=marksize)
ax2.plot(months,bbb_vicoutput_monthly[' OUT_EVAP']['mean'],'k*-',alpha=0.5,markersize=12)
ax2.plot(months,bbprec110_vicoutput_monthly[' OUT_EVAP']['mean'],'r^-',alpha=0.5,markersize=marksize)
ax2.plot(months,(bbprec070_vicoutput_monthly[' OUT_BASEFLOW']['mean']+bbprec070_vicoutput_monthly[' OUT_RUNOFF']['mean']),'gv-',alpha=0.5,markersize=marksize)
ax2.plot(months,(bbprec080_vicoutput_monthly[' OUT_BASEFLOW']['mean']+bbprec080_vicoutput_monthly[' OUT_RUNOFF']['mean']),'g>-',alpha=0.5,markersize=marksize)
ax2.plot(months,(bbprec090_vicoutput_monthly[' OUT_BASEFLOW']['mean']+bbprec090_vicoutput_monthly[' OUT_RUNOFF']['mean']),'g<-',alpha=0.5,markersize=marksize)
ax2.plot(months,(bbb_vicoutput_monthly[' OUT_BASEFLOW']['mean']+bbb_vicoutput_monthly[' OUT_RUNOFF']['mean']),'kP-',alpha=0.5,markersize=12)
ax2.plot(months,(bbprec110_vicoutput_monthly[' OUT_BASEFLOW']['mean']+bbprec110_vicoutput_monthly[' OUT_RUNOFF']['mean']),'g^-',alpha=0.5,markersize=marksize)
plt.title("Precipitation Perturbation")
#plt.xlabel("Month")
ax1.set_ylabel('Hydrologic States (mm)')
ax2.set_ylabel('Hydrologic Fluxes (mm/day)')
ax2.legend(["P(70%)","P(80%)","P(90%)","P(100%)","P(110%)",\
            "ET(70%)","ET(80%)","ET(90%)","ET(100%)","ET(110%)","Q(70%)","Q(80%)","Q(90%)","Q(100%)","Q(110%)"],loc='center left', bbox_to_anchor=(1.045, 0.5))
    ax1.legend(["SWE(70%)","SWE(80%)","SWE(90%)","SWE(100%)","SWE(110%)"],loc='center right', bbox_to_anchor=(-0.06, 0.5))
#plt.legend(["P","ET","Q"])
# Show the major grid lines with dark grey lines
plt.grid(b=True, which='major', color='#666666', linestyle='-',alpha=0.2)
# Show the minor grid lines with very faint and almost transparent grey lines
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
plt.show()

linetypes


Solution

  • You can use the linestyle parameter provided with the matplotlib.axes.Axes.plot function.

    The following example will use the standard built-in named formats, but you can craft your own custom linestyles.

    ax2.plot(months,bbprec070_vicoutput_monthly['OUT_PREC']['mean'],'bv-',alpha=0.5,markersize=marksize,linestyle='-')
    ax2.plot(months,bbprec080_vicoutput_monthly['OUT_PREC']['mean'],'b>-',alpha=0.5,markersize=marksize,linestyle='-')
    ax2.plot(months,bbprec090_vicoutput_monthly['OUT_PREC']['mean'],'b<-',alpha=0.5,markersize=marksize,linestyle='-')
    ax2.plot(months,bbb_vicoutput_monthly['OUT_PREC']['mean'],'ks-',alpha=0.5,markersize=12,linestyle='-')
    ax2.plot(months,bbprec110_vicoutput_monthly['OUT_PREC']['mean'],'b^-',alpha=0.5,markersize=marksize,linestyle='-')
    ax1.plot(months,bbprec070_vicoutput_monthly[' OUT_SWE']['mean'],'cv-',alpha=0.5,markersize=marksize,linestyle='--')
    ax1.plot(months,bbprec080_vicoutput_monthly[' OUT_SWE']['mean'],'c>-',alpha=0.5,markersize=marksize,linestyle='--')
    ax1.plot(months,bbprec090_vicoutput_monthly[' OUT_SWE']['mean'],'c<-',alpha=0.5,markersize=marksize,linestyle='--')
    ax1.plot(months,bbb_vicoutput_monthly[' OUT_SWE']['mean'],'kp-',alpha=0.5,markersize=12,linestyle='--')
    ax1.plot(months,bbprec110_vicoutput_monthly[' OUT_SWE']['mean'],'c^-',alpha=0.5,markersize=marksize,linestyle='--')
    ax2.plot(months,bbprec070_vicoutput_monthly[' OUT_EVAP']['mean'],'rv-',alpha=0.5,markersize=marksize,linestyle='-.')
    ax2.plot(months,bbprec080_vicoutput_monthly[' OUT_EVAP']['mean'],'r>-',alpha=0.5,markersize=marksize,linestyle='-.')
    ax2.plot(months,bbprec090_vicoutput_monthly[' OUT_EVAP']['mean'],'r<-',alpha=0.5,markersize=marksize,linestyle='-.')
    ax2.plot(months,bbb_vicoutput_monthly[' OUT_EVAP']['mean'],'k*-',alpha=0.5,markersize=12,linestyle='-.')
    ax2.plot(months,bbprec110_vicoutput_monthly[' OUT_EVAP']['mean'],'r^-',alpha=0.5,markersize=marksize,linestyle='-.')
    ax2.plot(months,(bbprec070_vicoutput_monthly[' OUT_BASEFLOW']['mean']+bbprec070_vicoutput_monthly[' OUT_RUNOFF']['mean']),'gv-',alpha=0.5,markersize=marksize,linestyle=':')
    ax2.plot(months,(bbprec080_vicoutput_monthly[' OUT_BASEFLOW']['mean']+bbprec080_vicoutput_monthly[' OUT_RUNOFF']['mean']),'g>-',alpha=0.5,markersize=marksize,linestyle=':')
    ax2.plot(months,(bbprec090_vicoutput_monthly[' OUT_BASEFLOW']['mean']+bbprec090_vicoutput_monthly[' OUT_RUNOFF']['mean']),'g<-',alpha=0.5,markersize=marksize,linestyle=':')
    ax2.plot(months,(bbb_vicoutput_monthly[' OUT_BASEFLOW']['mean']+bbb_vicoutput_monthly[' OUT_RUNOFF']['mean']),'kP-',alpha=0.5,markersize=12,linestyle=':')
    ax2.plot(months,(bbprec110_vicoutput_monthly[' OUT_BASEFLOW']['mean']+bbprec110_vicoutput_monthly[' OUT_RUNOFF']['mean']),'g^-',alpha=0.5,markersize=marksize,linestyle=':')