简体   繁体   English

如何设置海洋点图的x轴范围?

[英]How to set the range of x-axis for a seaborn pointplot?

I created a pointplot() and I cannot change x-axis limit. 我创建了一个pointplot() ,但无法更改x轴限制。 Although my data only contains 9 months, I want to show all 12 on my axis. 尽管我的数据仅包含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")

在此处输入图片说明

What am I doing wrong? 我究竟做错了什么?

Edit: data used to create the plot 编辑:用于创建绘图的数据

    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

IMHO, seaborn's pointplot is not the kind of plot you are looking for. 恕我直言,seaborn的pointplot不是您想要的那种图。

I'd suggest a simple lineplot , then your attempt to set the xlims work as expected: 我建议使用一个简单的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")

leads to 导致

在此处输入图片说明

However, I'd also recommend to set the xticks in this context to some list with 12 values, while 0...12 has 13 ... ;-) 但是,我也建议在此上下文中将xticks设置为具有12个值的某些列表,而0 ... 12具有13个 ... ;-)

It's a bit of a hack, but it seems to work. 这有点骇人听闻,但似乎可行。 I believe the issue is that pointplot ignores the numerical value of the axis and treats it like an ordinal. 我认为问题在于, pointplot忽略轴的数值,并将其视为序数。 This code is a manual override: 此代码是一个手动替代:

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")

You are basically forcing the plot to add more ticks to the left and right (using minus values) and then rename all the labels 1 through 12. 您基本上是在强迫绘图在左右添加更多的刻度(使用负值),然后将所有标签重命名为1到12。

It appears the issue is that your data only vary between months 3 and 11. The month indexing then begins at 3 and this corresponds to the xmin . 看来问题在于,您的数据仅在第3个月到第11个月之间变化。然后,月份索引从3开始,这对应于xmin An example which shows this using some random data (I generated it before you added the data) is 一个使用一些随机数据(在添加数据之前生成的数据)显示此示例的示例是

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()

This will produce 这将产生

在此处输入图片说明

The easiest way to fix this (though it does not fix the underlying data and won't work in all cases) is simply to set the limits manually to account for the offset - 解决此问题的最简单方法(尽管它不能修复基础数据,并且在所有情况下都无法使用)只是手动设置限制以解决偏移量-

ax.set(xlim=(-0.5, 8.5))

Which will give you 哪个会给你

在此处输入图片说明

If you want to include the months lower than the minimum (ie 0,1,2) you can set the xticks and xticklabels manually - 如果您要包括少于最小值(即0,1,2)的xticksxticklabels可以手动设置xticksxticklabels

ax.set_xticks(range(-3,9))
ax.set_xticklabels(range(0,12))

Which will give you 哪个会给你

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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