Skip to content

Instantly share code, notes, and snippets.

@motatoes
Last active August 29, 2018 22:29
Show Gist options
  • Save motatoes/eaf759257acbda304b02bd8b596d0fe8 to your computer and use it in GitHub Desktop.
Save motatoes/eaf759257acbda304b02bd8b596d0fe8 to your computer and use it in GitHub Desktop.
matplotlib hist adjustment
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
np.random.seed(209313920)
x = np.random.randint(1, 4, 500)
# we can see that the histgram x-ticks seems insensible when the
# range of values is small
print('A histogram of 500 int64 numbers between 1 and 3')
# print(x)
plt.hist(x, bins=3, rwidth=0.5)
plt.show()
# but for a larger range
x = np.random.randint(1, 301, 500)
print('A histogram of 500 int64 numbers between 1 and 300')
plt.hist(x, bins=100, rwidth=0.5)
plt.show()
# In order to adjust the histogram with small number of bins we specify
# the xticks of the plot as show bellow
x = np.random.randint(1, 4, 500)
# but when we
print('A histogram of 500 int64 numbers between 1 and 3')
# print(x)
plt.hist(x, bins=range(1,5), rwidth=0.5, align='left')
plt.xticks(range(7))
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment