How to Fix: TypeError: no numeric data to plot

28 Nov, 2021
In this article, we will fix the error: TypeError: no numeric data to plot
Python3
# importing pandasimport pandas as pd# importing numpyimport numpy as npimport matplotlib.pyplot as plt  petal_length = ['3.3', '3.5', '4.0', '4.5',                '4.6', '5.0', '5.5', '6.0',                 '6.5', '7.0']petal_width = ['3.6', '3.8', '4.4', '6.6',               '6.8', '7.0', '7.5', '8.0',                '8.5', '8.9']    df = pd.DataFrame({'petal_length(cm)': petal_length,                   'petal_width(cm)': petal_width})df.plot(x='petal_length(cm)', y='petal_width(cm)')plt.show()
Output:
TypeError: no numeric data to plot
Plotting can be done only on numeric data when we plot the data with datatype different that numeric data this error raises. To know whether the data types are numeric or not we can know it by using the function dtypes().
print(df.dtypes)
The data that we use to plot must be numeric.
This error can be fixed by converting the data to be plotted into numeric data. To convert the data to numeric data we can use the functions astype() or to_numeric().
Syntax:
df['column_name']= df['column_name'].astype(data_type)
where, df is the input dataframe
Example:
Python3
# importing pandasimport pandas as pd# importing numpyimport numpy as np# importing matplotlib.pyplotimport matplotlib.pyplot as plt  petal_length = ['3.3', '3.5', '4.0', '4.5',                '4.6', '5.0', '5.5', '6.0',                 '6.5', '7.0']petal_width = ['3.6', '3.8', '4.4', '6.6',               '6.8', '7.0', '7.5', '8.0',               '8.5', '8.9']    df = pd.DataFrame({'petal_length(cm)': petal_length,                   'petal_width(cm)': petal_width})df['petal_length(cm)'] = df['petal_length(cm)'].astype(float)df['petal_width(cm)'] = df['petal_width(cm)'].astype(float)  df.plot(x='petal_length(cm)', y='petal_width(cm)')plt.show()
Output:
Syntax:
df['column_name'] = pd.to_numeric(df['column_name'])
where df is the input dataframe
Example:
Python3
# importing pandasimport pandas as pd# importing numpyimport numpy as np# importing matplotlib.pyplotimport matplotlib.pyplot as plt  petal_length = ['3.3', '3.5', '4.0', '4.5',                '4.6', '5.0', '5.5', '6.0',                '6.5', '7.0']petal_width = ['3.6', '3.8', '4.4', '6.6',               '6.8', '7.0', '7.5', '8.0',                '8.5', '8.9']    df = pd.DataFrame({'petal_length(cm)': petal_length,                   'petal_width(cm)': petal_width})# Using to_numeric() functiondf['petal_length(cm)'] = pd.to_numeric(df['petal_length(cm)'])df['petal_width(cm)'] = pd.to_numeric(df['petal_width(cm)'])  df.plot(x='petal_length(cm)', y='petal_width(cm)')plt.show()
Output:
Picked
Python How-to-fix
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
How to drop one or multiple columns in Pandas Dataframe
Python | os.path.join() method
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python | Get unique values from a list
Python | datetime.timedelta() function