如何在我正在绘制的每个子地块上添加垂直线?

2024-09-30 01:23:56 发布

您现在位置: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的垂直线,我应该怎么做

谢谢,


Tags: 代码inaddlayerforvalue绘制range
1条回答
网友
1楼 · 发布于 2024-09-30 01:23:56

使用^{}^{},用法:

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()

enter image description here

ax.hist(x, 50, density=1)
ax.axvline(0.5)
plt.show()

enter image description here

在您的用例中,您只需要

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)

相关问题 更多 >

    热门问题