Python chart: Minimum value

jamescooper

Well-known Member
Joined
Sep 8, 2014
Messages
899
Not sure why this doesn't work?

import pandas as pd
from datetime import datetime
import matplotlib.pyplot as plt

today_format = datetime.strptime(datetime.today().strftime('%d/%m/%Y'), "%d/%m/%Y")
filtered_df = values=df['Current price'], aggfunc='min') & (df['Date Captured'] == today_format)]
filtered_df.plot(x="Mon Year", y=["Current price"], kind="bar")
plt.title("Minimum Price")
plt.xlabel("Month Year")
plt.ylabel("Price (£)")
plt.locator_params(axis='x', nbins=12)
plt.show()
 
One final question on this code.

So on testing this finds the minimum value of all dates in the dataset for the date rather than the minimum value on today's date for that "Check-In" date.

Which bit would I change to resolve; I have made a few modifications to it.

Many thanks.

import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
import datetime
import matplotlib.dates as mdates
import numpy as np
import matplotlib.ticker as plticker

today = datetime.datetime.today().date()
string_date = today.strftime("%b %y")

df["Date Captured"] = pd.to_datetime(df["Date Captured"])
df["Check-In"] = pd.to_datetime(df["Check-In"])
df_filtered = df[df["Date Captured"].dt.date == today]
df_filtered = df[df["Mon Year"]== string_date]
grouped = df_filtered.groupby("Check-In")["Current price"].min().reset_index()

fig, ax = plt.subplots()
p1 = ax.bar(grouped["Check-In"], grouped["Current price"], color='g')
ax.bar_label(p1, label_type="edge")
ax.bar_label(p1)

# Formatting the x-axis
ax.xaxis.set_major_formatter(mdates.DateFormatter("%a %d")) # Format as "Jan 2025"
loc = plticker.MultipleLocator(base=1.0) # this locator puts ticks at regular intervals
ax.xaxis.set_major_locator(loc)
plt.xlabel("Check-In Date")
plt.ylabel("Price (£)")
plt.title("February 25 - Minimum Price")
plt.xticks(rotation=90) # Rotate labels
plt.ylim(50, 150)
plt.show()
 
Upvote 0
df_filtered = df[df["Mon Year"]== string_date]
Try remove this line. I don't see the use of your "Mon Year" column. It seems like an unnecessary calculation. You can always use the code to format the the date as M-Y without adding a whole new column to your data set.

Like what you're doing here.
ax.xaxis.set_major_formatter(mdates.DateFormatter("%a %d")) # Format as "Jan 2025"
 
Upvote 0
Try remove this line. I don't see the use of your "Mon Year" column. It seems like an unnecessary calculation. You can always use the code to format the the date as M-Y without adding a whole new column to your data set.

Like what you're doing here.
This is with and without the line you said to remove. Essentially in the "with" graph, the values £95 for 1st February for example is the lowest captured across all "Dates Captured" for that date. I want the minimum value on today, i.e. 17th February for 1st February, 2nd February etc...

Hope that helps.
 

Attachments

  • Without.jpg
    Without.jpg
    56.3 KB · Views: 4
  • With.jpg
    With.jpg
    30.8 KB · Views: 4
Upvote 0
So I've tried a few options here but it is getting the Minimum value of ALL "Date Captured" for a "Check-in" date rather than the minimum value today. This is my code:

import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
import datetime
import matplotlib.dates as mdates
import numpy as np
import matplotlib.ticker as plticker

today = datetime.datetime.today().date()
string_date = today.strftime("%b %y")

df["Date Captured"] = pd.to_datetime(df["Date Captured"])
df["Check-In"] = pd.to_datetime(df["Check-In"])
df_filtered = df[df["Date Captured"].dt.date == today]
df_filtered = df[df["Mon Year"]== string_date]
grouped = df_filtered.groupby("Check-In")["Current price"].min().reset_index()

fig, ax = plt.subplots()
p1 = ax.bar(grouped["Check-In"], grouped["Current price"], color='g')
ax.bar_label(p1, label_type="edge")
ax.bar_label(p1)

# Formatting the x-axis
ax.xaxis.set_major_formatter(mdates.DateFormatter("%a %d")) # Format as "Jan 2025"
loc = plticker.MultipleLocator(base=1.0) # this locator puts ticks at regular intervals
ax.xaxis.set_major_locator(loc)
plt.xlabel("Check-In Date")
plt.ylabel("Price (£)")
plt.title("February 25 - Minimum Price")
plt.xticks(rotation=90) # Rotate labels
plt.ylim(50, 150)
plt.show()


Any thoughts appreciated.
 
Upvote 0
df_filtered = df[df["Date Captured"].dt.date == today]
df_filtered = df[df["Mon Year"]== string_date]
This is not working as you intended, the df_filtered is only filtering for "Mon Year" = string_date. The first filter gets overridden.

You need both simultaneously like this

Python:
df_filtered = df[(df["Date Captured"].dt.date == today) & (df["Mon Year"] == string_date)]
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top