EcoWarrior
EcoWarrior

Reputation: 621

pyplot x-axis tick mark spacing is not centered with all columns

I'm struggling with what I hope is a misspecification of the pyplot histogram function. As you see in the image, the x-axis tick marks are not centered consistently on the columns as per the align='mid' parameter. If necessary, I will upload the data file to Dropbox. Thanks for you help !

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import FormatStrFormatter

data = DRA_size_males_s

fig, ax = plt.subplots(nrows=1, ncols=1)
ax.hist(data, facecolor='blue', edgecolor='gray', bins=25, rwidth=1.10, align='mid')


bins=[1.4,1.5,1.6,1.7,1.9,2.0,2.1,2.2,2.3,2.4,2.5,2.6,2.7,2.8,2.9,3.1,3.2,3.5,3.6,3.8] 
ax.set_xticks(bins)

ax.set_ylabel('Frequency')
ax.set_xlabel('DRA Sizes(mm)')

ax.set_title('Frequencies of DRA Sizes in Males (mm)')

plt.show()

pyplot histogram

Here is the data array used to create the histogram: 1.4, 1.4, 1.4, 1.5, 1.5, 1.6, 1.7, 1.7, 1.7, 1.9, 1.9, 1.9, 1.9, 2.0, 2.0, 2.0, 2.1, 2.1, 2.1, 2.1, 2.2, 2.2, 2.3, 2.3, 2.3, 2.4, 2.5, 2.6, 2.7, 2.7, 2.8, 2.8, 2.8, 2.9, 2.9, 3.1, 3.1, 3.2, 3.2, 3.5, 3.6, 3.8

Upvotes: 1

Views: 2586

Answers (2)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339280

The plt.hist's align="mid" argument centers the bars of the histogram in the middle between the bin edges - this is in fact the usual way of plotting a histogram.

In order for the histogram to use predefined bin edges you need to supply those bin edges to the plt.hist function.

import matplotlib.pyplot as plt
import numpy as np

data = [1.4, 1.4, 1.4, 1.5, 1.5, 1.6, 1.7, 1.7, 1.7, 1.9, 1.9, 1.9, 1.9, 2.0, 
        2.0, 2.0, 2.1, 2.1, 2.1, 2.1, 2.2, 2.2, 2.3, 2.3, 2.3, 2.4, 2.5, 2.6, 
        2.7, 2.7, 2.8, 2.8, 2.8, 2.9, 2.9, 3.1, 3.1, 3.2, 3.2, 3.5, 3.6, 3.8]

fig, ax = plt.subplots(nrows=1, ncols=1)

bins=[1.4,1.5,1.6,1.7,1.9,2.0,2.1,2.2,2.3,2.4,2.5,2.6,2.7,2.8,2.9,3.1,3.2,3.5,3.6,3.8]

ax.hist(data, bins=bins, facecolor='blue', edgecolor='gray', rwidth=1, align='mid') 
ax.set_xticks(bins)

ax.set_ylabel('Frequency')
ax.set_xlabel('DRA Sizes(mm)')
ax.set_title('Frequencies of DRA Sizes in Males (mm)')

plt.show()

enter image description here

Upvotes: 0

kmario23
kmario23

Reputation: 61365

Try to use bins with a range of values minus a small offset as in the following example.

In [100]: x = np.array([1, 2, 3, 4, 0, 3, 1, 7, 4, 5, 8, 8, 9, 7, 7, 3])

In [101]: len(x)
Out[101]: 16

In [102]: bins = np.arange(10) - 0.5

In [103]: plt.hist(x, facecolor='blue', edgecolor='gray', bins=bins, rwidth=2, alpha=0.75)

Now, the bin numbers will be center aligned.

center aligned hist plot

Upvotes: 0

Related Questions