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










share|improve this question
























  • 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















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










share|improve this question
























  • 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













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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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

















active

oldest

votes











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%2f53234969%2fhow-to-plot-two-categories-across-years-as-a-bar-comparison%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Schultheiß

Android Play Services Check

Liste der Kulturdenkmale in Wilsdruff