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()
 
In your original code, there's a "Mon Year" column. Is that not relevant?
 
Upvote 0
I'd like all dates to be shown in the data set.
Do you want to aggregate min on date (d/m/yy) or month-year (m/yy)?
The former will show individual dates since it's taking day into consideration while the later aggregate the entire month.
So either 2/13/25, 2/14/25,..., 3/1/25,3/2/25
or
2/25, 3/25
?
 
Upvote 0
So along the bottom should be each date ("Check-in") for next 365 days, minimum value....then up the y-axis should be Current price ("Current price") but filtered by todays date as the date captured ("Date captured").
 
Upvote 0
Still don't see how "Mon Year" is relevant. Try:
Python:
import pandas as pd
import matplotlib.pyplot as plt
import datetime

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

plt.bar(grouped["Check-In"].astype(str), grouped["Current Price"], color='b')
plt.xlabel("Check-In Date")
plt.ylabel("Minimum Price (£)")
plt.title("Minimum Hotel Prices by Check-In Date")
plt.show()
 
Upvote 0
Still don't see how "Mon Year" is relevant. Try:
Python:
import pandas as pd
import matplotlib.pyplot as plt
import datetime

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

plt.bar(grouped["Check-In"].astype(str), grouped["Current Price"], color='b')
plt.xlabel("Check-In Date")
plt.ylabel("Minimum Price (£)")
plt.title("Minimum Hotel Prices by Check-In Date")
plt.show()
Thank you, that works just needed to be small p on the "Current price"
 
Upvote 0
Last thing,

Just need to make sure the x-axis isn't blurred. Any way I can get it to plot month year,

plt.locator_params(axis='x', nbins=12)

Is what I used for other graphs.
 
Upvote 0
The chart looks like this, you can see the blurred dates at the bottom because there is 365 days on the x-axis. How could I minimise so it is readable as with this other chart off the same dataset.
 

Attachments

  • Untitled2.jpg
    Untitled2.jpg
    27.8 KB · Views: 5
  • Untitled.jpg
    Untitled.jpg
    32 KB · Views: 4
Upvote 0
Try:
Python:
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import matplotlib.dates as mdates

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

fig, ax = plt.subplots()
ax.bar(grouped["Check-In"], grouped["Current Price"], color='b')

# Formatting the x-axis
ax.xaxis.set_major_locator(mdates.MonthLocator(bymonthday=1))  # Show label on the first of each month
ax.xaxis.set_major_formatter(mdates.DateFormatter("%b %Y"))  # Format as "Jan 2025"

plt.xlabel("Check-In Date")
plt.ylabel("Minimum Price (£)")
plt.title("Minimum Hotel Prices by Check-In Date")
plt.xticks(rotation=90)  # Rotate labels
plt.show()
 
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