Sure! The issue you're experiencing with the average temperature heatmaps overlapping their color scales is likely due to both heatmaps sharing the same column layout, causing their colorbars to stack vertically. To place them side by side, you can arrange the subplots in a single row with two columns instead of two rows with one column.
Here's how you can modify the additional_visualizations function to display the Average Temp_Max Heatmap and Average Temp_Min Heatmap side by side:
Updated additional_visualizations Function
# Function to add additional visualizations
def additional_visualizations(df):
figures = []
# 1. Seasonal Decomposition
import statsmodels.api as sm
decomposition_max = sm.tsa.seasonal_decompose(df.set_index('Date')['Temp_Max'], model='additive', period=12)
decomposition_min = sm.tsa.seasonal_decompose(df.set_index('Date')['Temp_Min'], model='additive', period=12)
fig_decomposition = make_subplots(rows=2, cols=1, subplot_titles=['Seasonal Decomposition of Temp_Max', 'Seasonal Decomposition of Temp_Min'])
# Add Temp_Max decomposition
fig_decomposition.add_trace(go.Scatter(x=decomposition_max.trend.index, y=decomposition_max.trend,
mode='lines', name='Trend'), row=1, col=1)
fig_decomposition.add_trace(go.Scatter(x=decomposition_max.seasonal.index, y=decomposition_max.seasonal,
mode='lines', name='Seasonal'), row=1, col=1)
fig_decomposition.add_trace(go.Scatter(x=decomposition_max.resid.index, y=decomposition_max.resid,
mode='lines', name='Residual'), row=1, col=1)
# Add Temp_Min decomposition
fig_decomposition.add_trace(go.Scatter(x=decomposition_min.trend.index, y=decomposition_min.trend,
mode='lines', name='Trend'), row=2, col=1)
fig_decomposition.add_trace(go.Scatter(x=decomposition_min.seasonal.index, y=decomposition_min.seasonal,
mode='lines', name='Seasonal'), row=2, col=1)
fig_decomposition.add_trace(go.Scatter(x=decomposition_min.resid.index, y=decomposition_min.resid,
mode='lines', name='Residual'), row=2, col=1)
fig_decomposition.update_layout(height=800, width=1200, title_text="Seasonal Decomposition")
figures.append(fig_decomposition)
# 2. Correlation Heatmap
corr = df[['Rain', 'Temp_Max', 'Temp_Min']].corr()
fig_corr = px.imshow(corr, text_auto=True,
x=corr.columns, y=corr.columns,
color_continuous_scale='RdBu_r', range_color=[-1,1],
title='Correlation Heatmap')
figures.append(fig_corr)
# 3. Rain vs Temperature Scatter Plot
fig_scatter_max = px.scatter(df, x='Rain', y='Temp_Max', trendline='ols',
title='Rain vs Temp_Max',
labels={'Rain': 'Rain (mm)', 'Temp_Max': 'Maximum Temperature (°C)'})
figures.append(fig_scatter_max)
fig_scatter_min = px.scatter(df, x='Rain', y='Temp_Min', trendline='ols',
title='Rain vs Temp_Min',
labels={'Rain': 'Rain (mm)', 'Temp_Min': 'Minimum Temperature (°C)'})
figures.append(fig_scatter_min)
# 4. Heatmap of Average Temperatures by Month and Year
df['Year'] = df['Date'].dt.year
df['Month'] = df['Date'].dt.month
pivot_max = df.pivot_table(values='Temp_Max', index='Month', columns='Year', aggfunc='mean')
pivot_min = df.pivot_table(values='Temp_Min', index='Month', columns='Year', aggfunc='mean')
# Modify the subplot layout to have 1 row and 2 columns
fig_heatmap = make_subplots(rows=1, cols=2, subplot_titles=['Average Temp_Max Heatmap', 'Average Temp_Min Heatmap'],
horizontal_spacing=0.05)
# Temp_Max Heatmap
heatmap_max = go.Heatmap(z=pivot_max.values,
x=pivot_max.columns,
y=pivot_max.index,
colorscale='Viridis',
colorbar=dict(title='Temp Max (°C)'))
fig_heatmap.add_trace(heatmap_max, row=1, col=1)
# Temp_Min Heatmap
heatmap_min = go.Heatmap(z=pivot_min.values,
x=pivot_min.columns,
y=pivot_min.index,
colorscale='Viridis',
colorbar=dict(title='Temp Min (°C)'))
fig_heatmap.add_trace(heatmap_min, row=1, col=2)
fig_heatmap.update_layout(height=600, width=1200, title_text="Average Temperatures Heatmaps",
showlegend=False)
figures.append(fig_heatmap)
return figures
Key Changes Explained
-
Subplot Arrangement:
- Original: The heatmaps were arranged in 2 rows and 1 column (
rows=2, cols=1), causing them to stack vertically.
- Updated: Changed to 1 row and 2 columns (
rows=1, cols=2) to place them side by side.
-
Horizontal Spacing:
- Adjusted the
horizontal_spacing parameter to ensure there's adequate space between the two heatmaps, preventing the colorbars from overlapping.
-
Colorbars:
- Separate Colorbars: Each heatmap has its own colorbar, positioned appropriately without overlapping.
- Customization: You can further customize the
colorbar parameters if needed, such as setting x positions to fine-tune their placement.
-
Layout Adjustments:
- Height and Width: Adjusted the
height and width parameters to ensure the heatmaps have sufficient space.
- Title: Maintained a descriptive title for the heatmap section.
Complete additional_visualizations Function with Side-by-Side Heatmaps
Below is the complete additional_visualizations function with the modifications applied. Replace your existing function with this one.
# Function to add additional visualizations
def additional_visualizations(df):
figures = []
# 1. Seasonal Decomposition
import statsmodels.api as sm
decomposition_max = sm.tsa.seasonal_decompose(df.set_index('Date')['Temp_Max'], model='additive', period=12)
decomposition_min = sm.tsa.seasonal_decompose(df.set_index('Date')['Temp_Min'], model='additive', period=12)
fig_decomposition = make_subplots(rows=2, cols=1, subplot_titles=['Seasonal Decomposition of Temp_Max', 'Seasonal Decomposition of Temp_Min'])
# Add Temp_Max decomposition
fig_decomposition.add_trace(go.Scatter(x=decomposition_max.trend.index, y=decomposition_max.trend,
mode='lines', name='Trend'), row=1, col=1)
fig_decomposition.add_trace(go.Scatter(x=decomposition_max.seasonal.index, y=decomposition_max.seasonal,
mode='lines', name='Seasonal'), row=1, col=1)
fig_decomposition.add_trace(go.Scatter(x=decomposition_max.resid.index, y=decomposition_max.resid,
mode='lines', name='Residual'), row=1, col=1)
# Add Temp_Min decomposition
fig_decomposition.add_trace(go.Scatter(x=decomposition_min.trend.index, y=decomposition_min.trend,
mode='lines', name='Trend'), row=2, col=1)
fig_decomposition.add_trace(go.Scatter(x=decomposition_min.seasonal.index, y=decomposition_min.seasonal,
mode='lines', name='Seasonal'), row=2, col=1)
fig_decomposition.add_trace(go.Scatter(x=decomposition_min.resid.index, y=decomposition_min.resid,
mode='lines', name='Residual'), row=2, col=1)
fig_decomposition.update_layout(height=800, width=1200, title_text="Seasonal Decomposition")
figures.append(fig_decomposition)
# 2. Correlation Heatmap
corr = df[['Rain', 'Temp_Max', 'Temp_Min']].corr()
fig_corr = px.imshow(corr, text_auto=True,
x=corr.columns, y=corr.columns,
color_continuous_scale='RdBu_r', range_color=[-1,1],
title='Correlation Heatmap')
figures.append(fig_corr)
# 3. Rain vs Temperature Scatter Plot
fig_scatter_max = px.scatter(df, x='Rain', y='Temp_Max', trendline='ols',
title='Rain vs Temp_Max',
labels={'Rain': 'Rain (mm)', 'Temp_Max': 'Maximum Temperature (°C)'})
figures.append(fig_scatter_max)
fig_scatter_min = px.scatter(df, x='Rain', y='Temp_Min', trendline='ols',
title='Rain vs Temp_Min',
labels={'Rain': 'Rain (mm)', 'Temp_Min': 'Minimum Temperature (°C)'})
figures.append(fig_scatter_min)
# 4. Heatmap of Average Temperatures by Month and Year
df['Year'] = df['Date'].dt.year
df['Month'] = df['Date'].dt.month
pivot_max = df.pivot_table(values='Temp_Max', index='Month', columns='Year', aggfunc='mean')
pivot_min = df.pivot_table(values='Temp_Min', index='Month', columns='Year', aggfunc='mean')
# Modify the subplot layout to have 1 row and 2 columns
fig_heatmap = make_subplots(rows=1, cols=2, subplot_titles=['Average Temp_Max Heatmap', 'Average Temp_Min Heatmap'],
horizontal_spacing=0.05)
# Temp_Max Heatmap
heatmap_max = go.Heatmap(z=pivot_max.values,
x=pivot_max.columns,
y=pivot_max.index,
colorscale='Viridis',
colorbar=dict(title='Temp Max (°C)'))
fig_heatmap.add_trace(heatmap_max, row=1, col=1)
# Temp_Min Heatmap
heatmap_min = go.Heatmap(z=pivot_min.values,
x=pivot_min.columns,
y=pivot_min.index,
colorscale='Viridis',
colorbar=dict(title='Temp Min (°C)'))
fig_heatmap.add_trace(heatmap_min, row=1, col=2)
fig_heatmap.update_layout(height=600, width=1200, title_text="Average Temperatures Heatmaps",
showlegend=False)
figures.append(fig_heatmap)
return figures
Additional Recommendations
-
Adjusting Plot Sizes:
- You might need to tweak the
height and width parameters to better fit your specific dataset and display preferences.
-
Uniform Color Scales:
- Ensure both heatmaps use the same
colorscale and range_color if you want to make direct comparisons easier. This can be done by specifying zmin and zmax based on the combined range of both temperature datasets.
-
Interactive Legends:
- If needed, you can turn on legends or adjust their positions to further enhance readability.
-
Tooltips and Annotations:
- Enhance interactivity by customizing tooltips to show additional information on hover.
Final Steps
After updating the additional_visualizations function, run your script as usual. The Average Temp_Max Heatmap and Average Temp_Min Heatmap should now appear side by side without overlapping color scales.
If you encounter any further issues or need additional customization, feel free to ask!