How to plot a barchart from timeseries data with matplotlib? - python

I'm processing a data set with in/out movements from a bird's nest. Right now the registrations is per 2 minutes with datetime object as index, I'm using pandas dataframe for this. The full data set is a year.
How do I plot a bar chart where "the bars" are the total movements per hour (and not per minute) over a period of x days?
This a bar chart I managed to do with the .groupby(pd.Grouper( freq='H')).sum().plot.bar() function, however I'm searching for better method because of the loss of datetime object as x-axis.
The barchart I want to replicate

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.

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

How to make date tick marks more precise in matplotlib?

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:

Categories