Horizontal barplot with annotations
I was successfully running the code when apparently python kernel died and dies everytime I am running this code:
Is it something wrong with the code or the problem is deeper? I can run other notebooks without problems.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
plt.rcParams['text.usetex'] = False
df = pd.DataFrame(np.random.uniform(size=37)*100, columns=['A'])
ax = plt.figure(figsize=(10,5))
plt.barh(df.index, df['A'], color='ForestGreen')
plt.yticks(df.index)
def annotateBars(row, ax=ax):
if row['A'] < 20:
color = 'black'
horalign = 'right'
horpad = 2
else:
color = 'white'
horalign = 'right'
horpad = -2
ax.text(row.name, row['A'] + horpad, ":.1f%".format(row['A']),
color=color,
horizontalalignment=horalign,
verticalalignment='center',
fontsize=10)
junk = df.apply(annotateBars, ax=ax, axis=1)
python pandas matplotlib kernel anaconda
|
show 3 more comments
I was successfully running the code when apparently python kernel died and dies everytime I am running this code:
Is it something wrong with the code or the problem is deeper? I can run other notebooks without problems.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
plt.rcParams['text.usetex'] = False
df = pd.DataFrame(np.random.uniform(size=37)*100, columns=['A'])
ax = plt.figure(figsize=(10,5))
plt.barh(df.index, df['A'], color='ForestGreen')
plt.yticks(df.index)
def annotateBars(row, ax=ax):
if row['A'] < 20:
color = 'black'
horalign = 'right'
horpad = 2
else:
color = 'white'
horalign = 'right'
horpad = -2
ax.text(row.name, row['A'] + horpad, ":.1f%".format(row['A']),
color=color,
horizontalalignment=horalign,
verticalalignment='center',
fontsize=10)
junk = df.apply(annotateBars, ax=ax, axis=1)
python pandas matplotlib kernel anaconda
Are there any errors you receive? and if you run the process with Task Manager up, is your computer handling the process okay?
– MattR
Mar 17 '17 at 15:35
Got the same issue when running your code, Kernel just dies... strange. However when defining the function at the beginning and assigning the junk variable before plotting everything runs fine.
– schlump
Mar 17 '17 at 15:38
@schlump This is extremely strange, I was able to run this code as it is now with junk at the bottom. I remember that last thing I changed was something in plt.barh() line but not sure what (cant revert back after kernel died).
– Alex T
Mar 17 '17 at 15:40
Well okay.. seems like its an issue with the plotting... when adding plt.show() after plt.yticks() everything also runs fine... seems like the Kernel does not really likes running the code when the plot is still "open"
– schlump
Mar 17 '17 at 15:43
Well I did as you said and its not dying anymore but, wow the result is awful right now. There is huge blank space before the plot, what did I do wrong
– Alex T
Mar 17 '17 at 15:48
|
show 3 more comments
I was successfully running the code when apparently python kernel died and dies everytime I am running this code:
Is it something wrong with the code or the problem is deeper? I can run other notebooks without problems.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
plt.rcParams['text.usetex'] = False
df = pd.DataFrame(np.random.uniform(size=37)*100, columns=['A'])
ax = plt.figure(figsize=(10,5))
plt.barh(df.index, df['A'], color='ForestGreen')
plt.yticks(df.index)
def annotateBars(row, ax=ax):
if row['A'] < 20:
color = 'black'
horalign = 'right'
horpad = 2
else:
color = 'white'
horalign = 'right'
horpad = -2
ax.text(row.name, row['A'] + horpad, ":.1f%".format(row['A']),
color=color,
horizontalalignment=horalign,
verticalalignment='center',
fontsize=10)
junk = df.apply(annotateBars, ax=ax, axis=1)
python pandas matplotlib kernel anaconda
I was successfully running the code when apparently python kernel died and dies everytime I am running this code:
Is it something wrong with the code or the problem is deeper? I can run other notebooks without problems.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
plt.rcParams['text.usetex'] = False
df = pd.DataFrame(np.random.uniform(size=37)*100, columns=['A'])
ax = plt.figure(figsize=(10,5))
plt.barh(df.index, df['A'], color='ForestGreen')
plt.yticks(df.index)
def annotateBars(row, ax=ax):
if row['A'] < 20:
color = 'black'
horalign = 'right'
horpad = 2
else:
color = 'white'
horalign = 'right'
horpad = -2
ax.text(row.name, row['A'] + horpad, ":.1f%".format(row['A']),
color=color,
horizontalalignment=horalign,
verticalalignment='center',
fontsize=10)
junk = df.apply(annotateBars, ax=ax, axis=1)
python pandas matplotlib kernel anaconda
python pandas matplotlib kernel anaconda
edited Mar 18 '17 at 7:28
Alex T
asked Mar 17 '17 at 15:12
Alex TAlex T
5181623
5181623
Are there any errors you receive? and if you run the process with Task Manager up, is your computer handling the process okay?
– MattR
Mar 17 '17 at 15:35
Got the same issue when running your code, Kernel just dies... strange. However when defining the function at the beginning and assigning the junk variable before plotting everything runs fine.
– schlump
Mar 17 '17 at 15:38
@schlump This is extremely strange, I was able to run this code as it is now with junk at the bottom. I remember that last thing I changed was something in plt.barh() line but not sure what (cant revert back after kernel died).
– Alex T
Mar 17 '17 at 15:40
Well okay.. seems like its an issue with the plotting... when adding plt.show() after plt.yticks() everything also runs fine... seems like the Kernel does not really likes running the code when the plot is still "open"
– schlump
Mar 17 '17 at 15:43
Well I did as you said and its not dying anymore but, wow the result is awful right now. There is huge blank space before the plot, what did I do wrong
– Alex T
Mar 17 '17 at 15:48
|
show 3 more comments
Are there any errors you receive? and if you run the process with Task Manager up, is your computer handling the process okay?
– MattR
Mar 17 '17 at 15:35
Got the same issue when running your code, Kernel just dies... strange. However when defining the function at the beginning and assigning the junk variable before plotting everything runs fine.
– schlump
Mar 17 '17 at 15:38
@schlump This is extremely strange, I was able to run this code as it is now with junk at the bottom. I remember that last thing I changed was something in plt.barh() line but not sure what (cant revert back after kernel died).
– Alex T
Mar 17 '17 at 15:40
Well okay.. seems like its an issue with the plotting... when adding plt.show() after plt.yticks() everything also runs fine... seems like the Kernel does not really likes running the code when the plot is still "open"
– schlump
Mar 17 '17 at 15:43
Well I did as you said and its not dying anymore but, wow the result is awful right now. There is huge blank space before the plot, what did I do wrong
– Alex T
Mar 17 '17 at 15:48
Are there any errors you receive? and if you run the process with Task Manager up, is your computer handling the process okay?
– MattR
Mar 17 '17 at 15:35
Are there any errors you receive? and if you run the process with Task Manager up, is your computer handling the process okay?
– MattR
Mar 17 '17 at 15:35
Got the same issue when running your code, Kernel just dies... strange. However when defining the function at the beginning and assigning the junk variable before plotting everything runs fine.
– schlump
Mar 17 '17 at 15:38
Got the same issue when running your code, Kernel just dies... strange. However when defining the function at the beginning and assigning the junk variable before plotting everything runs fine.
– schlump
Mar 17 '17 at 15:38
@schlump This is extremely strange, I was able to run this code as it is now with junk at the bottom. I remember that last thing I changed was something in plt.barh() line but not sure what (cant revert back after kernel died).
– Alex T
Mar 17 '17 at 15:40
@schlump This is extremely strange, I was able to run this code as it is now with junk at the bottom. I remember that last thing I changed was something in plt.barh() line but not sure what (cant revert back after kernel died).
– Alex T
Mar 17 '17 at 15:40
Well okay.. seems like its an issue with the plotting... when adding plt.show() after plt.yticks() everything also runs fine... seems like the Kernel does not really likes running the code when the plot is still "open"
– schlump
Mar 17 '17 at 15:43
Well okay.. seems like its an issue with the plotting... when adding plt.show() after plt.yticks() everything also runs fine... seems like the Kernel does not really likes running the code when the plot is still "open"
– schlump
Mar 17 '17 at 15:43
Well I did as you said and its not dying anymore but, wow the result is awful right now. There is huge blank space before the plot, what did I do wrong
– Alex T
Mar 17 '17 at 15:48
Well I did as you said and its not dying anymore but, wow the result is awful right now. There is huge blank space before the plot, what did I do wrong
– Alex T
Mar 17 '17 at 15:48
|
show 3 more comments
2 Answers
2
active
oldest
votes
Maybe you should change the title of the question, because as for you it does not really matter why the Kernel dies. As far as I've understood you, the problem is:
Creating a horizontal bar chart with different bar colors and the values of each bar as an annotation.
Here is a solution using Seaborn:
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import pandas
sns.set(style="darkgrid")
sns.set_color_codes("muted")
# Create example DataFrame
df = pandas.DataFrame(np.random.uniform(size=20)*100, columns=['A'])
# Create list of colors based on a condition
colors = ['red' if (x < 20) else 'green' for x in df['A']]
# Create barplot
ax = sns.barplot(data=df.transpose(), palette=colors, orient='h')
# Annotate every single Bar with its value, based on it's width
for p in ax.patches:
width = p.get_width()
plt.text(5+p.get_width(), p.get_y()+0.55*p.get_height(),
':1.2f'.format(width),
ha='center', va='center')
Creates:

Update:
For also coloring the text:
for p in ax.patches:
width = p.get_width()
if width < 20:
clr = 'red'
else:
clr = 'green'
plt.text(5+p.get_width(), p.get_y()+0.55*p.get_height(),
':1.2f'.format(width),color=clr,
ha='center', va='center')
Making the plot larger so the background also covers the annotations:
ax.set_xlim([0, max(df['A'])+10])
Right, this solves the problem, however I want to ask one more thing, is there any way to color the annotiations in same way as bars? I tried just addingcolor=colorsbut its not that simple.
– Alex T
Mar 18 '17 at 7:42
Yeha colors is a list of strings, you set the annotations in a loop where you need to pass 1 value.
– schlump
Mar 18 '17 at 8:03
what do you mean?
– Alex T
Mar 18 '17 at 8:11
See the update, you need to pass 1 value to the color kwargs. Because you are setting the color for every single annotation in a loop. So color needs to be 'green' or 'red'... before we created a list of colors ['green','red']
– schlump
Mar 18 '17 at 8:16
Okay this works, btw do you know what type of the variable need to be provided as clr? Because I redoing this barplot with a dataframe that has three columns and now im annotating barplots with different columns than the barplots itself so cant use the p.width colors but need to convert that column into type that can be used as color of choice here.
– Alex T
Mar 18 '17 at 10:14
add a comment |
Great! I can write annotations on horizontal barplot.
Well, what can i do if i have more than one bars?
seaborn horizontal barplot with annotation
After many tries that worked on my code:
cnt=0
for p in ax.patches:
width=p.get_width()
if cnt%2==0:
a=p.get_width()-5
clr = 'purple'
else:
a=p.get_width()+5
clr = 'blue'
plt.text(a,
p.get_y()+0.55*p.get_height(),':1.2f'.format(width),color=clr,ha='center', va='center')
cnt=cnt+1
result is:

add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42861049%2fhorizontal-barplot-with-annotations%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Maybe you should change the title of the question, because as for you it does not really matter why the Kernel dies. As far as I've understood you, the problem is:
Creating a horizontal bar chart with different bar colors and the values of each bar as an annotation.
Here is a solution using Seaborn:
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import pandas
sns.set(style="darkgrid")
sns.set_color_codes("muted")
# Create example DataFrame
df = pandas.DataFrame(np.random.uniform(size=20)*100, columns=['A'])
# Create list of colors based on a condition
colors = ['red' if (x < 20) else 'green' for x in df['A']]
# Create barplot
ax = sns.barplot(data=df.transpose(), palette=colors, orient='h')
# Annotate every single Bar with its value, based on it's width
for p in ax.patches:
width = p.get_width()
plt.text(5+p.get_width(), p.get_y()+0.55*p.get_height(),
':1.2f'.format(width),
ha='center', va='center')
Creates:

Update:
For also coloring the text:
for p in ax.patches:
width = p.get_width()
if width < 20:
clr = 'red'
else:
clr = 'green'
plt.text(5+p.get_width(), p.get_y()+0.55*p.get_height(),
':1.2f'.format(width),color=clr,
ha='center', va='center')
Making the plot larger so the background also covers the annotations:
ax.set_xlim([0, max(df['A'])+10])
Right, this solves the problem, however I want to ask one more thing, is there any way to color the annotiations in same way as bars? I tried just addingcolor=colorsbut its not that simple.
– Alex T
Mar 18 '17 at 7:42
Yeha colors is a list of strings, you set the annotations in a loop where you need to pass 1 value.
– schlump
Mar 18 '17 at 8:03
what do you mean?
– Alex T
Mar 18 '17 at 8:11
See the update, you need to pass 1 value to the color kwargs. Because you are setting the color for every single annotation in a loop. So color needs to be 'green' or 'red'... before we created a list of colors ['green','red']
– schlump
Mar 18 '17 at 8:16
Okay this works, btw do you know what type of the variable need to be provided as clr? Because I redoing this barplot with a dataframe that has three columns and now im annotating barplots with different columns than the barplots itself so cant use the p.width colors but need to convert that column into type that can be used as color of choice here.
– Alex T
Mar 18 '17 at 10:14
add a comment |
Maybe you should change the title of the question, because as for you it does not really matter why the Kernel dies. As far as I've understood you, the problem is:
Creating a horizontal bar chart with different bar colors and the values of each bar as an annotation.
Here is a solution using Seaborn:
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import pandas
sns.set(style="darkgrid")
sns.set_color_codes("muted")
# Create example DataFrame
df = pandas.DataFrame(np.random.uniform(size=20)*100, columns=['A'])
# Create list of colors based on a condition
colors = ['red' if (x < 20) else 'green' for x in df['A']]
# Create barplot
ax = sns.barplot(data=df.transpose(), palette=colors, orient='h')
# Annotate every single Bar with its value, based on it's width
for p in ax.patches:
width = p.get_width()
plt.text(5+p.get_width(), p.get_y()+0.55*p.get_height(),
':1.2f'.format(width),
ha='center', va='center')
Creates:

Update:
For also coloring the text:
for p in ax.patches:
width = p.get_width()
if width < 20:
clr = 'red'
else:
clr = 'green'
plt.text(5+p.get_width(), p.get_y()+0.55*p.get_height(),
':1.2f'.format(width),color=clr,
ha='center', va='center')
Making the plot larger so the background also covers the annotations:
ax.set_xlim([0, max(df['A'])+10])
Right, this solves the problem, however I want to ask one more thing, is there any way to color the annotiations in same way as bars? I tried just addingcolor=colorsbut its not that simple.
– Alex T
Mar 18 '17 at 7:42
Yeha colors is a list of strings, you set the annotations in a loop where you need to pass 1 value.
– schlump
Mar 18 '17 at 8:03
what do you mean?
– Alex T
Mar 18 '17 at 8:11
See the update, you need to pass 1 value to the color kwargs. Because you are setting the color for every single annotation in a loop. So color needs to be 'green' or 'red'... before we created a list of colors ['green','red']
– schlump
Mar 18 '17 at 8:16
Okay this works, btw do you know what type of the variable need to be provided as clr? Because I redoing this barplot with a dataframe that has three columns and now im annotating barplots with different columns than the barplots itself so cant use the p.width colors but need to convert that column into type that can be used as color of choice here.
– Alex T
Mar 18 '17 at 10:14
add a comment |
Maybe you should change the title of the question, because as for you it does not really matter why the Kernel dies. As far as I've understood you, the problem is:
Creating a horizontal bar chart with different bar colors and the values of each bar as an annotation.
Here is a solution using Seaborn:
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import pandas
sns.set(style="darkgrid")
sns.set_color_codes("muted")
# Create example DataFrame
df = pandas.DataFrame(np.random.uniform(size=20)*100, columns=['A'])
# Create list of colors based on a condition
colors = ['red' if (x < 20) else 'green' for x in df['A']]
# Create barplot
ax = sns.barplot(data=df.transpose(), palette=colors, orient='h')
# Annotate every single Bar with its value, based on it's width
for p in ax.patches:
width = p.get_width()
plt.text(5+p.get_width(), p.get_y()+0.55*p.get_height(),
':1.2f'.format(width),
ha='center', va='center')
Creates:

Update:
For also coloring the text:
for p in ax.patches:
width = p.get_width()
if width < 20:
clr = 'red'
else:
clr = 'green'
plt.text(5+p.get_width(), p.get_y()+0.55*p.get_height(),
':1.2f'.format(width),color=clr,
ha='center', va='center')
Making the plot larger so the background also covers the annotations:
ax.set_xlim([0, max(df['A'])+10])
Maybe you should change the title of the question, because as for you it does not really matter why the Kernel dies. As far as I've understood you, the problem is:
Creating a horizontal bar chart with different bar colors and the values of each bar as an annotation.
Here is a solution using Seaborn:
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import pandas
sns.set(style="darkgrid")
sns.set_color_codes("muted")
# Create example DataFrame
df = pandas.DataFrame(np.random.uniform(size=20)*100, columns=['A'])
# Create list of colors based on a condition
colors = ['red' if (x < 20) else 'green' for x in df['A']]
# Create barplot
ax = sns.barplot(data=df.transpose(), palette=colors, orient='h')
# Annotate every single Bar with its value, based on it's width
for p in ax.patches:
width = p.get_width()
plt.text(5+p.get_width(), p.get_y()+0.55*p.get_height(),
':1.2f'.format(width),
ha='center', va='center')
Creates:

Update:
For also coloring the text:
for p in ax.patches:
width = p.get_width()
if width < 20:
clr = 'red'
else:
clr = 'green'
plt.text(5+p.get_width(), p.get_y()+0.55*p.get_height(),
':1.2f'.format(width),color=clr,
ha='center', va='center')
Making the plot larger so the background also covers the annotations:
ax.set_xlim([0, max(df['A'])+10])
edited Mar 18 '17 at 8:04
answered Mar 17 '17 at 18:44
schlumpschlump
6211510
6211510
Right, this solves the problem, however I want to ask one more thing, is there any way to color the annotiations in same way as bars? I tried just addingcolor=colorsbut its not that simple.
– Alex T
Mar 18 '17 at 7:42
Yeha colors is a list of strings, you set the annotations in a loop where you need to pass 1 value.
– schlump
Mar 18 '17 at 8:03
what do you mean?
– Alex T
Mar 18 '17 at 8:11
See the update, you need to pass 1 value to the color kwargs. Because you are setting the color for every single annotation in a loop. So color needs to be 'green' or 'red'... before we created a list of colors ['green','red']
– schlump
Mar 18 '17 at 8:16
Okay this works, btw do you know what type of the variable need to be provided as clr? Because I redoing this barplot with a dataframe that has three columns and now im annotating barplots with different columns than the barplots itself so cant use the p.width colors but need to convert that column into type that can be used as color of choice here.
– Alex T
Mar 18 '17 at 10:14
add a comment |
Right, this solves the problem, however I want to ask one more thing, is there any way to color the annotiations in same way as bars? I tried just addingcolor=colorsbut its not that simple.
– Alex T
Mar 18 '17 at 7:42
Yeha colors is a list of strings, you set the annotations in a loop where you need to pass 1 value.
– schlump
Mar 18 '17 at 8:03
what do you mean?
– Alex T
Mar 18 '17 at 8:11
See the update, you need to pass 1 value to the color kwargs. Because you are setting the color for every single annotation in a loop. So color needs to be 'green' or 'red'... before we created a list of colors ['green','red']
– schlump
Mar 18 '17 at 8:16
Okay this works, btw do you know what type of the variable need to be provided as clr? Because I redoing this barplot with a dataframe that has three columns and now im annotating barplots with different columns than the barplots itself so cant use the p.width colors but need to convert that column into type that can be used as color of choice here.
– Alex T
Mar 18 '17 at 10:14
Right, this solves the problem, however I want to ask one more thing, is there any way to color the annotiations in same way as bars? I tried just adding
color=colors but its not that simple.– Alex T
Mar 18 '17 at 7:42
Right, this solves the problem, however I want to ask one more thing, is there any way to color the annotiations in same way as bars? I tried just adding
color=colors but its not that simple.– Alex T
Mar 18 '17 at 7:42
Yeha colors is a list of strings, you set the annotations in a loop where you need to pass 1 value.
– schlump
Mar 18 '17 at 8:03
Yeha colors is a list of strings, you set the annotations in a loop where you need to pass 1 value.
– schlump
Mar 18 '17 at 8:03
what do you mean?
– Alex T
Mar 18 '17 at 8:11
what do you mean?
– Alex T
Mar 18 '17 at 8:11
See the update, you need to pass 1 value to the color kwargs. Because you are setting the color for every single annotation in a loop. So color needs to be 'green' or 'red'... before we created a list of colors ['green','red']
– schlump
Mar 18 '17 at 8:16
See the update, you need to pass 1 value to the color kwargs. Because you are setting the color for every single annotation in a loop. So color needs to be 'green' or 'red'... before we created a list of colors ['green','red']
– schlump
Mar 18 '17 at 8:16
Okay this works, btw do you know what type of the variable need to be provided as clr? Because I redoing this barplot with a dataframe that has three columns and now im annotating barplots with different columns than the barplots itself so cant use the p.width colors but need to convert that column into type that can be used as color of choice here.
– Alex T
Mar 18 '17 at 10:14
Okay this works, btw do you know what type of the variable need to be provided as clr? Because I redoing this barplot with a dataframe that has three columns and now im annotating barplots with different columns than the barplots itself so cant use the p.width colors but need to convert that column into type that can be used as color of choice here.
– Alex T
Mar 18 '17 at 10:14
add a comment |
Great! I can write annotations on horizontal barplot.
Well, what can i do if i have more than one bars?
seaborn horizontal barplot with annotation
After many tries that worked on my code:
cnt=0
for p in ax.patches:
width=p.get_width()
if cnt%2==0:
a=p.get_width()-5
clr = 'purple'
else:
a=p.get_width()+5
clr = 'blue'
plt.text(a,
p.get_y()+0.55*p.get_height(),':1.2f'.format(width),color=clr,ha='center', va='center')
cnt=cnt+1
result is:

add a comment |
Great! I can write annotations on horizontal barplot.
Well, what can i do if i have more than one bars?
seaborn horizontal barplot with annotation
After many tries that worked on my code:
cnt=0
for p in ax.patches:
width=p.get_width()
if cnt%2==0:
a=p.get_width()-5
clr = 'purple'
else:
a=p.get_width()+5
clr = 'blue'
plt.text(a,
p.get_y()+0.55*p.get_height(),':1.2f'.format(width),color=clr,ha='center', va='center')
cnt=cnt+1
result is:

add a comment |
Great! I can write annotations on horizontal barplot.
Well, what can i do if i have more than one bars?
seaborn horizontal barplot with annotation
After many tries that worked on my code:
cnt=0
for p in ax.patches:
width=p.get_width()
if cnt%2==0:
a=p.get_width()-5
clr = 'purple'
else:
a=p.get_width()+5
clr = 'blue'
plt.text(a,
p.get_y()+0.55*p.get_height(),':1.2f'.format(width),color=clr,ha='center', va='center')
cnt=cnt+1
result is:

Great! I can write annotations on horizontal barplot.
Well, what can i do if i have more than one bars?
seaborn horizontal barplot with annotation
After many tries that worked on my code:
cnt=0
for p in ax.patches:
width=p.get_width()
if cnt%2==0:
a=p.get_width()-5
clr = 'purple'
else:
a=p.get_width()+5
clr = 'blue'
plt.text(a,
p.get_y()+0.55*p.get_height(),':1.2f'.format(width),color=clr,ha='center', va='center')
cnt=cnt+1
result is:

edited Nov 15 '18 at 1:11
Stephen Rauch
29.7k153657
29.7k153657
answered Nov 15 '18 at 0:53
Batuhan AKTAŞ boheminsanBatuhan AKTAŞ boheminsan
12
12
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42861049%2fhorizontal-barplot-with-annotations%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are there any errors you receive? and if you run the process with Task Manager up, is your computer handling the process okay?
– MattR
Mar 17 '17 at 15:35
Got the same issue when running your code, Kernel just dies... strange. However when defining the function at the beginning and assigning the junk variable before plotting everything runs fine.
– schlump
Mar 17 '17 at 15:38
@schlump This is extremely strange, I was able to run this code as it is now with junk at the bottom. I remember that last thing I changed was something in plt.barh() line but not sure what (cant revert back after kernel died).
– Alex T
Mar 17 '17 at 15:40
Well okay.. seems like its an issue with the plotting... when adding plt.show() after plt.yticks() everything also runs fine... seems like the Kernel does not really likes running the code when the plot is still "open"
– schlump
Mar 17 '17 at 15:43
Well I did as you said and its not dying anymore but, wow the result is awful right now. There is huge blank space before the plot, what did I do wrong
– Alex T
Mar 17 '17 at 15:48