I want to set the title of each graph to be the crop type and the interval it is looking at.
# Load the CSV files
df_corn = pd.read_csv(r"D:Final PrepEverythingWUVR_corn.csv")
df_soy = pd.read_csv(r"D:Final PrepEverythingWUVR_soy.csv")
start_dates= ['05-01', '05-15', '05-29', '06-12', '06-26', '07-10', '07-24', '08-07', '08-21', '09-04', '09-18']
end_dates = ['05-14', '05-28', '06-11', '06-25', '07-09', '07-23', '08-06', '08-20', '09-03', '09-17', '09-30']
# Create the intervals
intervals = [f"{start} to {end}" for start, end in zip(start_dates, end_dates)]
# Create the DataFrame
df_intervals = pd.DataFrame({'Day_Month': start_dates, 'Intervals': intervals})
# Select specific columns and drop rows where NDVI_wet or NDVI_up is 0
columns_to_check = ['NDVI_wet', 'NDVI_up']
df_corn = df_corn[['Sink_ID', 'Year', 'Day_Month', 'WUVR', 'NDVI_wet', 'NDVI_up', 'Temp', 'Precip']]
df_corn = df_corn[~df_corn[columns_to_check].isin([0]).any(axis=1)]
df_soy = df_soy[['Sink_ID', 'Year', 'Day_Month', 'WUVR', 'NDVI_wet', 'NDVI_up', 'Temp', 'Precip']]
df_soy = df_soy[~df_soy[columns_to_check].isin([0]).any(axis=1)]
# Combine the datasets and add a new column to distinguish them
df_corn['Crop'] = 'Corn'
df_soy['Crop'] = 'Soy'
df_combined = pd.concat([df_corn, df_soy])
# Melt the DataFrame to use NDVI_wet and NDVI_up as hue
df_melted = df_combined.melt(id_vars=['Sink_ID', 'Year', 'Day_Month', 'WUVR', 'Temp', 'Precip', 'Crop'],
value_vars=['NDVI_wet', 'NDVI_up'],
var_name='NDVI_Type', value_name='NDVI_Value')
df_melted = pd.merge(df_melted, df_intervals)
sns.set_theme(style='darkgrid')
# Create the displot
g = sns.displot(data=df_melted, x='NDVI_Value', hue='NDVI_Type', row='Intervals', col='Crop',
binwidth=0.05, height=3, aspect=2.5, facet_kws=dict(margin_titles=True),
row_order=df_intervals['Intervals'].tolist())
# Set the xlim for each facet
for ax in g.axes.flatten():
ax.set_xlim(0, 1)
plt.xticks([0, 0.25, 0.5, 0.75, 1], labels=['0', '0.25', '0.5', '0.75', '1'])
plt.show()
I tried asking Chat GPT but it is not every good at using Seaborn and it spat out this
# Set the xlim for each facet and add text
for ax in g.axes.flatten():
ax.set_xlim(0, 1)
# Get the title and split it
title = ax.get_title()
title_parts = title.split(" | ")
# Check if the title is as expected
print(title_parts) # Add this line to debug
if len(title_parts) == 2:
interval = title_parts[0].strip() # Extract interval and remove extra spaces
crop = title_parts[1].strip() # Extract crop type and remove extra spaces
# Add text to the graph
ax.text(0.5, 0.9, f'Crop: {crop}nInterval: {interval}', ha='center', va='center', transform=ax.transAxes)
else:
ax.text(0.5, 0.9, 'Data Missing', ha='center', va='center', transform=ax.transAxes)
It did put a title in each graph where I wanted it but all it said was ‘Data Missing’
New contributor
Ekkehardt Rosasee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1