Time series plot showing unique occurrences per day

2024/9/20 23:26:15

I have a dataframe, where I would like to make a time series plot with three different lines that each show the daily occurrences (the number of rows per day) for each of the values in another column.

To give an example, for the following dataframe, I would like to see the development for how many a's, b's and c's there have been each day.

df = pd.DataFrame({'date':pd.to_datetime(['2019-10-10','2019-10-14','2019-10-09','2019-10-10','2019-10-08','2019-10-14','2019-10-10','2019-10-08','2019-10-08','2019-10-13','2019-10-08','2019-10-12','2019-10-11','2019-10-09','2019-10-08']),'letter':['a','b','c','a','b','b','b','b','c','b','b','a','b','a','c']})

When I try the command below (my best guess so far), however, it does not filter for the different dates (I would like three lines representing each of the letters.

Any ideas on how to solve this?

df.groupby(['date']).count().plot()['letter']

enter image description here

I have also tried a solution in Matplotlib, though this one gives an error..

fig, ax = plt.subplots()
ax.plot(df['date'], df['letter'].count())
Answer

Based on your question, I believe you are looking for a line plot which has dates in X-axis and the counts of letters in the Y-axis. To achieve this, these are the steps you will need to do...

  1. Group the dataframe by date and then letter - get the number of entries/rows for each which you can do using size()
  2. Flatten the grouped dataframe using reset_index(), rename the new column to Counts and sort by letter column (so that the legend shows the data in the alphabetical format)... these are more to do with keeping the new dataframe and graph clean and presentable. I would suggest you do each step separately and print, so that you know what is happening in each step
  3. Plot each line plot separately using filtering the dataframe by each specific letter
  4. Show legend and rotate date so that it comes out with better visibility

The code is shown below....

df = pd.DataFrame({'date':pd.to_datetime(['2019-10-10','2019-10-14','2019-10-09','2019-10-10','2019-10-08','2019-10-14','2019-10-10','2019-10-08','2019-10-08','2019-10-13','2019-10-08','2019-10-12','2019-10-11','2019-10-09','2019-10-08']),'letter':['a','b','c','a','b','b','b','b','c','b','b','a','b','a','c']})df_grouped = df.groupby(by=['date', 'letter']).size().reset_index() ## New DF for grouped data
df_grouped.rename(columns = {0 : 'Counts'}, inplace = True)
df_grouped.sort_values(['letter'], inplace=True)
colors = ['r', 'g', 'b'] ## New list for each color, change as per your preferencefor i, ltr in enumerate(df_grouped.letter.unique()):plt.plot(df_grouped[df_grouped.letter == ltr].date, df_grouped[df_grouped.letter == ltr].Counts, '-o', label=ltr, c=colors[i])
plt.gcf().autofmt_xdate() ## Rotate X-axis so you can see dates clearly without overlap
plt.legend() ## Show legend

Output graph

New Plot

https://en.xdnf.cn/q/119271.html

Related Q&A

Problem accessing indexed results two stage stochastic programming Pyomo

When running a stochastic programming problem in Pyomo, the resulting solution works only when running 10 precisely the same scenarios but the results remain zero when running different scenarios. I ai…

Pandas python + format for values

This is the code:import pandas as pd from pandas import Series, DataFrame import numpy as np import matplotlib.pyplot as pltdf.head(3).style.format({Budget: "€ {:,.0f}"}) Year …

Is there any implementation of deconvolution?

Some one may prefer to call it the transposed convolution, as introduced here. Im looking forward to an implementation of the transposed convolution, in Python or C/C++. Thank you all for helping me!

discord.py How to check if user is on server?

I need to check if the user is on the server. Please help me

Inserting variable stored data into SQLite3 - Python 3

I have been reading information on how to insert data into a database using data stored in a variable. I have not been able to get my data to load to my database and I am not sure why.The program is w…

Print specific line in a .txt file in Python?

I have got a .txt file that contains a lot of lines. I would like my program to ask me what line I would like to print and then print it into the python shell. The .txt file is called packages.txt.

EOF error with both exec_command and invoke_shell method in paramiko

I am trying to execute a command on linux server from my windows machine using python paramiko , I used both of the methods1.exec_command2.invoke_shellBoth of them giving EOF error.import paramiko impo…

Trying to simulate an Overwatch loot box opening program in Python

So basically I am trying to recreate opening a loot box in Overwatch into a runnable program in python. Im trying to make it take four random items in an array and display them each time the user types…

How to remove extra commas from data in Python

I have a CSV file through which I am trying to load data into my SQL table containing 2 columns. I have 2 columns and the data is separated by commas, which identify the next field. The second column c…

How to linearize the sum of a product of two decision variables in LP?

Being new to Linear Programming and Gurobi, I am dealing with an Integer Linear program where I have two binary decision variables defined as B[u_v, x_y] and A[u_x], I am trying to implement this const…