0

I have a problem. I have a dataframe df. I want to plot a scatterplot with the help of seaborn. But I want to change the x- axis. I would like the x-axis to be finer from 4.0 to 5.0. The distances should be smaller, e.g. 4.1, 4.2 or even finer. How can I set so that the x-axis is displayed finer, so that I can see the values from 4.0 better?

I looked at seaborn, pylab - changing xticks from float to int , How to change the X axis range in seaborn in python?

d = {'review_scores_accuracy': [1.1, 2.0, 4.5, 5.0, 4.9, 4.8, 4.7], 
     'review_scores_rating': [1.1, 2.0, 4.6, 3.9, 4.2, 4.5, 4.2]}
df = pd.DataFrame(data=d)


fig, ax = plt.subplots(figsize=(20,10))
sns.scatterplot(data=df, x="review_scores_rating", y="review_scores_accuracy", ax = ax ) 
# ax.set_xlim(1,5)
# ax.set_xticks(1,2,3,4,4.1,4.2)
plt.show()
Test
  • 571
  • 13
  • 32

1 Answers1

1

Use matplotlib.ticker, as per here

import pandas as pd
import seaborn as sns
import matplotlib.ticker as ticker

d = {'review_scores_accuracy': [1.1, 2.0, 4.5, 5.0, 4.9, 4.8, 4.7], 
 'review_scores_rating': [1.1, 2.0, 4.6, 3.9, 4.2, 4.5, 4.2]}
df = pd.DataFrame(data=d)

fig, ax = plt.subplots(figsize=(20,10))
sns.scatterplot(data=df, x="review_scores_rating",y="review_scores_accuracy", ax=ax) 
ax.xaxis.set_major_locator(ticker.MultipleLocator(0.1))
plt.show()

enter image description here

Narrow your data range if you just want to plot between 4 and 5.

Ted
  • 1,189
  • 8
  • 15
  • Is there also the option to let it start only in 0.1 steps when 4.0 is but it should still start at 1.0 – Test Nov 18 '21 at 19:11
  • You'd have to specify that using something like `ax.set_xticks([1, 2, 3, 4, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5])` instead of the `ticker.MultipleLocator` line – Ted Nov 18 '21 at 19:19
  • thank you very much for your help! :) – Test Nov 18 '21 at 19:20