Reputation: 147
I have a problem in that Im drawing a plot of 7 moving averages (p1, dataframe) using the left y-axis. On the right y-axis Im drawing the data the MAs are made from (pidx, also dataframe). Being time-series, they share the x-axis.
Im drawing 2 plots, top and bottom. The bottom one is ok, the top one is giving me trouble in that the 7 labels of p1 are written correctly for the left y-axis, top left. However, the (single) label of the right y-axis, pidx, is drawn directly over the first label of the left y-axis.
Im still new to plotting and have tried several options, such as putting it bottom left instead of top, but keep gettng errors. My code might be a mess, so if someone could suggest the "neatest" way to define labels when using twin axes Id be really grateful. Thanks.
Heres what Im working with:
p1 = above_below_ma_df.tail(lookback)
p2 = t2108.tail(lookback)
# p4 = above_below_ma_df['$>MA40'].tail(lookback)
pidx = idx.tail(lookback)
# Create subplots
fig, axs = plt.subplots(nrows=2, ncols=1, figsize=(15, 10))
# Plot MAs on top subplot
#################################################################################
axs[0].plot(p1.index, p1, linewidth=1)
axs[0].set_ylabel('% stocks above MAs')
axs[0].set_xlabel('Date')
axs[0].set_title(f"{code} - No. stocks above/below MAs")
axs[0].legend(labels=p1.columns, loc='upper left') # Use column names for legend labels
# Add index to other axis of top subplot
axs0_twin = axs[0].twinx()
axs0_twin.plot(p1.index, pidx, 'black', linewidth=2, label=code)
# Set y-axis label at the lower left corner
#axs0_twin.ylabel(code, loc='bottom left')
#axs0_twin.yaxis.set_label_coords(-0.1, 0.5) # Adjust the coordinates as needed
#axs0_twin.set_ylabel(code, color='black')
# axs0_twin.set_ylabel(code, color='black')
# axs0_twin.legend(labels=code, loc='lower left',color='black')
# Plot T2108 vs Close on lower subplot
#################################################################################
axs[1].plot(p2.index, p2, color='b', alpha=0.7, label='T2108')
axs[1].set_xlabel('Date')
axs[1].set_ylabel('T2108-(%>40MA)', color='b')
# Add a horizontal dotted line to show where fundos might be
axs[1].axhline(y=15, color='b', linestyle='--')
axs[1].axhline(y=85, color='b', linestyle='--')
# Add index to other axis
axs1_twin = axs[1].twinx()
axs1_twin.plot(p2.index, pidx, 'black', label=code)
axs1_twin.set_ylabel(code, color='black')
# Combine legends for both subplots
lines0, labels0 = axs[0].get_legend_handles_labels()
lines0_twin, labels0_twin = axs0_twin.get_legend_handles_labels()
axs0_twin.legend(lines0 + lines0_twin, labels0 + labels0_twin, loc='upper left')
lines1, labels1 = axs[1].get_legend_handles_labels()
lines1_twin, labels1_twin = axs1_twin.get_legend_handles_labels()
axs1_twin.legend(lines1 + lines1_twin, labels1 + labels1_twin, loc='upper left')
plt.show()
return above_below_ma_df, t2108
Upvotes: 0
Views: 312
Reputation: 147
So after some messing I managed to get what I wanted, the right y-axis lable neatly below the list of left y-axis labels.
Couldnt find out how to delete the question, so I thought Id post the answer in case it helps anyone else.
p1 = above_below_ma_df.tail(lookback) # df with 7 moving average columns
pidx = idx.tail(lookback) # df with column of original data
# Create subplots
fig, axs = plt.subplots(nrows=2, ncols=1, figsize=(15, 10))
# Plot MAs on top subplot # Lower subplot not shown here
# p1 columns on left y-axis
axs[0].plot(p1.index, p1, linewidth=1, label=p1.columns)
# pidx column on twin y-axis on right
axs0_twin = axs[0].twinx()
axs0_twin.plot(p1.index, pidx, linewidth=2, color='black', label=code)
# Adding the x-axis with dates
axs[0].set_xlabel('Date')
# Adding labels for both y-axes
axs[0].set_ylabel('% stocks above MAs')
axs0_twin.set_ylabel(code, color='black')
# Combine legends for both subplots
lines0, labels0 = axs[0].get_legend_handles_labels()
lines0_twin, labels0_twin = axs0_twin.get_legend_handles_labels()
axs0_twin.legend(lines0 + lines0_twin, labels0 + labels0_twin, loc='upper left')
# Add title for plot
axs[0].set_title(f"{code} - No. stocks above/below MAs")
Upvotes: 0