python - 如何在我正在绘制的每个子图上添加垂直线?
问题描述
.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)
推荐阅读
- javascript - ASP.Net Core JS 适用于 _Layout,但不适用于正文页面
- machine-learning - 机器学习模型可以用于预测 TRNG 输出吗?
- python - 使用 Pytorch 数据加载器加载特定样本的简单方法
- hibernate - envers @AuditMappedBy() 应该做什么?
- c - (1.0e300 + pow(2.0, -30.0) > 1.0) 在 STDC 中究竟做了什么?
- python - PyCharm 未正确显示绘图
- stripe-payments - 在条带连接中,有没有办法安排在条带帐户中实际可用的收费金额时发生转移
- c# - 使用.Net核心的SQL数据库中的数据持久性问题
- laravel - Laravel 5.7.13 - FirstOrCreate/FirstOrNew 不存储值
- c# - C# SSL 流 - 系统重启后出现证书错误