3

I created a pointplot() and I cannot change x-axis limit. Although my data only contains 9 months, I want to show all 12 on my axis.

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

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
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Bn.F76
  • 783
  • 2
  • 12
  • 30
  • If you include some of the data with your it will be easier for people to answer your question - are you able to do so? – William Miller Apr 12 '19 at 03:16
  • It seems like it does work but the problem is interpreting the ticks.. I'm trying to find a solution, but this might send you in the right direction in the meanwhile. I think that the pointplot by default treats the x-axis as a catagorical value, and therefore it just ignores the actual value of your data – Tacratis Apr 12 '19 at 03:40

3 Answers3

5

IMHO, seaborn's pointplot is not the kind of plot you are looking for.

I'd suggest a simple lineplot, then your attempt to set the xlims work as expected:

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

enter image description here

However, I'd also recommend to set the xticks in this context to some list with 12 values, while 0...12 has 13... ;-)

SpghttCd
  • 10,510
  • 2
  • 20
  • 25
  • Thanks for your answer! Why do you think `pointplots` are not a good choice here? I'm guessing because `numOfTrips` is quantitative and `poinplots` are used to compare categorical variables ? – Bn.F76 Apr 12 '19 at 16:40
  • I admit that I also guessed something similar (as I also mentioned in my first edit). But after checking the docs I don't think that this is the case. `pointplot` simply felt not as natural as `lineplot` to me, and for your case, this feeling was obviously hinting in the right direction, because `pointplot` seems to handle the axes differently. However, I don't know what exactly the relevant difference is, which is resulting in your issue, sorry. – SpghttCd Apr 14 '19 at 13:39
2

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. 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

enter image description here

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

enter image description here

If you want to include the months lower than the minimum (i.e. 0,1,2) you can set the xticks and xticklabels manually -

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

Which will give you

enter image description here

William Miller
  • 9,839
  • 3
  • 25
  • 46
1

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. 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.

Tacratis
  • 1,035
  • 1
  • 6
  • 16