This question already has answers here:
How to add value labels on a bar chart
(7 answers)
How to plot and annotate a grouped bar chart
(1 answer)
Annotate bars with values on Pandas bar plots
(4 answers)
Closed 8 months ago.
I want to display a feature from a panda's dataframe, with using the hue parameter.
I keep getting errors of
AttributeError: 'Text' object has no attribute 'get_xticklabels'
and
AttributeError: 'Text' object has no attribute 'patches'
First I tried this code:
graph = sns.countplot(x="Residence_type", hue='stroke' , data=data).set_title("Stroke ~ Residence", size =18, y=1)
sns.set(rc={'figure.figsize':(11,8)})
medians = data.groupby(['Residence_type'])['stroke'].median().values
nobs = data['Residence_type'].value_counts().values
nobs = [str(x) for x in nobs.tolist()]
nobs = ["n: " + i for i in nobs]
pos = range(len(nobs))
for tick,label in zip(pos,graph.get_xticklabels()):
graph.text(pos[tick], medians[tick] + 0.03, nobs[tick], horizontalalignment='center', size='x-small', color='w', weight='semibold')
Didn't work so used this (also didn't work):
graph = sns.countplot(x="Residence_type", hue='stroke' , data=data).set_title("Stroke ~ Residence", size =18, y=1)
sns.set(rc={'figure.figsize':(11,8)})
for p in graph.patches:
graph.annotate('{:.1f}'.format(p.get_height()), (p.get_x()+0.25, p.get_height()+0.01))
This is my plot. I just want the height (the value) of each bin to show up on top of it:
Related
This question already has answers here:
X-axis not properly aligned with bars in barplot (seaborn)
(2 answers)
Bar labels in matplotlib/Seaborn
(1 answer)
How to get the label values on a bar chat with seaborn on a categorical data
(2 answers)
Closed 2 months ago.
The code below is the code I am using for a heart failure analysis project. But,
This method is not centering the values of each bar under the graph, pictured below.
I am not getting the percentage value above each bar in the graph
def plot_percentage(df, col, target):
x,y = col, target
temp_df = df.groupby(x)[y].value_counts(normalize=True)
temp_df = temp_df.mul(100).rename('percent').reset_index()
temp_df = temp_df[temp_df.HeartDisease != 0]
order_list = list(df[col].unique())
order_list.sort()
sns.set(font_scale=1.5)
g = sns.catplot(x=x, y='percent', hue=x,kind='bar', data=temp_df, height=8, aspect=2, order=order_list, legend_out=False)
g.ax.set_ylim(0,100)
plt.title(f'{col.title()} By Percent {target.title()}',
fontdict={'fontsize': 30})
plt.xlabel(f'{col.title()}', fontdict={'fontsize': 20})
plt.ylabel(f'{target.title()} Percentage', fontdict={'fontsize': 20})
return g
This question already has answers here:
Stacked bars are unexpectedly annotated with the sum of bar heights
(2 answers)
How to add value labels on a bar chart
(7 answers)
Closed 10 months ago.
I want to create a stacked horizontal bar plot with values of each stack displayed inside it and the total value of the stacks just after the bar. Using python matplotlib, I could create a simple barh. My dataframe looks like below:
import pandas as pd
df = pd.DataFrame({"single":[168,345,345,352],
"comp":[481,44,23,58],})
item = ["white_rice",
"pork_and_salted_vegetables",
"sausage_and_potato_in_tomato_sauce",
"curry_vegetable",]
df.index = item
Expect to get bar plot like below except that it is not horizontal:
The code I tried is here...and i get AttributeError: 'DataFrame' object has no attribute 'rows'. Please help me with horizontal bar plot. Thanks.
fig, ax = plt.subplots(figsize=(10,4))
colors = ['c', 'y']
ypos = np.zeros(len(df))
for i, row in enumerate(df.index):
ax.barh(df.index, df[row], x=ypos, label=row, color=colors[i])
bottom += np.array(df[row])
totals = df.sum(axis=0)
x_offset = 4
for i, total in enumerate(totals):
ax.text(totals.index[i], total + x_offset, round(total), ha='center',) # weight='bold')
x_offset = -15
for bar in ax.patches:
ax.text(
# Put the text in the middle of each bar. get_x returns the start so we add half the width to get to the middle.
bar.get_y() + bar.get_height() / 2,
bar.get_width() + bar.get_x() + x_offset,
# This is actual value we'll show.
round(bar.get_width()),
# Center the labels and style them a bit.
ha='center',
color='w',
weight='bold',
size=10)
labels = df.index
ax.set_title('Label Distribution Overview')
ax.set_yticklabels(labels, rotation=90)
ax.legend(fancybox=True)
Consider the following approach to get something similar with matplotlib only (I use matplotlib 3.5.0). Basically the job is done with bar/barh and bar_label combination. You may change label_type and add padding to tweak plot appearance. Also you may use fmt to format values. Edited code with total values added.
import matplotlib.pyplot as plt
import pandas as pd
import random
def main(data):
data['total'] = data['male'] + data['female']
fig, (ax1, ax2) = plt.subplots(1, 2)
fig.suptitle('Plot title')
ax1.bar(x=data['year'].astype(str), height=data['female'], label='female')
ax1.bar_label(ax1.containers[0], label_type='center')
ax1.bar(x=data['year'].astype(str), height=data['male'], bottom=data['female'], label='male')
ax1.bar_label(ax1.containers[1], label_type='center')
ax1.bar_label(ax1.containers[1], labels=data['total'], label_type='edge')
ax1.legend()
ax2.barh(y=data['year'].astype(str), width=data['female'], label='female')
ax2.bar_label(ax2.containers[0], label_type='center')
ax2.barh(y=data['year'].astype(str), width=data['male'], left=data['female'], label='male')
ax2.bar_label(ax2.containers[1], label_type='center')
ax2.bar_label(ax2.containers[1], labels=data['total'], label_type='edge')
ax2.legend()
plt.show()
if __name__ == '__main__':
N = 4
main(pd.DataFrame({
'year': [2010 + val for val in range(N)],
'female': [int(10 + 100 * random.random()) for dummy in range(N)],
'male': [int(10 + 100 * random.random()) for dummy in range(N)]}))
Result (with total values added):
This question already has answers here:
Matplotlib pie chart: Show both value and percentage
(2 answers)
Closed 12 months ago.
we are trying with the below code to get the pie charts but we can only see the percentage in pie chart for each category and unable to print its exact values along with percentage.
dfinfoofbuss = pd.read_csv("file.csv", header=None)
dfinfoofbuss.columns = ['ID', 'INFO']
dfinfoofbuss['VALUE_INFO'] = dfinfoofbuss['ID'].str.split('_').str[1]
dfinfoofbusscnt = dfinfoofbuss.groupby(['VALUE_INFO']).size().reset_index(name='COUNT_INFO')
print("dfinfoofbusscnt:",dfinfoofbusscnt)
plotvar3 = dfinfoofbusscnt.groupby(['VALUE_INFO']).sum().plot(kind='pie' ,title='pie chart', figsize=(6,6), autopct='%.2f', legend = False, use_index=False, subplots=True, colormap="Pastel1")
fig3 = plotvar3[0].get_figure()
fig3.savefig("Info.jpg")
Sample Data
VALUE_INFO CountInfo
abc 1
defair 2
cdf 109
aggr 1
sum 1
normal 2
dev 1
Is there a way to print its original values along with percentage in pie chart.. Pls suggest
You will likely need to write your own custom function to get both value and percentage as labels.
Try:
def formatter(x):
return f"{total*x/100:.0f} ({x:.2f})%"
total = sum(dfinfoofbusscnt["CountInfo"])
plotdata = dfinfoofbusscnt.groupby("VALUE_INFO").sum()
>>> plotdata.plot(kind='pie',
title='pie chart',
figsize=(6,6),
autopct=formatter,
colormap="Pastel1",
legend=False,
subplots=True
)
This question already has answers here:
Need to add space between SubPlots for X axis label, maybe remove labelling of axis notches
(2 answers)
Improve subplot size/spacing with many subplots
(8 answers)
How to remove gaps between subplots in matplotlib
(6 answers)
Closed 1 year ago.
I have the following code:
fig1 = plt.figure(figsize=(10, 10))
# Objeto
ax1 = fig1.add_subplot(3, 1, 1)
ax1.plot(xL, fL, 'k')
ax1.set_xlim(-0.04, 0.04)
ax1.set_ylim(0, 2.1)
ax1.set_title('Objeto')
ax1.set_xlabel('d (cm)')
ax1.set_ylabel('Intensidade')
ax1.grid(axis='both')
# Echo
ax2 = fig1.add_subplot(3, 1, 2)
tempo = np.arange(0, N0) * dT
CurvaT2 = exp(-tempo / T2)
ax2.plot(tempo, Ms[0, :], 'b', tempo, Ms[1, :], 'k-')
ax2.set_title('Echo') # 'FontSize',12
ax2.set_xlabel('Tempo (ms)')
ax2.set_ylabel('Intensidade')
ax2.grid(axis='both')
# Módulo Magnetização
ax3 = fig1.add_subplot(3, 1, 3)
ax3.plot(tempo, Mod, 'k', tempo, CurvaT2, 'g--')
ax3.set_title('Módulo Magnetização') # 'FontSize',12
ax3.set_xlabel('Tempo (ms)')
ax3.set_ylabel('Intensidade')
ax3.grid(axis='both')
Which produces the following image:
As you can see, the title and the xlabels are mixed up. What could I do to fix that?
An easy and practical way to resolve this is using the method plt.tight_layout() at the end of the code. This method automatically separates in a good way the figures.
Example:
and you can increase the separation between them with the pad parameter: plt.tight_layout(pad = 2) (the default is 1.08).
More details about this lovely function:
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.tight_layout.html
This question already has answers here:
How do I change the size of figures drawn with Matplotlib?
(14 answers)
Closed 1 year ago.
I want to make the x axis of a figure wider in matplotlib and I use the following code.
But it seems that figsize does not have any effect. How I can change the size of the figure?
data_dates = np.loadtxt(file,usecols = 0, dtype=np.str)
data1 = np.loadtxt(file,usecols = 1)
data2 = np.loadtxt(file,usecols = 2)
data3 = np.loadtxt(file,usecols = 3)
plt.plot(figsize=(30,5))
plt.plot(data_dates,data1, label = "T")
plt.plot(data_dates,data2, label = "WS")
plt.plot(data_dates,data3, label = "WD")
plt.xlabel('Date', fontsize=8)
plt.xticks(rotation=90,fontsize=4)
plt.ylabel(' Percentage Difference (%)')
plt.legend()
plt.savefig(outfile,format='png',dpi=200,bbox_inches='tight')
a sample of the file is
01/06/2019 0.1897540512577196 0.28956205456965856 0.10983099750547703
02/06/2019 0.1914523564094276 0.1815325705314345 0.0004533827128655877
03/06/2019 0.2365346386184113 0.12301344973593868 0.058843355966174876
04/06/2019 0.2085897993039386 0.005466902564359565 0.014087537281676313
05/06/2019 0.15563355684612554 0.16249844426472368 0.11036007669758358
06/06/2019 0.11981475728282368 0.11015459703126898 0.03501167308950372
fig, ax = plt.subplots(1, 1)
fig.set_size_inches(30, 5)
plt.plot(data_dates,data1, label = "T")
plt.plot(data_dates,data2, label = "WS")
plt.plot(data_dates,data3, label = "WD")
plt.xlabel('Date', fontsize=8)
plt.xticks(rotation=90,fontsize=4)
plt.ylabel(' Percentage Difference (%)')
plt.legend()
plt.savefig("test.png",format='png',dpi=200,bbox_inches='tight')
Instead of creating the figure explicitly using subplots you could also use the get-current-figure method: fig = plt.gcf().