如何设置 seaborn pointplot 的 x 轴范围?

3

我创建了一个 pointplot(),但是我无法更改 x 轴限制。尽管我的数据只包含 9 个月,但我想在轴上显示所有的 12 个。

fig,ax = plt.subplots(figsize=(12,4))
sns.pointplot(data=tr_df, x='Month', y='numOfTrips', hue='Year', ax=ax, palette='nipy_spectral')
# sns.plt.xlim(0, 12) # AttributeError: module 'seaborn' has no attribute 'plt'
# ax.set_xlim=(0, 12) # does nothing
ax.set(xlim=(0, 12))
ax.set(title="Number of trips each month")

enter image description here

我做错了什么?
编辑:用于创建图表的数据。
    Year Month numOfTrips
0   2011   7     2608
1   2011   8     33579
2   2011   9     34756
3   2011   10    31423
4   2011   11    20746
5   2012   3     12240
6   2012   4     37637
7   2012   5     46056
8   2012   6     48315
9   2012   7     61659
10  2012   8     75443
11  2012   9     73012
12  2012   10    67372
13  2012   11    40862
14  2013   4     56625
15  2013   5     88105
16  2013   6     99301
17  2013   7     92504

如果您在问题中包含一些数据,那么回答您的问题会更容易 - 您能这样做吗? - William Miller
似乎代码是有效的,但问题在于如何解释数据。我正在尝试找到解决方案,但同时这可能会指引你朝正确的方向前进。我认为,pointplot默认将x轴视为分类值,因此它会忽略你的数据的实际值。 - Tacratis
3个回答

5

我个人认为,seaborn的pointplot不是你要寻找的那种图表。

我建议使用简单的lineplot,然后你尝试设置xlims的操作将按预期进行:

fig,ax = plt.subplots(figsize=(12,4))
sns.lineplot(data=tr_df, x='Month', y='numOfTrips', hue='Year', ax=ax, palette='nipy_spectral')
ax.set(xlim=(0, 12))
ax.set(title="Number of trips each month")

导致

enter image description here

但是,在这种情况下,我建议将xticks设置为具有12个值的列表,而不是0到12有13个值;-)


谢谢您的回答!您为什么认为在这种情况下“点图”不是一个好选择?我猜想这是因为“乘车次数”是定量变量,而“点图”通常用于比较分类变量? - Bn.F76
我承认我也猜测了类似的事情(正如我在第一次编辑中提到的)。但是在查看文档后,我不认为这是正确的。对我来说,“pointplot”感觉不如“lineplot”自然,并且对于您的情况,这种感觉显然指向了正确的方向,因为“pointplot”似乎处理轴的方式不同。然而,我不知道相关差异的确切内容,这导致了您的问题,很抱歉。 - SpghttCd

2

看起来问题在于您的数据仅在第3个月和第11个月之间变化。然后,月份索引从3开始,并对应于xmin。以下是一个使用一些随机数据的示例(我在您添加数据之前生成了它)。

import seaborn as sns
import pandas as pd
import numpy as np

y = [2011,2012,2013]
years = []
months = []
trips = []
np.random.seed(0)
for ii in range(27):
    years.append(y[ii / 9])
    months.append(ii % 9+3)
    trips.append(np.random.randint(0,10)+(ii / 12)*10)

tr_df = pd.DataFrame({'Month':months, 'Trips':trips, 'Year':years})
fig,ax = plt.subplots(figsize=(12,4))
sns.pointplot(data=tr_df, x='Month', y='Trips', hue='Year', ax=ax, 
              palette='nipy_spectral', scale=0.7)
ax.set(xlim=(0, 12))
ax.set(title="Number of trips each month")
plt.show()

这将产生:

enter image description here

最简单的解决方法(虽然它不能修复基础数据并且在某些情况下不起作用)是手动设置限制来考虑偏移量。
ax.set(xlim=(-0.5, 8.5))

这将会给你:

enter image description here

如果您想包括低于最小值(即0、1、2)的月份,可以手动设置xticksxticklabels
ax.set_xticks(range(-3,9))
ax.set_xticklabels(range(0,12))

这将给你:

enter image description here


1

这有点像hack,但似乎起作用了。我认为问题在于pointplot忽略了轴的数值,并将其视为序数。以下代码是手动覆盖:

fig,ax = plt.subplots(figsize=(12,4))
sns.pointplot(data=tr_df, x='Month', y='numOfTrips', hue='Year', ax=ax, palette='nipy_spectral')
ax.set_xticks(range(-3,10))
ax.set_xticklabels(range(12))
ax.set(title="Number of trips each month")

你基本上是在强制绘图向左和向右添加更多的刻度(使用负值),然后将所有标签从1到12进行重命名。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接