S.Slusky
S.Slusky

Reputation: 242

Seaborn X axis Categorical data

I have a seaborn plot that I would like to have 35 evenly spaced categories. My problem is that the categories are numbers that are not evenly spaced. Note their datatype is set to category.

Categories = [8.01, 8.02, 8.03, 8.04, 8.05, 8.11, 8.12, 8.13, 8.14, 8.15, 8.16, 9.01, 9.02, 9.03, 9.04, 9.05, 9.06, 9.1, 9.11, 9.12, 9.13, 9.14, 9.15, 10.01, 10.02, 10.03, 10.04, 10.05, 10.06, 10.11, 10.12, 10.13, 10.14, 10.15, 10.16]

If I try and set my xticks = Categories, I get them bunch up like this: Bunched Up ticks

Here's my code, I've tried converting the values in Categories to strings but that doesn't seem to solve my issue. Any suggestions would be welcome.

fig, ax = plt.subplots(figsize = (16,8))
ax.set_title("Avg Kills Comparions Per Patch")
ax.set_ylabel("Avg Kills")
ax.set_xlabel("Patch")
ax.set_xticks(Categories)

p1 = sns.lineplot(ax=ax, x='patch', y='avg_kills', data=stat_data, hue='player')

I'd essentially like to stretch out the clusters of data here, but I haven't been able to figure out how to do that.

EDIT: Here is some sample data:

patch,player,avg_kills
8.01,Solo,0.5000
8.02,Solo,1.5000
8.03,Solo,2.7500
8.04,Solo,0.5000
8.05,Solo,1.4375
8.11,Solo,1.0000
8.12,Solo,2.5000
8.13,Solo,1.7500
8.14,Solo,1.7500
8.15,Solo,2.5000
9.01,Solo,2.5000
9.02,Solo,2.5000
9.03,Solo,1.7500
9.04,Solo,1.0000
9.05,Solo,3.5000
9.06,Solo,2.7500
9.1,Solo,3.0000
9.11,Solo,3.2500
9.12,Solo,1.0000
9.13,Solo,1.5000
9.14,Solo,1.5000
10.05,Solo,2.0000
10.06,Solo,2.2632
10.11,Solo,3.0000
10.12,Solo,2.0000
10.13,Solo,1.7500
10.14,Solo,1.7500
10.15,Solo,3.7500
10.16,Solo,3.2000
8.01,TopLaneAvg,2.4500
8.02,TopLaneAvg,1.9500
8.03,TopLaneAvg,1.8500
8.04,TopLaneAvg,1.7250
8.05,TopLaneAvg,1.7703
8.11,TopLaneAvg,2.1111
8.12,TopLaneAvg,3.0750
8.13,TopLaneAvg,2.5750
8.14,TopLaneAvg,2.1250
8.15,TopLaneAvg,1.8958
8.16,TopLaneAvg,1.9400
9.01,TopLaneAvg,2.3500
9.02,TopLaneAvg,2.2250
9.03,TopLaneAvg,2.0250
9.04,TopLaneAvg,1.7250
9.05,TopLaneAvg,2.0238
9.06,TopLaneAvg,3.0455
9.1,TopLaneAvg,2.9000
9.11,TopLaneAvg,2.3750
9.12,TopLaneAvg,2.4250
9.13,TopLaneAvg,2.3750
9.14,TopLaneAvg,2.5435
9.15,TopLaneAvg,2.6000
10.01,TopLaneAvg,2.2000
10.02,TopLaneAvg,2.1750
10.03,TopLaneAvg,1.8250
10.04,TopLaneAvg,1.7750
10.05,TopLaneAvg,2.4000
10.06,TopLaneAvg,2.3667
10.11,TopLaneAvg,1.5000
10.12,TopLaneAvg,2.1500
10.13,TopLaneAvg,2.4750
10.14,TopLaneAvg,1.9500
10.15,TopLaneAvg,2.5238
10.16,TopLaneAvg,2.3571
'''

Upvotes: 2

Views: 4708

Answers (1)

Trenton McKinney
Trenton McKinney

Reputation: 62383

  • Even when patch is set to categorical the values are still interpreted as numbers by the plot API, so convert them to a str type.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# given your dataframe as df

# sort values by patch
df.sort_values('patch', inplace=True, ascending=True)

# convert to string
df.patch = df.patch.astype(str)

# set as Categorical and Categories as strings
df.patch = pd.Categorical(df.patch, categories=map(str, Categories), ordered=True)

# plot
fig, ax = plt.subplots(figsize = (16,8))
p1 = sns.lineplot(ax=ax, x='patch', y='avg_kills', data=df, hue='player')

enter image description here

Upvotes: 4

Related Questions