Matplotlib 在原始轴标签顶部从双轴绘制标签

问题描述 投票:0回答:1

我有一个问题,因为我使用左 y 轴绘制 7 个移动平均值(p1,数据帧)的图。在右侧 y 轴上,我绘制了 MA 的数据(pidx,也是数据帧)。作为时间序列,它们共享 x 轴。

我画了 2 个图,顶部和底部。下面的一个没问题,上面的一个给我带来了麻烦,因为 p1 的 7 个标签在左 y 轴(左上角)上正确书写。但是,右侧 y 轴的(单个)标签 pidx 直接绘制在左侧 y 轴的第一个标签上。

我对绘图还是新手,并且尝试了几种选择,例如将其放在左下角而不是顶部,但不断出现错误。我的代码可能很混乱,所以如果有人能建议在使用双轴时定义标签的“最简洁”方法,我将非常感激。谢谢。

这就是我正在做的事情:

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
pandas matplotlib plot axis-labels twinx
1个回答
0
投票

经过一番混乱之后,我设法得到了我想要的东西,右侧 y 轴标签整齐地位于左侧 y 轴标签列表的下方。

无法找到如何删除问题,所以我想我会发布答案,以防它对其他人有帮助。

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")
© www.soinside.com 2019 - 2024. All rights reserved.