0

How can I manage the tick for this horizontal histogram?

My code:

d1=pd.read_csv('Gold1.Dat')
d1.dropna(subset=['WF (mV)'],how='any', inplace=True)

binwidth=5
m_n=np.floor(min(d1['WF (mV)'])/binwidth)*binwidth
m_x=np.floor(max(d1['WF (mV)'])/binwidth)*binwidth+binwidth            
print(m_n)
print(m_x)
print(m_x-m_n)
binnum=int((m_x-m_n)/binwidth)
print(binnum)
a,b,c=plt.hist(d1['WF (mV)'],bins=binnum,range=(m_n,m_x),edgecolor='k',orientation='horizontal')

plt.xticks(b,rotation=45)

plt.tight_layout()
plt.savefig('vertical histogram')

Image of this graph

JohanC
  • 71,591
  • 8
  • 33
  • 66
Prabesh Bista
  • 61
  • 1
  • 3
  • `plt.yticks(b)` instead of `plt.xticks(b,rotation=45)` could do the trick. `bin_values, bin_boundaries, patches = plt.hist(...)` could help to identify the return value of `plt.hist` – JohanC Jul 02 '20 at 15:27

1 Answers1

0

You should be using plt.yticks instead: matplotlib.pyplot.yticks(ticks=None, labels=None, **kwargs) to get or set the current tick locations and labels of the y-axis.

As a reference take a look at the following code that compares vertical vs horizontal histogram plotting:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

rating = pd.read_csv('Book1.csv')

plt.subplot(1, 2, 1)
bins_age = np.arange(18, rating['Age'].max() + 2, 2)
plt.hist(rating['Age'], bins=bins_age, orientation='horizontal')
ticks_age = np.arange(18, rating['Age'].max() + 10, 10)
labels_age = ticks_age**2
plt.yticks(ticks=ticks_age, labels=labels_age)

plt.subplot(1, 2, 2)
bins_age = np.arange(18, rating['Age'].max() + 2, 2)
plt.hist(rating['Age'], bins=bins_age)
plt.show()

Output:

enter image description here

Mateo Lara
  • 827
  • 2
  • 12
  • 29