简体   繁体   English

3D scatter plot with in Python 从日期中提取

[英]3D scatter plot with in Python extracted from Dates

Question:问题:

Is there a way I can convert day to String rather than decimal value?有没有办法可以将天转换为字符串而不是十进制值? Similarly for Month.对于月份也是如此。

Note: I already visited this ( 3D Scatterplot with strings in Python ) answer which does not solve my question.注意:我已经访问过这个3D Scatterplot with strings in Python )答案,它不能解决我的问题。

I am working on a self project where I am trying to create 3D chart for my commute from data I retrieved from my google activity.我正在做一个自我项目,我试图从我从谷歌活动中检索到的数据中为我的通勤创建 3D 图表。 For reference I am following this guide: https://nvbn.github.io/2018/05/01/commute/作为参考,我正在关注本指南: https://nvbn.github.io/2018/05/01/commute/

I am able to create informative 2D chart based on Month + Time and Day +Time attributes however I wish to combine these 2 chart.我能够根据月 + 时间日 + 时间属性创建信息丰富的 2D 图表,但是我希望将这 2 个图表结合起来。

3D chart I want to create requires 3 attribute Day (Mon/Tue), Month (Jan/Feb), Time taken .我要创建的 3D 图表需要 3 个属性Day (Mon/Tue), Month (Jan/Feb), Time taken

Given that matplotlib does not support String values in charts right away I have used Number for Day (0-7) and Month (1-12).鉴于 matplotlib 立即不支持图表中的字符串值,我已将数字用于日 (0-7) 和月 (1-12)。 However graph seems bit obscure with decimal values for days.但是,对于几天的十进制值,图表似乎有点模糊。 Looks like following看起来像以下在此处输入图像描述

My current code looks like this, retrieving weekday() to get day number, and month for month.我当前的代码看起来像这样,检索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()

nb.注意。 if you wish to gaze into detail of this code it's available on github here如果您想查看此代码的详细信息,请在此处的github上找到

You can try this (I have not verified for the 3d plot though):你可以试试这个(虽然我还没有验证 3d plot):

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)

You can repeat a similar procedure for months.您可以重复类似的程序数月。

The source for this answer is here .这个答案的来源是here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM