1

I am trying to draw a line chart from the following data. On the x-axis, I want to show the year, on the y-axis, I want to show the population of different 'Borough'. Basically, I want to draw the population of all the boroughs throughout the years. I wrote the following code to transpose the data and draw the line graph. I am getting error- "no numeric data to plot". First figure is of the transposed data, second figure is of the original data

bar_plot = bar_plot.transpose()
bar_plot
bar_plot.columns = ['NYC Total Population', 'Bronx Population', 'Brooklyn Population'   , 'Manhattan Population',   'Queens Population',    'Staten Island Population']
bar_plot.drop('Borough')
bar_plot.plot(kind = 'line', y = ['NYC Total Population',   'Bronx Population', 'Brooklyn Population'   , 'Manhattan Population',   'Queens Population',    'Staten Island Population'])

I transposed the data and changed it to this

The original dataframe looked like this

4
  • Have a look at the seaborn library which allows you to make plots easily with pandas. see: seaborn.pydata.org/generated/seaborn.lineplot.html Commented Nov 20, 2020 at 8:13
  • 1
    Could it be that your population data are in fact type category? Could you check what gives bar_plot.dtypes ? Commented Nov 20, 2020 at 8:32
  • 2
    @RuthgerRighart I just checked, my datatype of all columns was an object, I changed it to int and it's working fine now Commented Nov 20, 2020 at 8:38
  • 1
    @ShivamGupta: no problem, glad to have helped you. Commented Nov 20, 2020 at 8:46

1 Answer 1

1

There are no errors in your plot code. This error occurs if data type is category. Take care to convert and verify for numeric type using for ex.:

bar_plot = bar_plot.apply(pd.to_numeric)
bar_plot.dtypes

The following simplified code gives a lineplot:

import pandas as pd

bar_plot = pd.DataFrame({'NYC Total Population': [200, 400, 600], 'Staten Island': [30, 60, 90]}, index=[1950, 1960, 1970])

bar_plot.plot(kind = 'line', y = ['NYC Total Population', 'Staten Island'])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.