python - 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)
Why does the X-axis start at year 2000? Am I doing something wrong?
解决方案
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.
推荐阅读
- java - 使用 Tomcat 插件的 Jersey webapp 未部署
- c - 使用 syslog-ng 在 json 日志中显示特殊字符
- laravel - 如何在 Carbon 上自动捕捉这一天?
- php - 语言字符串加载失败:phpmailer 第一个实例的 tls 但第二个实例成功发送邮件
- javascript - 如何使用 HTML 在 javascript 中加载网格
- python - 使用 Matlab 引擎将 Numpy ndarray 传递到 Matlab 函数时数据损坏
- php - 使用链接信息在 PHP 中重定向
- java - 在Java中按暗度递减顺序对十六进制颜色进行排序
- mongodb - 仅根据 createdAt 列从 MongoDB 中获取数据
- javascript - 使 Vue.js 路由器只匹配数字