Pandas TypeError: no numeric data to plot [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
4 min

banner

# Table of Contents

  1. Pandas TypeError: no numeric data to plot
  2. Use the astype() method to convert the values in the columns to numeric
  3. Using the to_numeric() method to solve the error
  4. Make sure the DataFrame is not empty

# Pandas TypeError: no numeric data to plot [Solved]

The Pandas "TypeError: no numeric data to plot" occurs when you try to plot a DataFrame but your DataFrame is empty or contains no numeric columns.

To solve the error, make sure your DataFrame is not empty and use the astype() method if you need to convert the values in your columns to numeric.

Here is an example of how the error occurs.

main.py
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({ 'year': [2011, 2014, 2018, 2012, 2025], 'net_worth': ['5000', '10000', '15000', '10000', '20000'], 'gdp_per_capita': ['10000', '12000', '15000', '20000', '25000'], }) print(df[['net_worth', 'gdp_per_capita']].plot()) # ⛔️ TypeError: no numeric data to plot plt.show()

type error no numeric data to plot

Notice that the values in the columns we tried to call DataFrame.plot are not numeric.

# Use the astype() method to convert the values in the columns to numeric

If your DataFrame is not empty, use the DataFrame.astype method to convert the values in the columns you want to plot to numeric.

main.py
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({ 'year': [2011, 2014, 2018, 2012, 2025], 'net_worth': ['5000', '10000', '15000', '10000', '20000'], 'gdp_per_capita': ['10000', '12000', '15000', '20000', '25000'], }) df = df.astype(float) print(df[['net_worth', 'gdp_per_capita']].plot()) plt.show()

convert column values to numeric before plotting

The code for this article is available on GitHub

Make sure you have the matplotlib module installed to be able to run the code sample.

The DataFrame.astype() method converts the given Pandas object to the specified data type.

You can also call the method on specific DataFrame columns.

main.py
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({ 'year': [2011, 2014, 2018, 2012, 2025], 'net_worth': ['5000', '10000', '15000', '10000', '20000'], 'gdp_per_capita': ['10000', '12000', '15000', '20000', '25000'], }) df['net_worth'] = df['net_worth'].astype(float) df['gdp_per_capita'] = df['gdp_per_capita'].astype(float) print(df[['net_worth', 'gdp_per_capita']].plot()) plt.show()

call astype method on specific columns

The code for this article is available on GitHub

Once the values in the columns you want to plot have been converted to numeric, you will be able to call the DataFrame.plot() method without any issues.

You can use the DataFrame.dtypes attribute to verify that the column values are numeric.

main.py
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({ 'year': [2011, 2014, 2018, 2012, 2025], 'net_worth': ['5000', '10000', '15000', '10000', '20000'], 'gdp_per_capita': ['10000', '12000', '15000', '20000', '25000'], }) print(df.dtypes) df['net_worth'] = df['net_worth'].astype(float) df['gdp_per_capita'] = df['gdp_per_capita'].astype(float) print('-' * 50) print(df.dtypes)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
year int64 net_worth object gdp_per_capita object dtype: object -------------------------------------------------- year int64 net_worth float64 gdp_per_capita float64 dtype: object

verify the values are numeric

Accessing the dtypes attribute returns "object" for the two columns before using astype().

After converting the values in the two columns to floating-point numbers, float64 is returned.

# Using the to_numeric() method to solve the error

You can also use the pandas.to_numeric() method to solve the error.

main.py
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({ 'year': [2011, 2014, 2018, 2012, 2025], 'net_worth': ['5000', '10000', '15000', '10000', '20000'], 'gdp_per_capita': ['10000', '12000', '15000', '20000', '25000'], }) df['net_worth'] = pd.to_numeric(df['net_worth']) df['gdp_per_capita'] = pd.to_numeric(df['gdp_per_capita']) print(df[['net_worth', 'gdp_per_capita']].plot()) plt.show()
The code for this article is available on GitHub

The pandas.to_numeric method converts the given argument to a numeric type.

# Make sure the DataFrame is not empty

You will also get the error if you try to plot an empty DataFrame.

main.py
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({}) df.plot() # ⛔️ TypeError: no numeric data to plot plt.show()

trying to plot empty dataframe

The DataFrame has to have at least 1 numeric column for you to be able to use the plot() method.

main.py
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({ 'net_worth': ['5000', '10000', '15000', '10000', '20000'], }) df['net_worth'] = df['net_worth'].astype(float) df.plot() plt.show()

need at least 1 numeric column to be able to plot

The code for this article is available on GitHub

You can also add a column to an empty DataFrame after it has been created.

main.py
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({}) df['net_worth'] = [5000, 10000, 15000, 10000, 20000] df.plot() plt.show()

need at least 1 numeric column to be able to plot

I've also written an article on how to add axis labels to a plot in Pandas.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.