Ksenia
Ksenia

Reputation: 59

Making a scatter plot with string in x axis in pandas

I have two columns: genres (more than 10 unique) and price and I want to compare them using scatter plot. How can I write string names in x axis?

price 2.99 3.54 7.00 and so on

genre fiction drama psychology and so on

Another option: convert string to category and somehow convert category numbers to string during graphing. Is it possible?

df['genre'] = df['genre'].astype("category").cat.codes 
Plot = df.plot.scatter(x='genre',
                      y='price',
                      c='DarkBlue')

Upvotes: 1

Views: 982

Answers (2)

Ragıp Gürlek
Ragıp Gürlek

Reputation: 160

You can leave the genre column as object, aka string, Dtype in pandas and plot it. Try running just the line below.

df.plot.scatter(x='genre', y='price', c='DarkBlue')

Here is a working example.

import seaborn as sns
iris = sns.load_dataset('iris')

iris.plot.scatter("species", "sepal_length")

Upvotes: 2

YKerem
YKerem

Reputation: 82

I think you are asking about axis labels?

if so it's as simple as this

plot.xlabel("Genre")

Upvotes: 0

Related Questions