如何在我正在绘制的每个子图中添加垂直线?
How to add vertical lines on each subplot that I am plotting?
我正在尝试使用 .add_subplot()
绘制多个直方图
以下是我的部分代码:
for j in range(nlayer):
p_value_tensor_Wiki103_at_layer_j = p_value_tensor_Wiki103_at_layer[:,j].tolist()
hist_j = fig.add_subplot(grid[0,j], xticklabels=[], yticklabels=[])
hist_j.set_xlabel(labels_Wiki103[j],fontsize=3)
# histogram on the attached axes
hist_j.hist(p_value_tensor_Wiki103_at_layer_j, bins = 20)
但是如果我想在我生成的每个子图上的 x=0.05 处添加一条垂直线,我应该怎么做?
谢谢,
使用matplotlib.pyplot.vlines
or matplotlib.pyplot.axvline
,用法:
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(1)
mu, sigma = 0.5, 0.15
x = mu + sigma * np.random.randn(10000)
fig, ax = plt.subplots(1)
ax.hist(x, 50, density=1)
ax.vlines(0.5,0,3)
plt.show()
ax.hist(x, 50, density=1)
ax.axvline(0.5)
plt.show()
在您的用例中,您只需要做
for j in range(nlayer):
#...
hist_j.axvline(0.5)
# or to draw a line from ymin to ymax
hist_j.vlines(0.5, ymin, ymax)
我正在尝试使用 .add_subplot()
绘制多个直方图
以下是我的部分代码:
for j in range(nlayer):
p_value_tensor_Wiki103_at_layer_j = p_value_tensor_Wiki103_at_layer[:,j].tolist()
hist_j = fig.add_subplot(grid[0,j], xticklabels=[], yticklabels=[])
hist_j.set_xlabel(labels_Wiki103[j],fontsize=3)
# histogram on the attached axes
hist_j.hist(p_value_tensor_Wiki103_at_layer_j, bins = 20)
但是如果我想在我生成的每个子图上的 x=0.05 处添加一条垂直线,我应该怎么做?
谢谢,
使用matplotlib.pyplot.vlines
or matplotlib.pyplot.axvline
,用法:
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(1)
mu, sigma = 0.5, 0.15
x = mu + sigma * np.random.randn(10000)
fig, ax = plt.subplots(1)
ax.hist(x, 50, density=1)
ax.vlines(0.5,0,3)
plt.show()
ax.hist(x, 50, density=1)
ax.axvline(0.5)
plt.show()
在您的用例中,您只需要做
for j in range(nlayer):
#...
hist_j.axvline(0.5)
# or to draw a line from ymin to ymax
hist_j.vlines(0.5, ymin, ymax)