Last updated: Apr 12, 2024
Reading time·4 min
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.
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()
Notice that the values in the columns we tried to call DataFrame.plot are not numeric.
astype()
method to convert the values in the columns to numericIf your DataFrame
is not empty, use the
DataFrame.astype
method to convert the values in the columns you want to plot to numeric.
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()
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.
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()
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.
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)
Running the code sample produces the following output.
year int64 net_worth object gdp_per_capita object dtype: object -------------------------------------------------- year int64 net_worth float64 gdp_per_capita float64 dtype: object
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.
to_numeric()
method to solve the errorYou can also use the pandas.to_numeric() method to solve the error.
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 pandas.to_numeric
method converts the given argument to a numeric type.
You will also get the error if you try to plot an empty DataFrame
.
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({}) df.plot() # ⛔️ TypeError: no numeric data to plot plt.show()
The DataFrame
has to have at least 1 numeric column for you to be able to use
the plot()
method.
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()
You can also add a column to an empty DataFrame
after it has been created.
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({}) df['net_worth'] = [5000, 10000, 15000, 10000, 20000] df.plot() plt.show()
I've also written an article on how to add axis labels to a plot in Pandas.
You can learn more about the related topics by checking out the following tutorials: