首页 > 解决方案 > seaborn scatterplot datetime xaxis too wide

问题描述

I have this dataframe:

pd.DataFrame({'Depth': {0: 0.2,
  1: 0.4,
  2: 0.4,
  3: 0.4,
  4: 0.4,
  5: 0.4,
  6: 0.6000000000000001,
  7: 0.4,
  8: 3.2,
  9: 2.0},
 'DateTimeUTC': {0: Timestamp('2018-03-28 06:25:08'),
  1: Timestamp('2018-03-28 06:25:49'),
  2: Timestamp('2018-03-28 06:27:06'),
  3: Timestamp('2018-03-28 06:32:11'),
  4: Timestamp('2018-03-28 06:32:59'),
  5: Timestamp('2018-03-28 06:34:02'),
  6: Timestamp('2018-03-28 06:35:38'),
  7: Timestamp('2018-03-28 06:37:04'),
  8: Timestamp('2018-03-28 06:39:08'),
  9: Timestamp('2018-03-28 06:40:52')}})

which looks like this:

<table>
  <tr><th></th><th>Depth</th><th>DateTimeUTC</th></tr>
  <tr><th>0</th><td>0.2</td><td>2018-03-28 06:25:08</td></tr>
  <tr><th>1</th><td>0.4</td><td>2018-03-28 06:25:49</td></tr>
  <tr><th>2</th><td>0.4</td><td>2018-03-28 06:27:06</td></tr>
  <tr><th>3</th><td>0.4</td><td>2018-03-28 06:32:11</td></tr>
  <tr><th>4</th><td>0.4</td><td>2018-03-28 06:32:59</td></tr>
  <tr><th>5</th><td>0.4</td><td>2018-03-28 06:34:02</td></tr>
  <tr><th>6</th><td>0.6</td><td>2018-03-28 06:35:38</td></tr>
  <tr><th>7</th><td>0.4</td><td>2018-03-28 06:37:04</td></tr>
  <tr><th>8</th><td>3.2</td><td>2018-03-28 06:39:08</td></tr>
  <tr><th>9</th><td>2.0</td><td>2018-03-28 06:40:52</td></tr>
</table>

Note the all DateTimeUTC are within 2018. When I try to plot depth vs time using sns.scatterplot I get:

sns.scatterplot('DateTimeUTC', 'Depth', data=df)

plot

Why does the X-axis start at year 2000? Am I doing something wrong?

标签: pythonpandasseaborn

解决方案


I posted the question as an issue on Github, and got this great response. Basically, the problem is that plt.scatter does not deal with dates well, and seaborn uses it. If seaborn will add a type check for the x-axis, which uses plt.plot_date for date values instead, this will be fixed. In the meanwhile, one can create a custom version of sns.scatterplot that does excatly that.


推荐阅读