为什么设置x轴范围后我的图形变小了?

0 投票
1 回答
60 浏览
提问于 2025-04-14 17:31

为什么当我尝试设置x轴范围时,图表都缩到左边去了?我的x轴最大值其实是70,但我想让图表的范围到x=90(也就是说,从x=70x=90,这部分应该是空白的)。

# plotting
xmin = 0
xmax = 90
ymin = 0
ymax = 100

plt.figure(linewidth = 2.5)
plt.figure(figsize = (15,10))
ax = plt.subplots()

ax = sns.pointplot(data = df, 
               x = "Time",
               y = "Percent", 
               hue = "Concentration", 
               errorbar = "se", 
               scale = 0.7, 
               errwidth=1)

plt.xlabel("Time", fontsize = '12')
plt.ylabel("Percent Activity", fontsize = '12')
plt.ylim(ymin, ymax)
plt.xlim(xmin, xmax)
# xticks = np.arange(0, 100, 10)
# ax.set_xticks(xticks, labels=xticks)

ax.legend(title = 'Concentration(uM)', 
      loc = 'lower left', 
      title_fontsize='7',
      fontsize ='7')

plt.show()

我尝试设置xticks,但没有效果。图表还是缩小了。

这是我的数据:

enter image description here

按照建议,我尝试了df.describe()来查看df的情况。结果是这样的:

enter image description here

1 个回答

1
ax = sns.pointplot(data=df, 
                   x="Time",
                   y="Percent", 
                   hue="Concentration", 
                   errorbar="see", 
                   scale=0.7,
                   errwidth=1,
                   native_scale=True    # <---------                 
                   )

sns.pointplot这个函数会把x轴的数据变成一串连续的整数列表。比如你输入的是0, 10, ..., 100,结果会变成0, 1, ..., 10,每个整数代表一个样本。如果你开启“native_scale”选项,应该可以让它恢复到原来的样子,具体可以参考这个链接

native_scale(布尔值)

当设置为True时,分类轴上的数字或日期时间值会保持原来的比例,而不是被转换成固定的索引。

注意:默认情况下,比例会根据你有多少个样本而变化,而不管你实际输入的x值是什么。换句话说,如果没有开启native_scale,输出的x值是依赖于样本数量的,而不是你输入的x值。

这个问题和使用seaborn绘图时改变坐标轴有关,可能会给你一些帮助,虽然它不是重复的问题;可以参考这个链接:如何设置seaborn pointplot的x轴范围?

撰写回答