0

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 enter image description here

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

learner
  • 3,168
  • 3
  • 18
  • 35
Milan Desai
  • 1,228
  • 8
  • 23
  • You say the linked answer does not solve your problem. The suggestion there is to set the tick labels on the axis. Have you really tried that? `ax.set([...], xticks=range(1, 6), xticklabels=["Mon", "Tue", "Wed", "Thu", "Fri"])` – Seb Nov 11 '19 at 14:04

1 Answers1

1

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.

learner
  • 3,168
  • 3
  • 18
  • 35
  • I will give it a go. "Why you have 9 days in a week is beyond me" - I wondered same however it's kind of scattered. 0.0 - 1.0 Monday, 1.0 - 2.0 Tuesday, 2.0 - 3.0 Wednesday, 3.0-4.0 Thursday, 4.0 - 5.0 is Friday - I don't know why tho – Milan Desai Nov 11 '19 at 14:25