How to add a line to a bar chart
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
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
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 themembership
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
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
python graph seaborn data-analysis
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 themembership
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
add a comment |
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 themembership
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
add a comment |
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)
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
add a comment |
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)
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
add a comment |
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)
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
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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%2f53185527%2fhow-to-add-a-line-to-a-bar-chart%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
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