Unable to position matplotlib ax.annotate correctly [duplicate] - python

This question already has answers here:
How to add value labels on a bar chart
(7 answers)
Stacked Bar Chart with Centered Labels
(2 answers)
How to annotate each segment of a stacked bar chart
(1 answer)
Horizontal stacked bar plot and add labels to each section
(3 answers)
Closed 11 months ago.
in the below code, I'm trying to plot stacked bar chart of % of stoke events against the gender variable.
dfgender=df.groupby('gender')['stroke'].value_counts(normalize=True).unstack().fillna(0)
ax=dfgender.plot.bar(stacked=True,colormap='tab10',figsize=(8, 6))
plt.legend(loc="upper right", ncol=1, prop={'size': 8})
plt.title('Gender vs Stroke')
plt.xlabel("Gender")
plt.ylabel("Proportion")
for p in ax.patches:
ax.annotate(round(p.get_height(),2), (p.get_x()+p.get_width()/2, p.get_height()),ha='center',va='center',xytext=(10, 0), textcoords='offset points')
plt.show()
But as you can see, the placement of the bar labels is incorrect. 1 should be 5% and 0 should be 95%. I want the figures to be represented in percentages and be positioned in the center of the bars. Can somebody help me please?

Related

Why colors on my bar plot not matching my Y axis values [duplicate]

This question already has answers here:
Trying to add color gradients to Matplotlib chart
(1 answer)
Python: Barplot colored according to a third variable
(1 answer)
Changing color scale in seaborn bar plot
(5 answers)
Closed 2 months ago.
I have monthly rain data from a list, where values are on the Y-axis and months on the X-axis.
This is my color palette:
sns.color_palette("crest", as_cmap=True)
This is my code to barplot the data:
plt.figure(figsize=(8,4), tight_layout=True)
colors = sns.color_palette("crest")
plt.bar(bocas_rain['Date'], bocas_rain['Pr'], color=colors)
plt.xlabel('Months')
plt.ylabel('Rain in mm')
plt.title('Rain in Bocas')
plt.show()
The result I am getting is this:
How can I make the highest values of my data match the dark blue colors from the palette?

Python bar chart border overlapping horizontal bars values [duplicate]

This question already has answers here:
How can I remove the top and right axis in matplotlib?
(10 answers)
Closed 7 months ago.
enter image description here
the values of bars overlapping the borders. How to change the border so that bars with values should fit in properly
#adding following lines solved the problem
fig = plt.figure(figsize=(6, 6))
ax = fig.add_subplot(111)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)

Issue with seaborn twinx - Only one graph is displaying [duplicate]

This question already has answers here:
How can I make a barplot and a lineplot in the same seaborn plot with different Y axes nicely?
(2 answers)
Problem in combining bar plot and line plot (python)
(2 answers)
Closed 10 months ago.
I want to plot 2 subplot. The First subplot is using only one y-axis and is working as expected, in second subplot i want to create dual y -axis and plot line and bar plot. But seems like only barplot is rendering, Can someplease please highlight where i am going wrong ?
f,axes =plt.subplots(1,2)
sns.set_theme(style='whitegrid',palette="pastel")
sns.set(rc={'figure.figsize':(20.7,3.27)})
plt.figure(figsize=(20.7,3.27))
# Single Y-Axis in left side plot
sns.lineplot(x='date', y="visit", data=df[(df['Website'] == url)], ci= None,ax=axes[0]).set_title(url)
sns.lineplot(x='date', y="visit_industry", data=df[(df['Website'] == url)], ci= None,ax=axes[0]).set_title(url)
## Dual Y- Axis in right hand side plot.
ax2 = axes[1].twinx()
sns.lineplot(x='date', y="yoy_change", data=df[(df['Website'] == url)],color='blue',ax=ax2)
sns.barplot(x='date', y="yoy_change_industry", data=df[(df['Website'] == url)],color='black',ax=ax2)
plt.tight_layout()
plt.show()

Can a subplot within a python-Matplotlib subplots be used for bullet statements? [duplicate]

This question already has answers here:
Delete a subplot
(3 answers)
Add Empty Subplot With No Axis Ticks/Labels for Text as Subplot in Matplotlib
(1 answer)
How to write annotation outside the drawing in data coords
(2 answers)
Closed 11 months ago.
I'd like to make a [2,2] subplot of charts/graphs using the first three open plots for the graphs and the 4 spot for bullet points about the graphs/charts. Is this possible using python/matplotlib?
Take for example the code below:
plt.style.use('fivethirtyeight')
fig, axes = plt.subplots(2,2)
fig.suptitle("Owner Occupied Loss Ratio")
sns.barplot(ax = axes[0,0], x=decile_oo['decile'], y=np.round((decile_oo['NonCAT Loss Ratio']*100),2), color = 'blue')
# g.bar_label(ax.containers[0])
sns.barplot(ax = axes[0,1], x=decile_oo['decile'], y=np.round((decile_oo['NonCAT Frequency']*100),2), color = 'red')
# f.bar_label(ax.containers[0])
sns.barplot(ax = axes[1,0], x=decile_oo['decile'], y=np.round((decile_oo['NonCAT Severity']*100),2), color = 'green')
# i.bar_label(ax.containers[0])
plt.tight_layout()
plt.show()
That produced the below graphic.
I'd like that blank graph in position [1,1] to have bullet points I type out instead of a blank graph. Is this possible?

How can I Display Bar Graph Numbers [duplicate]

This question already has answers here:
How to annotate horizontal bar plots with count and percent
(1 answer)
How to display the value of the bar on each bar with pyplot.barh()
(11 answers)
Horizontal stacked bar plot and add labels to each section
(3 answers)
how to add labels to a horizontal bar chart in matplotlib?
(1 answer)
Closed last year.
What can I add to this code to display Bar Graph Numbers?
plt.barh(regionTotal['WHO Region'],regionTotal['Confirmed'])
plt.title('Region Total Confirmed Cases')
plt.xlabel('Region')
plt.ylabel('Confirmed Cases')
plt.show()

Categories