4

I am trying to plot multiple number of histograms by using .add_subplot() Below is a part of my code:

    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)  

But if I want to add a vertical lines at x= 0.05 on EACH of the subplot that I generated, what should I do?

Thank you,

1

1 Answer 1

4

Use matplotlib.pyplot.vlines or matplotlib.pyplot.axvline, usage:

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

In your use case you would simply need to do

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)

Not the answer you're looking for? Browse other questions tagged or ask your own question.