How to make date tick marks more precise in matplotlib? - python

I am using matplotlib to chart data with datetime.date as the x-axis.
Currently the graph has tick marks of the month and year in a certain range. As this range is too wide, the actual point for the price can't even be seen.
I want my matplotlib chart to plot x tick marks either:
for every date that is retrieved from the table, or
within a small enough range (e.g. 30 days within a month) so the price of the ticket on each of the days can be seen.
How can this be done?
I tried solutions from SO questions like this but they aren't working, probably because the dates from the table aren't of type float.
Here's an image of the chart:

Related

How to set a starting day in heatmap (python)?

I have drawn a heatmap that represents the anomaly score value for a specific week and its days. The heatmap I got is shown below.
Now, on the Y-axis, I want the day that should start with Monday and on the x-axis, the gap between the two dates should be 7 days i.e. one week. Is there any other way to draw a heatmap to get the results I desired? Or is there any other ways to set the parameters in the existing heatmap function (sns.heatmap())?
There may be a more sophisticated way to do this, but I have taken the day of the week values from the sample data dates and pivot transformed them to be the source data for the graph.
Next, we will create a list of days of the week to make the day of the week data into day names. Then, create a label for the x-axis with a date interval of 7 days.
weekday = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
ax = sns.heatmap(df_pivot, cmap='afmhot_r')
freq = 7
ax.set_xticks(df.index[::freq])
ax.set_xticklabels(df.iloc[::freq]["date"].dt.strftime("%Y-%m-%d"))
ax.set_yticklabels(weekday, rotation=0)
ax.invert_yaxis()
plt.show()

plotting timeseries of different years in a single plot for comparison and limiting sticks using Seaborn

I have data that stretches 4 years in 5 minute resolution. I need to compare data in a single plot. I am using Seaborn package for plotting. I cannot plot with the data as-is because it will make the plot a four year time series plot. I converted the "Time Stamp" data to contain only day month and time using :
df_2018['no_year_dt'] = df_2018['Time Stamp'].dt.strftime('%m/%d %H:%M:%S')
but the resulting column datatype is 'object' and not a 'datetime' format. Plotting using 'no_year_dt' column will result in all datapoints in x-axis ticks. (not readable)
I want the resulting plot to have limited x-ticks, say only the date like '1-1' '1-2' if I am plotting one months worth data.

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.

Why don't matplotlib recognize my timestamp on xaxis?

Right now Im trying to figure out how Matplotlib and Pandas work by processing a dataset with movements out/in a birdnest. I successfully plotted the full dataset as an line graph with an xaxis as timestamp.
However when Im trying to plot a set interval as a barchart (grouped by hour) matplotlib no longer recognize the xaxis as a timestamp, so i can't use formatters and locators correctly (HourLocator etc.).
The index of my dataframe is a timestamp, however it seems to loose it properties as a timestamp when i use the .groupby() function.
newdf = df[date:(date+forward*deltaday)]
bx = newdf['Movements'].groupby(pd.Grouper( freq='H')).sum().plot.bar()
xtick_locator = mdates.AutoDateLocator()
xtick_formatter = mdates.AutoDateFormatter(xtick_locator)
bx.xaxis.set_major_locator(xtick_locator)
bx.xaxis.set_major_formatter(xtick_formatter)
When running this code i get the following error messages:
ValueError: view limit minimum -0.5 is less than 1 and is an invalid
Matplotlib date value. This often happens if you pass a non-datetime
value to an axis that has datetime units"
Which indicates that my xaxis is not datetime object anymore. When fetching my xticks labels with xticks() i get following:
Text(0, 0, '2015-02-02 00:00:00')...
Text(47, 0, '2015-02-03 23:00:00')
It looks like an timestamp but matplotlib won't recognize it as one. Without any expertise knowledge i believe it .groupby() who is the bandit.
Is there a better way to plot a barchart with the sum of df['Movements'] per hour plotted for a set time interval?
(df['Movements'] is right now movements per minute)

How to plot candlestick chart with dates on xaxis using cufflinks?

I tried to plot a candlestick chart with dates on xaxis.
But the dates on chart is autoincrementing rather than using the dates used in dataframe.
Generally saturday, sunday dates are having no candles since no trading.
My output is also including these dates on chart and finally plotting the chart as white empty space for these dates.
These candles for the day will be present from 9:15 AM to 3:15 PM.
I need include the candles for these timings only.
But when I tried to plot these xaxis is plotting with 24hrs time on it.
Resulting many gaps between day to day candles.
I need the chart without gaps between candles.
somebody help plz.Need to plot the chart without these gaps

Categories