0

I have a csv file with strings as x-axis. Now i have to make a scatter plot using matplotlib and pandas. But its showing error "scatter plots require number in x axis"

I tried reading the file as a variable df and scatter plotted it.But it isnt able is to read strings. Its a huge file and i cant define string variables as x = ["x1","x2",...etc]

*There are also multiple values for the same x axis id.

import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv("scatter2")
ax = plt.gca()
df.plot(kind='scatter',x='Spectral Type',y='Hα EW',ax=ax)

The error message: ...ValueError: scatter requires x column to be numeric

1
  • Matplotlib should be able to plot this like ax.scatter(x='Spectral Type',y='Hα EW', data=df). Commented Apr 12, 2019 at 20:53

1 Answer 1

0

Your x values have to be a normal range list (e.g. something like list(range(len(x)))) and then you just set your ticks to the desired value (something like plt.xticks(x_range_values, x_str_values)).

Have a look at Python Matplotlib - how to specify values on y axis? for a bit more information

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.