Home > OS >  Seaborn manually set interval of x axis
Seaborn manually set interval of x axis

Time:01-08

My data:

data = pd.DataFrame({'y': {0: 0.8285, 1: 0.869, 2: 0.8781, 3: 0.8806, 4: 0.8825, 5: 0.8831},'x': {0: 5764, 1: 22021, 2: 56906, 3: 114157, 4: 289474, 5: 4755584}})

My current plot:

sns.set(rc={'figure.figsize':(6,3.5)}, style="white")

ax = sns.lineplot(data=data, x="x", y="y", dashes=False, markersize=8, marker="o")

ax.grid(axis='x', linestyle='dotted')

ax.set_xlabel("X", fontsize=16)
ax.set_ylabel("Y", fontsize=16)

enter image description here

As you can see, the curve is too sharp. Since there are 5 points from x=0 to x=1, I want to expand the interval from x=0 to x=1 and squeeze the interval from x=1 to x=4. A prefered plot looks like this:

enter image description here

where the x axis is uneven and I can manually set its interval.

How to do this? Thanks in advance.

CodePudding user response:

It looks like you want to create a graph with "fake" x values, you can certainly do this but this is misleading at best, or unethical/fraudulent in some cases (e.g. scientific data).

You can use:

import seaborn as sns

# set up fake data
data['fake_x'] = [1.5, 2, 2.5, 3, 5, 7]

ax = sns.lineplot(data=data, x="fake_x", y="y", dashes=False, markersize=8, marker="o")

ax.grid(axis='x', linestyle='dotted')
ax.set_xlabel("fake X", fontsize=16)
ax.set_ylabel("Y", fontsize=16)

# change xticks
ax.set_xticks(data['fake_x'], data['x'])

Output:

enter image description here

  • Related