seaborn scatterplot datetime xaxis too wide

泄露秘密 提交于 2019-12-12 11:42:14

问题


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)

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


回答1:


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.




回答2:


As an alternative you could use seaborns lineplot which does have a correct x-axis:

sns.lineplot(x='DateTimeUTC', y='Depth', data=df, marker='o')

Or you could use:

plt.plot(df['DateTimeUTC'], df['Depth'], linestyle='None', marker='o')


来源:https://stackoverflow.com/questions/54050472/seaborn-scatterplot-datetime-xaxis-too-wide

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!