How to add a line to a bar chart











up vote
0
down vote

favorite
1












Diclaimer: I am still pretty new to data analysis and Python, so if you see a better way to write code for plotting my data, I would be more than happy to get feedback.



I have data for two different categories, over 7 weeks.
I need to plot this data onto a graph and make it easy to read.



The first thing I did it to create a bar chart with data from group 2 stacked on data from group 1.
However I would also like to add a line to my graph that shows the evolution of the data from group 2 a little better.



But when I do so, the line seems to use different values for x, even though I used the exact same thing.



fig, ax = plt.subplots(figsize=(20,10))
sns.barplot('week', 'group1', data=memberships, color = '#E30613', saturation=1)
sns.barplot('week', 'group2', data=memberships, color = '#009FE3', bottom=group1, saturation=1)
sns.lineplot('week', 'group2', data=memberships)


The result



What can I do to have the line on top of the bar chart?



EDIT: To complement my question, here is the dataset I am using










share|improve this question
























  • I am surprised the line is not on top of the bar chart already since you specify the same x-values for the bar- and the lineplot. Could you post a few lines of the membership data?
    – onno
    Nov 7 at 8:33










  • I just edited the post, thank you for trying to help me out. Same as you I don't understand since I use the same x-values
    – Valentin Semur
    Nov 7 at 9:07












  • a barplot is for categorical data, and uses sequential ordinal x-positions for each category (0,1,2,...). But the labels (in your case) are numerical and not starting at zero. seaborn doesn't allow specification of the x-positions in barplots. See seaborn.pydata.org/tutorial/categorical.html. In the lineplot, the x-positions are defined by the first argument, i.e., 'week'.
    – Bonlenfum
    Nov 7 at 9:37










  • if it is essential to add these two types of plot on one figure, you might be better off using matplotlib. See python-graph-gallery.com/5-control-width-and-space-in-barplots for an example of how to set x positions in a bar plot. But perhaps you can just rethink how to show your data (e.g. with 2 subplots)
    – Bonlenfum
    Nov 7 at 9:44












  • Thank you for your help, using matplotlib instead of seabron seems to work fine.
    – Valentin Semur
    Nov 8 at 1:00















up vote
0
down vote

favorite
1












Diclaimer: I am still pretty new to data analysis and Python, so if you see a better way to write code for plotting my data, I would be more than happy to get feedback.



I have data for two different categories, over 7 weeks.
I need to plot this data onto a graph and make it easy to read.



The first thing I did it to create a bar chart with data from group 2 stacked on data from group 1.
However I would also like to add a line to my graph that shows the evolution of the data from group 2 a little better.



But when I do so, the line seems to use different values for x, even though I used the exact same thing.



fig, ax = plt.subplots(figsize=(20,10))
sns.barplot('week', 'group1', data=memberships, color = '#E30613', saturation=1)
sns.barplot('week', 'group2', data=memberships, color = '#009FE3', bottom=group1, saturation=1)
sns.lineplot('week', 'group2', data=memberships)


The result



What can I do to have the line on top of the bar chart?



EDIT: To complement my question, here is the dataset I am using










share|improve this question
























  • I am surprised the line is not on top of the bar chart already since you specify the same x-values for the bar- and the lineplot. Could you post a few lines of the membership data?
    – onno
    Nov 7 at 8:33










  • I just edited the post, thank you for trying to help me out. Same as you I don't understand since I use the same x-values
    – Valentin Semur
    Nov 7 at 9:07












  • a barplot is for categorical data, and uses sequential ordinal x-positions for each category (0,1,2,...). But the labels (in your case) are numerical and not starting at zero. seaborn doesn't allow specification of the x-positions in barplots. See seaborn.pydata.org/tutorial/categorical.html. In the lineplot, the x-positions are defined by the first argument, i.e., 'week'.
    – Bonlenfum
    Nov 7 at 9:37










  • if it is essential to add these two types of plot on one figure, you might be better off using matplotlib. See python-graph-gallery.com/5-control-width-and-space-in-barplots for an example of how to set x positions in a bar plot. But perhaps you can just rethink how to show your data (e.g. with 2 subplots)
    – Bonlenfum
    Nov 7 at 9:44












  • Thank you for your help, using matplotlib instead of seabron seems to work fine.
    – Valentin Semur
    Nov 8 at 1:00













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





Diclaimer: I am still pretty new to data analysis and Python, so if you see a better way to write code for plotting my data, I would be more than happy to get feedback.



I have data for two different categories, over 7 weeks.
I need to plot this data onto a graph and make it easy to read.



The first thing I did it to create a bar chart with data from group 2 stacked on data from group 1.
However I would also like to add a line to my graph that shows the evolution of the data from group 2 a little better.



But when I do so, the line seems to use different values for x, even though I used the exact same thing.



fig, ax = plt.subplots(figsize=(20,10))
sns.barplot('week', 'group1', data=memberships, color = '#E30613', saturation=1)
sns.barplot('week', 'group2', data=memberships, color = '#009FE3', bottom=group1, saturation=1)
sns.lineplot('week', 'group2', data=memberships)


The result



What can I do to have the line on top of the bar chart?



EDIT: To complement my question, here is the dataset I am using










share|improve this question















Diclaimer: I am still pretty new to data analysis and Python, so if you see a better way to write code for plotting my data, I would be more than happy to get feedback.



I have data for two different categories, over 7 weeks.
I need to plot this data onto a graph and make it easy to read.



The first thing I did it to create a bar chart with data from group 2 stacked on data from group 1.
However I would also like to add a line to my graph that shows the evolution of the data from group 2 a little better.



But when I do so, the line seems to use different values for x, even though I used the exact same thing.



fig, ax = plt.subplots(figsize=(20,10))
sns.barplot('week', 'group1', data=memberships, color = '#E30613', saturation=1)
sns.barplot('week', 'group2', data=memberships, color = '#009FE3', bottom=group1, saturation=1)
sns.lineplot('week', 'group2', data=memberships)


The result



What can I do to have the line on top of the bar chart?



EDIT: To complement my question, here is the dataset I am using







python graph seaborn data-analysis






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 9:07

























asked Nov 7 at 8:05









Valentin Semur

83




83












  • I am surprised the line is not on top of the bar chart already since you specify the same x-values for the bar- and the lineplot. Could you post a few lines of the membership data?
    – onno
    Nov 7 at 8:33










  • I just edited the post, thank you for trying to help me out. Same as you I don't understand since I use the same x-values
    – Valentin Semur
    Nov 7 at 9:07












  • a barplot is for categorical data, and uses sequential ordinal x-positions for each category (0,1,2,...). But the labels (in your case) are numerical and not starting at zero. seaborn doesn't allow specification of the x-positions in barplots. See seaborn.pydata.org/tutorial/categorical.html. In the lineplot, the x-positions are defined by the first argument, i.e., 'week'.
    – Bonlenfum
    Nov 7 at 9:37










  • if it is essential to add these two types of plot on one figure, you might be better off using matplotlib. See python-graph-gallery.com/5-control-width-and-space-in-barplots for an example of how to set x positions in a bar plot. But perhaps you can just rethink how to show your data (e.g. with 2 subplots)
    – Bonlenfum
    Nov 7 at 9:44












  • Thank you for your help, using matplotlib instead of seabron seems to work fine.
    – Valentin Semur
    Nov 8 at 1:00


















  • I am surprised the line is not on top of the bar chart already since you specify the same x-values for the bar- and the lineplot. Could you post a few lines of the membership data?
    – onno
    Nov 7 at 8:33










  • I just edited the post, thank you for trying to help me out. Same as you I don't understand since I use the same x-values
    – Valentin Semur
    Nov 7 at 9:07












  • a barplot is for categorical data, and uses sequential ordinal x-positions for each category (0,1,2,...). But the labels (in your case) are numerical and not starting at zero. seaborn doesn't allow specification of the x-positions in barplots. See seaborn.pydata.org/tutorial/categorical.html. In the lineplot, the x-positions are defined by the first argument, i.e., 'week'.
    – Bonlenfum
    Nov 7 at 9:37










  • if it is essential to add these two types of plot on one figure, you might be better off using matplotlib. See python-graph-gallery.com/5-control-width-and-space-in-barplots for an example of how to set x positions in a bar plot. But perhaps you can just rethink how to show your data (e.g. with 2 subplots)
    – Bonlenfum
    Nov 7 at 9:44












  • Thank you for your help, using matplotlib instead of seabron seems to work fine.
    – Valentin Semur
    Nov 8 at 1:00
















I am surprised the line is not on top of the bar chart already since you specify the same x-values for the bar- and the lineplot. Could you post a few lines of the membership data?
– onno
Nov 7 at 8:33




I am surprised the line is not on top of the bar chart already since you specify the same x-values for the bar- and the lineplot. Could you post a few lines of the membership data?
– onno
Nov 7 at 8:33












I just edited the post, thank you for trying to help me out. Same as you I don't understand since I use the same x-values
– Valentin Semur
Nov 7 at 9:07






I just edited the post, thank you for trying to help me out. Same as you I don't understand since I use the same x-values
– Valentin Semur
Nov 7 at 9:07














a barplot is for categorical data, and uses sequential ordinal x-positions for each category (0,1,2,...). But the labels (in your case) are numerical and not starting at zero. seaborn doesn't allow specification of the x-positions in barplots. See seaborn.pydata.org/tutorial/categorical.html. In the lineplot, the x-positions are defined by the first argument, i.e., 'week'.
– Bonlenfum
Nov 7 at 9:37




a barplot is for categorical data, and uses sequential ordinal x-positions for each category (0,1,2,...). But the labels (in your case) are numerical and not starting at zero. seaborn doesn't allow specification of the x-positions in barplots. See seaborn.pydata.org/tutorial/categorical.html. In the lineplot, the x-positions are defined by the first argument, i.e., 'week'.
– Bonlenfum
Nov 7 at 9:37












if it is essential to add these two types of plot on one figure, you might be better off using matplotlib. See python-graph-gallery.com/5-control-width-and-space-in-barplots for an example of how to set x positions in a bar plot. But perhaps you can just rethink how to show your data (e.g. with 2 subplots)
– Bonlenfum
Nov 7 at 9:44






if it is essential to add these two types of plot on one figure, you might be better off using matplotlib. See python-graph-gallery.com/5-control-width-and-space-in-barplots for an example of how to set x positions in a bar plot. But perhaps you can just rethink how to show your data (e.g. with 2 subplots)
– Bonlenfum
Nov 7 at 9:44














Thank you for your help, using matplotlib instead of seabron seems to work fine.
– Valentin Semur
Nov 8 at 1:00




Thank you for your help, using matplotlib instead of seabron seems to work fine.
– Valentin Semur
Nov 8 at 1:00












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










I struggled a lot with this one. First I tried to plot using the plot methods from pandas but I could not manage to get the line graph on top of the bar graph. So I used the standard matplotlib functions.



memberships = pd.DataFrame(data={'week':range(37,44), 
'group1':[248, 243, 287, 354, 315, 348, 303],
'group2':[42,53,65,73,60,62,84]})

fig, ax = plt.subplots()
ax.bar(memberships.week-0.5, memberships.group1, color = '#E30613')
ax.bar(memberships.week-0.5, memberships.group2, color = '#009FE3', bottom=memberships.group1)
ax.plot(memberships.week, memberships.group2, linewidth=4)





share|improve this answer





















  • I'm gonna go with that, than you very much! (I just removed the -0.5 you added to the weeks, because it looks better in my opinion)
    – Valentin Semur
    Nov 8 at 1:00













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',
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
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53185527%2fhow-to-add-a-line-to-a-bar-chart%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










I struggled a lot with this one. First I tried to plot using the plot methods from pandas but I could not manage to get the line graph on top of the bar graph. So I used the standard matplotlib functions.



memberships = pd.DataFrame(data={'week':range(37,44), 
'group1':[248, 243, 287, 354, 315, 348, 303],
'group2':[42,53,65,73,60,62,84]})

fig, ax = plt.subplots()
ax.bar(memberships.week-0.5, memberships.group1, color = '#E30613')
ax.bar(memberships.week-0.5, memberships.group2, color = '#009FE3', bottom=memberships.group1)
ax.plot(memberships.week, memberships.group2, linewidth=4)





share|improve this answer





















  • I'm gonna go with that, than you very much! (I just removed the -0.5 you added to the weeks, because it looks better in my opinion)
    – Valentin Semur
    Nov 8 at 1:00

















up vote
0
down vote



accepted










I struggled a lot with this one. First I tried to plot using the plot methods from pandas but I could not manage to get the line graph on top of the bar graph. So I used the standard matplotlib functions.



memberships = pd.DataFrame(data={'week':range(37,44), 
'group1':[248, 243, 287, 354, 315, 348, 303],
'group2':[42,53,65,73,60,62,84]})

fig, ax = plt.subplots()
ax.bar(memberships.week-0.5, memberships.group1, color = '#E30613')
ax.bar(memberships.week-0.5, memberships.group2, color = '#009FE3', bottom=memberships.group1)
ax.plot(memberships.week, memberships.group2, linewidth=4)





share|improve this answer





















  • I'm gonna go with that, than you very much! (I just removed the -0.5 you added to the weeks, because it looks better in my opinion)
    – Valentin Semur
    Nov 8 at 1:00















up vote
0
down vote



accepted







up vote
0
down vote



accepted






I struggled a lot with this one. First I tried to plot using the plot methods from pandas but I could not manage to get the line graph on top of the bar graph. So I used the standard matplotlib functions.



memberships = pd.DataFrame(data={'week':range(37,44), 
'group1':[248, 243, 287, 354, 315, 348, 303],
'group2':[42,53,65,73,60,62,84]})

fig, ax = plt.subplots()
ax.bar(memberships.week-0.5, memberships.group1, color = '#E30613')
ax.bar(memberships.week-0.5, memberships.group2, color = '#009FE3', bottom=memberships.group1)
ax.plot(memberships.week, memberships.group2, linewidth=4)





share|improve this answer












I struggled a lot with this one. First I tried to plot using the plot methods from pandas but I could not manage to get the line graph on top of the bar graph. So I used the standard matplotlib functions.



memberships = pd.DataFrame(data={'week':range(37,44), 
'group1':[248, 243, 287, 354, 315, 348, 303],
'group2':[42,53,65,73,60,62,84]})

fig, ax = plt.subplots()
ax.bar(memberships.week-0.5, memberships.group1, color = '#E30613')
ax.bar(memberships.week-0.5, memberships.group2, color = '#009FE3', bottom=memberships.group1)
ax.plot(memberships.week, memberships.group2, linewidth=4)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 7 at 9:39









onno

49726




49726












  • I'm gonna go with that, than you very much! (I just removed the -0.5 you added to the weeks, because it looks better in my opinion)
    – Valentin Semur
    Nov 8 at 1:00




















  • I'm gonna go with that, than you very much! (I just removed the -0.5 you added to the weeks, because it looks better in my opinion)
    – Valentin Semur
    Nov 8 at 1:00


















I'm gonna go with that, than you very much! (I just removed the -0.5 you added to the weeks, because it looks better in my opinion)
– Valentin Semur
Nov 8 at 1:00






I'm gonna go with that, than you very much! (I just removed the -0.5 you added to the weeks, because it looks better in my opinion)
– Valentin Semur
Nov 8 at 1:00




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53185527%2fhow-to-add-a-line-to-a-bar-chart%23new-answer', 'question_page');
}
);

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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini