【问题标题】:3D scatter plot with in Python extracted from Dates从日期中提取的 Python 中的 3D 散点图
【发布时间】:2019-11-08 15:39:41
【问题描述】:

问题:

有没有办法可以将天转换为字符串而不是十进制值?月份也是如此。

注意:我已经访问过this (3D Scatterplot with strings in Python) 的答案,但没有解决我的问题。

我正在做一个自我项目,我正在尝试根据我从谷歌活动中检索到的数据为我的通勤创建 3D 图表。 作为参考,我正在关注本指南:https://nvbn.github.io/2018/05/01/commute/

我能够根据 Month + TimeDay +Time 属性创建信息丰富的 2D 图表,但我希望将这 2 个图表结合起来。

我要创建的 3D 图表需要 3 个属性 Day(周一/周二)、Month(一月/二月)、耗时

鉴于 matplotlib 不立即支持图表中的字符串值,我使用数字表示日 (0-7) 和月 (1-12)。但是,对于几天的十进制值,图表似乎有点模糊。看起来像以下

我当前的代码如下所示,检索 weekday() 以获取日期,month 获取月份。

# How commute is calculated and grouped
import pandas as pd
#{...}
def get_commute_to_work():
#{...}
 yield Commute_to_work(pd.to_datetime(start.datetime), start.datetime, end.datetime, end.datetime - start.datetime)

#Now creating graph here

fig, ax = pyplot.subplots(subplot_kw={'projection': '3d'})
ax.grid()
ax.scatter([commute.day.weekday() for commute in normalised],
           [commute.day.month for commute in normalised],
           [commute.took.total_seconds() / 60 for commute in normalised])

ax.set(xlabel='Day',ylabel='Month' ,zlabel='commute (minutes)',
       title='Daily commute')
ax.legend()
pyplot.show()

注意。如果您想查看此代码的详细信息,请访问github 此处

【问题讨论】:

  • 您说链接的答案不能解决您的问题。那里的建议是在轴上设置刻度标签。你真的试过吗? ax.set([...], xticks=range(1, 6), xticklabels=["Mon", "Tue", "Wed", "Thu", "Fri"])

标签: python matplotlib


【解决方案1】:

你可以试试这个(虽然我还没有验证 3d 情节):

x_tick_labels = ['Sun','Mon','Tue','Wed','Thurs', 'Fri', 'Sat']

# Set number of ticks for x-axis
x = np.linspace(1.0, 4.0, 7)  # Why you have 9 days in a week is beyond me
ax.set_xticks(x)
# Set ticks labels for x-axis
ax.set_xticklabels(x_ticks_labels, rotation='vertical', fontsize=18)

您可以重复类似的程序数月。

这个答案的来源是here

【讨论】:

  • 我会试一试的。 “为什么你一周有 9 天我无法理解” - 我也想知道,但它有点分散。 0.0 - 1.0 周一,1.0 - 2.0 周二,2.0 - 3.0 周三,3.0 - 4.0 周四,4.0 - 5.0 是周五 - 我不知道为什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-18
  • 2019-06-23
  • 1970-01-01
  • 2019-06-04
  • 1970-01-01
  • 2016-12-22
相关资源
最近更新 更多