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()
在此如何确定x轴范围。我的意思是我希望x轴应为0 50 100 150等,我希望yaxis为0 500 1000 1500等,并且还要确保两个轴都从0开始
答案 0 :(得分:0)
您可以执行以下操作-使用一行
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)])
动态设置刻度线。
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()
要增加绘图的高度和宽度,以使没有点重叠,只需更改此线即可
p1 = sns.relplot(x='x-axis', y='y-axis', data=df)
到
p1 = sns.relplot(x='x-axis', y='y-axis', data=df, height=10, aspect=1)
您可以根据需要更改height
和aspect
。