0

I have a code that plots two charts side by side. I want to additionally plot a bar chart on one of the two plots, with another quantity. My code indicates the hashed lines which would additionally plot a bar chart. But I want to plot this bar chart on a twin axis (its y label and limits on the right of the plot). Currently, the twinx() command does not work on a 1 by 2 plot and gives an error. My code is below

def result_variability_onerow(variable1,variable2, yr):
    scenarios_PSN = {'Low Snow': 3, 'Medium Snow': 15, 'High Snow': 46}
    scenarios_TSN = {'Low Snow': 46, 'Medium Snow': 25, 'High Snow': 3}
    date_form = DateFormatter("%m-%y")

  
    plt.close()
    fig, ax = plt.subplots(1 , 2,  figsize = [15,5])
    # ax2 = ax.twinx()  
    # ax2.set_ylim(4, 20)
    for key, value in scenarios_PSN.items():
        p = snow_vary[str(yr) + '_' + str(value)][150:250]
        ax[0].plot(p[variable1], label= str(key))
        ax[0].set_ylabel(str(variable1))
        ax[0].xaxis.set_major_formatter(date_form)
        ax[0].grid()
        ax[0].legend()
        
    for key, value in scenarios_TSN.items():
        t = temp_vary[str(yr) + '_' + str(value)][150:250]
        ax[1].plot(t[variable1], label= str(key))
        ax[1].xaxis.set_major_formatter(date_form)
        ax[1].grid()
 
    # ax[1].bar(t.index, t[variable2], label= "Precipitation")
    ax[0].set_title(variable1 + "Phase Change")
    ax[1].set_title(variable1 + "Temperature Change")
    ax[0].set_ylim(0,180)
    ax[1].set_ylim(0,180)
    ax[0].set_ylabel("Streamflow (mm)")
    plt.savefig('behaviour.pdf', format = 'pdf', bbox_inches = 'tight')

    print(str(variable1) + ' for the year ' + str(yr))

result_variability_onerow('streamflow','precip', 2005)
StayOnTarget
  • 11,743
  • 10
  • 52
  • 81

0 Answers0