Home > front end >  Is there a way to create a secondary y axis within a looped subplot figure using seaborn?
Is there a way to create a secondary y axis within a looped subplot figure using seaborn?

Time:01-19

I have written the code successfully to loop through data and create subplots however, I want two of the data sets to be displayed on the y axis. Those are y and y1 or ax2. This code creates a secondary y axis however, it does not label the secondary y axis nor is their any data associated with it.

nrow = 4
ncol = 3
fig, axes = plt.subplots(nrow, ncol, figsize=(17,17))
plt.subplots_adjust(bottom=.1, right=1.5,top=1.75)
fig.delaxes(axes[3,2])

count = 0
for r in range(nrow):
  for c in range(ncol):

    x = dataobserved[count].index
    x1 = datamodeled[count].index
    y = dataobserved[count]['Average Q cfs']
    y1 = datamodeled[count]['QHistorical']
    y2 = dataobserved[count]['Total Generation Mwh']
    y3 = datamodeled[count]['Historical']

    sns.set_style("white")
    ax2 = ax.twinx()

    ax2 = sns.lineplot(x = x, y = y, color = "black", linestyle = '--', ax = axes[r,c])    
    ax2 = sns.lineplot(x = x1, y = y1, color = "blue", linestyle = '--', ax = axes[r,c]) 
    ax = sns.lineplot(x = x, y = y2, color = "black", ax = axes[r,c]) 
    ax = sns.lineplot(x = x1, y = y3, color = "blue", ax = axes[r,c]) 


    subplot_title = (str(ttl[count]))
    axes[r,c].set_title(subplot_title, size = 20)
    axes[r,c].legend(labels = ['Observed Outflow','Historical Baseline Outflow','Observed Generation','Historical Baseline Generation'], loc = "upper left")

    count =1
  for ax in axes.flat:
    ax.set_xlabel('Water Year Week', fontsize = 15)
    ax.set_ylabel( 'Weekly Total Generation (Mwh)', fontsize = 15)
    ax2.set_ylabel('Weekly Average Outflow (cfs)')
    ax.set_xticks(range(1,52,10))

enter image description here

  •  Tags:  
  • Related