Matplotlib:如何在给定点添加 ytick 标签(或其他可以完成这项工作的标签)?
Matplotlib: how to add a ytick label (or other label that can do the job) at a given point?
我想显示 PCC 测试的结果。我有两个 y 轴分别显示 r 和 p 值。我在第二个轴上绘制了一条水平虚线以指示阈值 p=0.05
并且我想添加另一个 ytick 标签(或其他标签,只要它位于正确的位置,例如下图中的红色 0.05 ) 到第二个 y 轴,同时保持原始 ytick 标签。
根据 matplotlib 文档 Anatomy of a figure,我相信 Minor tick label
是我需要的,但我还没有找到任何关于如何实现我想要的教程。
这是我的代码:
import numpy as np
N = 8
ind = np.arange(N)
width = 0.35
pcc_r_data = feature_i_data[pcc_r]
pcc_p_data = feature_i_data[pcc_p]
fig, ax1 = plt.subplots()
ax1.set_xticks(ind + width / 2,
('GSE35570_N', 'GSE35570_P',
'GSE29265_N', 'GSE29265_P',
'GSE33630_N', 'GSE33630_P',
'GSE60542_N', 'GSE60542_P'),
rotation=-45)
ax1.bar(ind, pcc_r_data, width, label='r', color=sns.color_palette()[0])
ax1.set_ylim([0, 1])
ax1.set_ylabel('r', rotation="horizontal")
ax1.yaxis.set_label_coords(0, 1.03)
ax2 = ax1.twinx()
ax2.axhline(y=0.05, color='r', alpha=0.5, linestyle='--')
ax2.bar(ind + width, pcc_p_data, width, label='p-value', color=sns.color_palette()[1])
ax2.set_yscale("log")
ax2.set_ylim([0.00000001, 1])
ax2.set_ylabel('p-value', rotation="horizontal")
ax2.yaxis.set_label_coords(1.05, 1.08)
lines, labels = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax2.legend(lines + lines2, labels + labels2, loc='upper right', ncol=2)
fig.suptitle('Pearson correlation coefficient')
plt.show()
您可以在情节外添加文字。 y-axis transform 在“轴坐标”中使用 x-position,在“数据坐标”中使用 y-position。
import matplotlib.pyplot as plt
import numpy as np
N = 8
ind = np.arange(N)
width = 0.35
fig, ax1 = plt.subplots()
ax1.set_xticks(ind + width / 2)
ax1.set_xticklabels(('GSE35570_N', 'GSE35570_P', 'GSE29265_N', 'GSE29265_P', 'GSE33630_N',
'GSE33630_P', 'GSE60542_N', 'GSE60542_P'), rotation=-45)
ax1.bar(ind, np.random.rand(N) ** .2, width, label='r', color=sns.color_palette()[0])
ax1.set_ylim([0, 1])
ax1.set_ylabel('r', rotation="horizontal")
ax1.yaxis.set_label_coords(0, 1.03)
ax2 = ax1.twinx()
ax2.axhline(y=0.05, color='r', alpha=0.5, linestyle='--')
ax2.bar(ind + width, np.random.rand(N) ** 5, width, label='p-value', color=sns.color_palette()[1])
ax2.set_yscale("log")
ax2.set_ylim([0.00000001, 1])
ax2.set_ylabel('p-value', rotation="horizontal")
ax2.yaxis.set_label_coords(1.05, 1.08)
ax2.text(1, 0.05, ' 0.05', transform=ax2.get_yaxis_transform(), fontsize=18, color='r', ha='left', va='center')
lines, labels = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax2.legend(lines + lines2, labels + labels2, loc='upper right', ncol=2)
fig.suptitle('Pearson correlation coefficient')
plt.tight_layout()
plt.show()
或者,您可以设置一个带有标签的小刻度:
ax2.set_yticks([0.05], minor=True)
ax2.set_yticklabels([0.05], minor=True, color='r', fontsize=18)
我想显示 PCC 测试的结果。我有两个 y 轴分别显示 r 和 p 值。我在第二个轴上绘制了一条水平虚线以指示阈值 p=0.05
并且我想添加另一个 ytick 标签(或其他标签,只要它位于正确的位置,例如下图中的红色 0.05 ) 到第二个 y 轴,同时保持原始 ytick 标签。
根据 matplotlib 文档 Anatomy of a figure,我相信 Minor tick label
是我需要的,但我还没有找到任何关于如何实现我想要的教程。
这是我的代码:
import numpy as np
N = 8
ind = np.arange(N)
width = 0.35
pcc_r_data = feature_i_data[pcc_r]
pcc_p_data = feature_i_data[pcc_p]
fig, ax1 = plt.subplots()
ax1.set_xticks(ind + width / 2,
('GSE35570_N', 'GSE35570_P',
'GSE29265_N', 'GSE29265_P',
'GSE33630_N', 'GSE33630_P',
'GSE60542_N', 'GSE60542_P'),
rotation=-45)
ax1.bar(ind, pcc_r_data, width, label='r', color=sns.color_palette()[0])
ax1.set_ylim([0, 1])
ax1.set_ylabel('r', rotation="horizontal")
ax1.yaxis.set_label_coords(0, 1.03)
ax2 = ax1.twinx()
ax2.axhline(y=0.05, color='r', alpha=0.5, linestyle='--')
ax2.bar(ind + width, pcc_p_data, width, label='p-value', color=sns.color_palette()[1])
ax2.set_yscale("log")
ax2.set_ylim([0.00000001, 1])
ax2.set_ylabel('p-value', rotation="horizontal")
ax2.yaxis.set_label_coords(1.05, 1.08)
lines, labels = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax2.legend(lines + lines2, labels + labels2, loc='upper right', ncol=2)
fig.suptitle('Pearson correlation coefficient')
plt.show()
您可以在情节外添加文字。 y-axis transform 在“轴坐标”中使用 x-position,在“数据坐标”中使用 y-position。
import matplotlib.pyplot as plt
import numpy as np
N = 8
ind = np.arange(N)
width = 0.35
fig, ax1 = plt.subplots()
ax1.set_xticks(ind + width / 2)
ax1.set_xticklabels(('GSE35570_N', 'GSE35570_P', 'GSE29265_N', 'GSE29265_P', 'GSE33630_N',
'GSE33630_P', 'GSE60542_N', 'GSE60542_P'), rotation=-45)
ax1.bar(ind, np.random.rand(N) ** .2, width, label='r', color=sns.color_palette()[0])
ax1.set_ylim([0, 1])
ax1.set_ylabel('r', rotation="horizontal")
ax1.yaxis.set_label_coords(0, 1.03)
ax2 = ax1.twinx()
ax2.axhline(y=0.05, color='r', alpha=0.5, linestyle='--')
ax2.bar(ind + width, np.random.rand(N) ** 5, width, label='p-value', color=sns.color_palette()[1])
ax2.set_yscale("log")
ax2.set_ylim([0.00000001, 1])
ax2.set_ylabel('p-value', rotation="horizontal")
ax2.yaxis.set_label_coords(1.05, 1.08)
ax2.text(1, 0.05, ' 0.05', transform=ax2.get_yaxis_transform(), fontsize=18, color='r', ha='left', va='center')
lines, labels = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax2.legend(lines + lines2, labels + labels2, loc='upper right', ncol=2)
fig.suptitle('Pearson correlation coefficient')
plt.tight_layout()
plt.show()
或者,您可以设置一个带有标签的小刻度:
ax2.set_yticks([0.05], minor=True)
ax2.set_yticklabels([0.05], minor=True, color='r', fontsize=18)