简体   繁体   中英

how to change limit between axis range in seaborn.relplot

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt

d = {'x-axis':[100,915,298,299], 'y-axis': [1515,1450,1313,1315],
     'text':['point1','point2','point3','point4']}
df = pd.DataFrame(d)

p1 = sns.relplot(x='x-axis', y='y-axis',data=df )
ax = p1.axes[0,0]
for idx,row in df.iterrows():
    x = row[0]
    y = row[1]
    text = row[2]
    ax.text(x+.05,y,text, horizontalalignment='left')

plt.show()

Here How I can decide x-axis range. I mean I want that x-axis should be 0 50 100 150 etc and I want yaxis to be 0 500 1000 1500 etc and also I make sure that both axes are starting from 0

You can do the following - using the line

p1.set(xticks=[i for i in range(0, max(df['x-axis']) + 50, 50)],
       yticks=[i for i in range(0, max(df['y-axis']) + 500, 500)])

to dynamically set the ticks.

from matplotlib import pyplot as plt
import pandas as pd
import seaborn as sns

d = {'x-axis':[100,915,298,299], 'y-axis': [1515,1450,1313,1315],
     'text':['point1','point2','point3','point4']}
df = pd.DataFrame(d)

p1 = sns.relplot(x='x-axis', y='y-axis',data=df )
ax = p1.axes[0,0]
for idx,row in df.iterrows():
    x = row[0]
    y = row[1]
    text = row[2]
    ax.text(x+.05,y,text, horizontalalignment='left')

p1.set(xticks=[i for i in range(0, max(df['x-axis']) + 50, 50)],
       yticks=[i for i in range(0, max(df['y-axis']) + 500, 500)])


plt.show()

To increase the height and width of the plot so no points overlap, just alter this line from

p1 = sns.relplot(x='x-axis', y='y-axis', data=df)

to

p1 = sns.relplot(x='x-axis', y='y-axis', data=df, height=10, aspect=1)

You can change height and aspect as you see fit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM