0

I have following set of data in CSV format

Name    Place
A   India
B   USA
C   India
D   USA
E   China
F   UK
G   Canada
H   China
I   UK
J   UK

Following is the code I used,

output2.to_csv("Place.csv", index = False)
pd.options.display.mpl_style = 'default'
place_plot=output2.plot(kind="bar",x=output2["Place"],title="Count",legend=False)
fig = place_plot.get_figure() 

Outout2 -> CSV file.

I need to plot a graph using Python so that it will show the count of people from different country, as shown in the picture

count

How do I do it?

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • I think you forgot to include a problem description (see [ask]) in your question. What you want is a *bar chart*. Searching for this gives you a huge amount of examples. Without you explaining in detail what the problem of implementing any of those is, this question should probably be closed. – ImportanceOfBeingErnest Apr 18 '17 at 14:21
  • Possible duplicate of [Bar Chart Using Matplotlib in Python](http://stackoverflow.com/questions/42895602/bar-chart-using-matplotlib-in-python) – ImportanceOfBeingErnest Apr 18 '17 at 14:23

1 Answers1

0

I guess that what you want is this:

output2.Place.value_counts().plot(kind="bar")

assuming output2 is a Pandas Dataframe, which is not clear from your question.

jrjc
  • 21,103
  • 9
  • 64
  • 78
  • No its not a Dataframe, output2 is a CSV file where the above data is present. I need to plot a Bar graph(repeated place) for "Place" column in the output2.csv file. – Skanda Udupa Apr 19 '17 at 11:33
  • well it looks like a DataFrame. I don't know other object having the `.to_csv` and `.plot` method. And the code I propose produces exactly what you want. So, if it's really not a Dataframe, I suggest you use `output2 = pd.read_csv("Place.csv")` before the above mentioned code – jrjc Apr 19 '17 at 12:27
  • Thanks a lot for your help. Following is my code, problem is, the graph is getting closed immediately after i execute the program. Can you help me with this? import pandas import csv import pandas as pd import matplotlib df2 =pandas.read_csv('Place.csv') pd.options.display.mpl_style = 'default' place_plot= df2.Place.value_counts().plot(kind="bar",x=df2["Place"],title="Count",legend=False) fig = place_plot.get_figure() – Skanda Udupa Apr 19 '17 at 13:47
  • Edit your question, format it well, remove all unnecessary parts (`display.mpl`, `get_figure`). Your code should contain 1 line for, 1 line for reading the file, 1 line for plotting, nothing more. – jrjc Apr 19 '17 at 14:24
  • Sorry, I'm new to this. – Skanda Udupa Apr 19 '17 at 14:44
  • `df2 =pandas.read_csv('c:\\CSVTOKML\\barcharttrail1.csv')` place_plot= df2.Place.value_counts().plot(kind="bar",x=df2["Place"],title="Count",legend=False) `fig = place_plot.get_figure() ` – Skanda Udupa Apr 19 '17 at 14:57
  • Above code displays the graph for 2 seconds and graph disappears and if I dont use `fig = place_plot.get_figure()` nothing gets displayed. – Skanda Udupa Apr 19 '17 at 15:00
  • This is the comment section. Modify your initial question (at the top of the page). – jrjc Apr 19 '17 at 15:51