简体   繁体   中英

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.

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. For reference I am following this guide: 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.

3D chart I want to create requires 3 attribute 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). 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.

# 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

You can try this (I have not verified for the 3d plot though):

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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