how to plot two categories across years as a bar comparison
up vote
0
down vote
favorite
Sample df
Output of this code
import numpy as np
import matplotlib.pyplot as plt
# data to plot
n_groups = 5
means_frank=df['crime_categories'].groupby(df.Date1.dt.year).agg('count')
#create plot
fig, ax = plt.subplots()
index = np.arange(n_groups)
bar_width = 0.35
opacity = 0.8
rects1 = plt.bar(index, means_frank, bar_width,
alpha=opacity,
color='b',
label='Violence')
rects2 = plt.bar(index + bar_width, means_frank, bar_width,
alpha=opacity,
color='g',
label='Non-Violence')
plt.xlabel('Year')
plt.ylabel('Count')
plt.title('Crime by Year')
plt.xticks(index + bar_width, ('2012','2013','2014','2015','2016'))
plt.legend()
plt.tight_layout()
plt.show()
This is what I get when I run the code
I want to display Violence and Non-Violence count across years like for 2012 the Violence is 100 and Non-Violence is 200
python matplotlib bar-chart
add a comment |
up vote
0
down vote
favorite
Sample df
Output of this code
import numpy as np
import matplotlib.pyplot as plt
# data to plot
n_groups = 5
means_frank=df['crime_categories'].groupby(df.Date1.dt.year).agg('count')
#create plot
fig, ax = plt.subplots()
index = np.arange(n_groups)
bar_width = 0.35
opacity = 0.8
rects1 = plt.bar(index, means_frank, bar_width,
alpha=opacity,
color='b',
label='Violence')
rects2 = plt.bar(index + bar_width, means_frank, bar_width,
alpha=opacity,
color='g',
label='Non-Violence')
plt.xlabel('Year')
plt.ylabel('Count')
plt.title('Crime by Year')
plt.xticks(index + bar_width, ('2012','2013','2014','2015','2016'))
plt.legend()
plt.tight_layout()
plt.show()
This is what I get when I run the code
I want to display Violence and Non-Violence count across years like for 2012 the Violence is 100 and Non-Violence is 200
python matplotlib bar-chart
Can you edit your post to include some example of the data you're using?
– Edgar R. Mondragón
Nov 10 at 1:51
I don't quite get, what you want, actually. Where is your exact problem?
– user8408080
Nov 10 at 2:03
@EdgarR.Mondragón it has been edited now please check.
– Rakesh
Nov 10 at 14:28
@user8408080 I have two categories type i.e., Violence and Non-Violence across subsequent years (2012-2016) and I want to plot the graph for each year by comparing the count of the Category type for each year
– Rakesh
Nov 10 at 14:30
You are plotting twice means_frank, is it what you want ?
– Patol75
Nov 14 at 3:38
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Sample df
Output of this code
import numpy as np
import matplotlib.pyplot as plt
# data to plot
n_groups = 5
means_frank=df['crime_categories'].groupby(df.Date1.dt.year).agg('count')
#create plot
fig, ax = plt.subplots()
index = np.arange(n_groups)
bar_width = 0.35
opacity = 0.8
rects1 = plt.bar(index, means_frank, bar_width,
alpha=opacity,
color='b',
label='Violence')
rects2 = plt.bar(index + bar_width, means_frank, bar_width,
alpha=opacity,
color='g',
label='Non-Violence')
plt.xlabel('Year')
plt.ylabel('Count')
plt.title('Crime by Year')
plt.xticks(index + bar_width, ('2012','2013','2014','2015','2016'))
plt.legend()
plt.tight_layout()
plt.show()
This is what I get when I run the code
I want to display Violence and Non-Violence count across years like for 2012 the Violence is 100 and Non-Violence is 200
python matplotlib bar-chart
Sample df
Output of this code
import numpy as np
import matplotlib.pyplot as plt
# data to plot
n_groups = 5
means_frank=df['crime_categories'].groupby(df.Date1.dt.year).agg('count')
#create plot
fig, ax = plt.subplots()
index = np.arange(n_groups)
bar_width = 0.35
opacity = 0.8
rects1 = plt.bar(index, means_frank, bar_width,
alpha=opacity,
color='b',
label='Violence')
rects2 = plt.bar(index + bar_width, means_frank, bar_width,
alpha=opacity,
color='g',
label='Non-Violence')
plt.xlabel('Year')
plt.ylabel('Count')
plt.title('Crime by Year')
plt.xticks(index + bar_width, ('2012','2013','2014','2015','2016'))
plt.legend()
plt.tight_layout()
plt.show()
This is what I get when I run the code
I want to display Violence and Non-Violence count across years like for 2012 the Violence is 100 and Non-Violence is 200
python matplotlib bar-chart
python matplotlib bar-chart
edited Nov 10 at 5:53
asked Nov 10 at 0:32
Rakesh
12
12
Can you edit your post to include some example of the data you're using?
– Edgar R. Mondragón
Nov 10 at 1:51
I don't quite get, what you want, actually. Where is your exact problem?
– user8408080
Nov 10 at 2:03
@EdgarR.Mondragón it has been edited now please check.
– Rakesh
Nov 10 at 14:28
@user8408080 I have two categories type i.e., Violence and Non-Violence across subsequent years (2012-2016) and I want to plot the graph for each year by comparing the count of the Category type for each year
– Rakesh
Nov 10 at 14:30
You are plotting twice means_frank, is it what you want ?
– Patol75
Nov 14 at 3:38
add a comment |
Can you edit your post to include some example of the data you're using?
– Edgar R. Mondragón
Nov 10 at 1:51
I don't quite get, what you want, actually. Where is your exact problem?
– user8408080
Nov 10 at 2:03
@EdgarR.Mondragón it has been edited now please check.
– Rakesh
Nov 10 at 14:28
@user8408080 I have two categories type i.e., Violence and Non-Violence across subsequent years (2012-2016) and I want to plot the graph for each year by comparing the count of the Category type for each year
– Rakesh
Nov 10 at 14:30
You are plotting twice means_frank, is it what you want ?
– Patol75
Nov 14 at 3:38
Can you edit your post to include some example of the data you're using?
– Edgar R. Mondragón
Nov 10 at 1:51
Can you edit your post to include some example of the data you're using?
– Edgar R. Mondragón
Nov 10 at 1:51
I don't quite get, what you want, actually. Where is your exact problem?
– user8408080
Nov 10 at 2:03
I don't quite get, what you want, actually. Where is your exact problem?
– user8408080
Nov 10 at 2:03
@EdgarR.Mondragón it has been edited now please check.
– Rakesh
Nov 10 at 14:28
@EdgarR.Mondragón it has been edited now please check.
– Rakesh
Nov 10 at 14:28
@user8408080 I have two categories type i.e., Violence and Non-Violence across subsequent years (2012-2016) and I want to plot the graph for each year by comparing the count of the Category type for each year
– Rakesh
Nov 10 at 14:30
@user8408080 I have two categories type i.e., Violence and Non-Violence across subsequent years (2012-2016) and I want to plot the graph for each year by comparing the count of the Category type for each year
– Rakesh
Nov 10 at 14:30
You are plotting twice means_frank, is it what you want ?
– Patol75
Nov 14 at 3:38
You are plotting twice means_frank, is it what you want ?
– Patol75
Nov 14 at 3:38
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53234969%2fhow-to-plot-two-categories-across-years-as-a-bar-comparison%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
Can you edit your post to include some example of the data you're using?
– Edgar R. Mondragón
Nov 10 at 1:51
I don't quite get, what you want, actually. Where is your exact problem?
– user8408080
Nov 10 at 2:03
@EdgarR.Mondragón it has been edited now please check.
– Rakesh
Nov 10 at 14:28
@user8408080 I have two categories type i.e., Violence and Non-Violence across subsequent years (2012-2016) and I want to plot the graph for each year by comparing the count of the Category type for each year
– Rakesh
Nov 10 at 14:30
You are plotting twice means_frank, is it what you want ?
– Patol75
Nov 14 at 3:38