Adjusting histogram in matplotlib [duplicate] - matplotlib

This question already has answers here:
How to create a bar chart/histogram with bar per discrete value?
(1 answer)
Matplotlib xticks not lining up with histogram
(5 answers)
Matplotlib: incorrect histograms
(1 answer)
Python Matplotlib pyplot histogram
(3 answers)
Closed 4 months ago.
I am making a histogram using matplotlib. I am using integer data and I want them to represent 1 bin. The following is the sample code which I made.
import matplotlib.pyplot as plt
a=[1,2,2,3,3,3,4,4,4,4]
plt.figure()
plt.hist(a,bins=4)
plt.show()
The histogram which I got is above. This have left end 1 ad rightend 4. I want the width of the histogram to be 1, however, this will just show 0.75 size. Moreover, I want the x value to locate at the center of the bar of histogram. Is there any way I can adjust?

Related

How to show probability density for a KDE plot in Seaborn? [duplicate]

This question already has answers here:
Seaborn kde plot plotting probabilities instead of density (histplot without bars)
(1 answer)
Seaborn distplot y-axis normalisation wrong ticklabels
(1 answer)
How to normalize seaborn distplot?
(2 answers)
Why does the y-axis of my distplot contain values up to 150?
(1 answer)
Closed 17 hours ago.
I'd like to construct a univariate KDE plot using Seaborn. The x-axis is amygdala volume, and I would like the y-axis to display probability densities. But when I use seaborn's kdeplot method, it seems like it's returning the raw counts (as if it was a histogram) instead of the densities. How do I get it to show the densities?
I've tried looking for parameters to include, but nothing would help.
plt.figure(figsize=(15,8))
sns.kdeplot(data=n90pol, x='amygdala', bw_adjust=0.5)
plt.xlabel('Amygdala Volume', fontsize=16);
plt.ylabel('Density', fontsize=16);
plt.title('KDE plot of Amygdala Volume', fontsize=24)

Seaborn colorbar height to match heatmap [duplicate]

This question already has answers here:
How to set Matplotlib colorbar height for image with aspect ratio < 1
(2 answers)
Set Matplotlib colorbar size to match graph
(9 answers)
Setting the size of a matplotlib ColorbarBase object
(2 answers)
Closed 11 months ago.
There are a few examples showing how to use "shrink" for changing the colorbar. How can I automatically figure out what the shrink should be so the colorbar is equal to the height of the heatmap?
I don't have a matplotlib axis because I am using seaborn and plotting the heatmap from the dataframe.
r = []
r.append(np.arange(0,1,.1))
r.append(np.arange(0,1,.1))
r.append(np.arange(0,1,.1))
df_cm = pd.DataFrame(r)
sns.heatmap(df_cm, square=True, cbar_kws=dict(ticks=[df_cm.min().min(), df_cm.max().max()]))
plt.tight_layout()
plt.savefig(f'test.png', bbox_inches="tight")

Matplotlib - Adding value labels to bar graph [duplicate]

This question already has answers here:
How to add value labels on a bar chart
(7 answers)
Annotating Pandas Barplot with an Additional Value Unrelated to the Plot's x and y Axis
(1 answer)
Annotate bars with values on Pandas bar plots
(4 answers)
Python pandas / matplotlib annotating labels above bar chart columns [duplicate]
(2 answers)
Stacked Bar Chart with Centered Labels
(2 answers)
Closed 1 year ago.
I have been trying to get a bar plot with value labels on each bar. I have searched all over but can't get this done. My df is the one below.
Pillar %
Exercise 19.4
Meaningful Activity 19.4
Sleep 7.7
Nutrition 22.9
Community 16.2
Stress Management 23.9
My code so far is
df_plot.plot(x ='Pillar', y='%', kind = 'bar')
plt.show()
Use ax.bar_label:
ax = df.plot(x='Pillar', y='%', kind='bar', legend=False, rot=0)
ax.bar_label(ax.containers[0], label_type='edge')
plt.tight_layout()

How to add horizontal line to a pandas df plot? [duplicate]

This question already has answers here:
Plot a horizontal line on a given plot
(7 answers)
Closed 1 year ago.
I want to draw a line on y=0. However, I am not sure how to do that using pandas built-in charting.
df.mean(axis=1).plot() I am running this and that is the result.
Catch the ax instance returned by plot, and you can use axhline:
ax = df.mean(axis=1).plot()
ax.axhline(0, c='r')
Output:

How to generate color legend for bivariate plots in Seaborn? [duplicate]

This question already has answers here:
Getting legend in seaborn jointplot
(2 answers)
Closed 3 years ago.
When generating bivariate plots like hexbin, pandas generates a legend explaining frequency value for each color shade:
pokemon.plot.hexbin(x='HP', y='Attack', gridsize=30)
I cannot find a similar way to generate such a legend for jointplot and kdeplot in seaborn:
sns.jointplot(data=pokemon, x='HP', y='Attack', kind='hex')
sns.kdeplot(pokemon['HP'], pokemon['Attack'], shade=True)
How can I do it?
for a kdeplot, simply pass cbar=True
cbar : bool, optional
If True and drawing a bivariate KDE plot, add a colorbar.