import matplotlib.pyplot as plt
# Create figure and primary axis
fig, ax1 = plt.subplots(figsize=(10, 6))
# Customize the plot
plt.style.use('seaborn-v0_8')
# Plot temperature on primary y-axis
color = '#e74c3c' # Red color for temperature
ax1.set_xlabel('Month', fontsize=12, labelpad=10)
ax1.set_ylabel('Temperature (°C)', color=color, fontsize=12)
temp_line = ax1.plot(months, temperature, color=color, marker='o',
linewidth=2, markersize=8, label='Temperature')[0]
ax1.tick_params(axis='y', labelcolor=color)
ax1.set_ylim(0, 35)
# Create a second y-axis for rainfall
ax2 = ax1.twinx()
color = '#3498db' # Blue color for rainfall
ax2.set_ylabel('Rainfall (mm)', color=color, fontsize=12)
rain_bars = ax2.bar(months, rainfall, color=color, alpha=0.3,
width=0.5, label='Rainfall')
ax2.tick_params(axis='y', labelcolor=color)
ax2.set_ylim(0, 100)
# Add value labels on top of bars
for bar in rain_bars:
height = bar.get_height()
ax2.text(bar.get_x() + bar.get_width()/2., height + 2,
f'{int(height)}',
ha='center', va='bottom',
color=color, fontweight='bold')
# Add title and grid
plt.title('Monthly Weather Data (2023)', fontsize=16, pad=20)
ax1.grid(True, linestyle='--', alpha=0.3)
# Add legend
lines1, labels1 = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax1.legend(lines1 + lines2, labels1 + labels2,
loc='upper right', frameon=True, fancybox=True)
# Adjust layout and save
plt.tight_layout()
plt.savefig('weather_plot.png', dpi=300, bbox_inches='tight')
plt.show()