Vertical Bar Chart in Python and Matplotlib

How to create a vertical bar chart. Is there any option for this or should it be built from scratch? I want the top chart to look like the bottom, but along the vertical axis!

from matplotlib import pyplot as plt import numpy as np sample=np.random.normal(size=10000) vert_hist=np.histogram(sample,bins=30) ax1=plt.subplot(2,1,1) ax1.plot(vert_hist[0],vert_hist[1][:-1],'*g') ax2=plt.subplot(2,1,2) ax2.hist(sample,bins=30) plt.show() 

enter image description here

+10
python numpy matplotlib plot histogram
source share
3 answers

Use orientation="horizontal" in ax.hist :

 from matplotlib import pyplot as plt import numpy as np sample = np.random.normal(size=10000) vert_hist = np.histogram(sample, bins=30) ax1 = plt.subplot(2, 1, 1) ax1.plot(vert_hist[0], vert_hist[1][:-1], '*g') ax2 = plt.subplot(2, 1, 2) ax2.hist(sample, bins=30, orientation="horizontal"); plt.show() 

enter image description here

+21
source share

Just use barh() to plot:

 import math from matplotlib import pyplot as plt import numpy as np sample=np.random.normal(size=10000) vert_hist=np.histogram(sample,bins=30) # Compute height of plot. height = math.ceil(max(vert_hist[1])) - math.floor(min(vert_hist[1])) # Compute height of each horizontal bar. height = height/len(vert_hist[0]) ax1=plt.subplot(2,1,1) ax1.barh(vert_hist[1][:-1],vert_hist[0], height=height) ax2=plt.subplot(2,1,2) ax2.hist(sample,bins=30) plt.show() 

enter image description here

+5
source share

I used the ax [1] as a subplot

f ,ax = plt.subplots(1,2,figsize = (30, 13),gridspec_kw={'width_ratios': [5, 1]})

The following code rotates the histogram 90 degrees clockwise. he also builds a fitting curve for silos. The key argument is orientation = u'horizontal

 ax[1].hist(data,normed =1, bins = num_bin, color = 'yellow' ,alpha = 1, orientation=u'horizontal') # Fit a normal distribution to the data: mu, std = norm.fit(data) # Plot the PDF. xmin = min(data) xmax = max(data) x = np.linspace(xmin, xmax, 10000) p = norm.pdf(x, mu, std) base = plt.gca().transData rot = transforms.Affine2D().rotate_deg(-90) # rotate the histogram line 90 degress clockwise ax[1].plot(-x, p, 'r',linewidth=2, transform = rot+base) # use -x for the reverse y-axis plotting 
0
source share

All Articles