Reputation: 33
first of all, yes I am aware there is a post already from 2019 about this topic, however, it did not answer my question and I'm hoping that maybe someone has found a solution since 2019 :)
I have plotted a histogram with matplotlib and set the x-axis ticks equal to the bin edges (it makes more sense visually that way). The code looks like this:
# Using the penguins df from seaborn
n, bins, patches = plt.hist(penguins['bill_depth_mm'],
bins=7,
color='darkgreen',
edgecolor='black'
)
plt.title('Frequency of Bill depth (mm)')
plt.xlabel('Bill depth (mm)')
plt.ylabel('Penguins')
# Set x-axis ticks as bin edges
plt.xticks(bins)
plt.show()
I am trying to apply the same to a seaborn histplot by also extracting the necessary information from the sns.histplot function (n, bins, patches). Sadly, this does not work with seaborn or I could not get it to work.
The seaborn plot code looks like this so far:
import seaborn as sns
# load data
penguins = sns.load_dataset('penguins')
penguins
sns.histplot(data=penguins,
x="bill_depth_mm",
bins=7,
color='darkgreen'
)
plt.xticks(binwidth) # This throws the error: NameError: name 'binwidth' is not defined
# Add title and labels
plt.title('Distribution of Bill Depth (mm)')
plt.xlabel('Bill Depth (mm)')
plt.ylabel('Count')
plt.show()
I checked the seaborn documentation and there is a parameter called 'binwidth' which is the number or pair of numbers of the width of each bar or bin. My idea was to extract this information as an array (or pandas series) and then use this array for the x-axis?! I have tried as you can see above but it does not work so my question is:
How can I extract the binwidth information (which should be the bin edges) and use this to define my x-axis? Thank you for any tips/tricks/help in advance!
Upvotes: 0
Views: 475