gpt4 book ai didi

python - 如何在 matplotlib 中添加额外的 y 轴标签

转载 作者:行者123 更新时间:2023-12-01 07:26:12 28 4
gpt4 key购买 nike

我正在尝试在轴的右侧添加额外的轴标签,但不需要刻度线或刻度线标签。

import pandas as pd
import matplotlib.pyplot as plt

fig, axs = plt.subplots(2,2,sharey=True,sharex=True)
for i in range(2):
for j in range(2):
pd.Series(np.random.random(10)).plot(ax=axs[i,j])
axs[0,j].set_title('j = {}'.format(j))
ax[1,j].set_ylabel('x')
secaxy = axs[i,1].secondary_yaxis('right')
secaxy.set_ylabel('i = {}'.format(i))
secaxy.set_yticks([])
ax[i,0].set_ylabel('y')

出于某种原因,set_yticks([]) 不会替换刻度。

[In]: secaxy.get_yticks()
[Out:] array([-0.25, 0. , 0.25, 0.5 , 0.75, 1. , 1.25])

在图右侧获得不带刻度的附加轴标签的便捷方法是什么?

correct labels, but need to remove the ticks on the right

最佳答案

我不会为此滥用辅助轴。只需将标签放在右侧并使其可见即可。

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

fig, axs = plt.subplots(2,2,sharey=True,sharex=True)
for i in range(2):
for j in range(2):
pd.Series(np.random.random(10)).plot(ax=axs[i,j])
for j in range(2):
axs[0,j].set_title('j = {}'.format(j))
for i in range(2):
axs[i,1].set_ylabel('i = {}'.format(i))
axs[i,1].yaxis.get_label().set_visible(True)
axs[i,1].yaxis.set_label_position("right")
axs[i,0].set_ylabel('y')

plt.show()

enter image description here

关于python - 如何在 matplotlib 中添加额外的 y 轴标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57436986/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com