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 Answers1

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

Ido_f
  • 689
  • 1
  • 11
  • 29