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()
Related
I'm trying to plot minimum and maximum daily temperature values for last 20 years. Since there are too many days in between, my plot graph looks too complicated.
How can I make change the frequency of days to reduce the density of my graph?
In other words, I want to set that it gets the weather of one day and then skips following 2 days in the plot without changing the dataframe.
fig, ax = plt.subplots()
colors = ["Orange", "Blue"]
for i,col in enumerate(weather_data.columns):
if col is "Date": continue
ax.plot('Date', col, data=weather_data)
ax.set_xlabel("Date")
ax.set_ylabel("Temperature (Celcius)")
# set 15 xticks to prevent overlapping
ax.set_xticks(np.arange(0, weather_data.shape[0],weather_data.shape[0] / 15))
ax.legend()
fig.autofmt_xdate()
ax.set_title('Time Plot of Weather');
Dataset:
https://drive.google.com/uc?id=1O-7DuL6-bkPBpz7mAUZ7M62P6EOyngG2
Hard to say without sample data, but one option is to show only one data point out of every k data points in the original DataFrame, and interpolate the missing days with straight line segments. (This is basically downsampling.)
For example, to show every 5 data points, change this line:
ax.plot('Date', col, data=weather_data)
to this:
ax.plot('Date', col, data=weather_data.iloc[::5])
There are other approaches such as nonlinear interpolation or showing a rolling average, but this should serve as a starting point.
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.
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.
I have a dataset for taxi trip information for four years (2010-2013). I want to do some preliminary data analysis by looking at plots of each value in two columns in a data with 'Day Hours' on the x axis in increments of 1-24 or 0-23 'Trip Duration (seconds)' on the y-axis. The problem is the pickup_hour column is not sequential, it's based on datetime column.
>>df10.head(20)
First 20 rows
Then, I was tried to plot data as following as:
fig, ax = plt.subplots(figsize=(9, 7))
plt.plot(df10['pickup_hour'][0:10],df10[' trip_time_in_secs'][0:10])
plt.xlabel('Day Hours')
plt.ylabel('Trip Duration (seconds)')
#plt.xticks(rotation='vertical')
plt.show()
Plot for first 10 rows
Now I hope to find some way to help please, thanks advance.
If you want to plot all the days in the same diagram, then don't draw the lines. Just mark them with an 'x' or 'o'
plt.plot(df10['pickup_hour'][0:10],df10[' trip_time_in_secs'][0:10],'x')
you can set the X-axis labels using
plt.xticks(range(24))
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: