user2764689 user2764689 - 2 years ago 233
Python Question

Matplotlib pad white space in the figure

I have made a shared axis plot in matplotlib, shown below:

enter image description here

I tried a lot to pad a little white space near the Jan and Dec of the X axis to make it look more aesthetically appealing but couldn't find any way to do this. Can anybody suggest something on this?

My Code:

fig, axes1 = plt.subplots(figsize=(12,8))

axes1.plot(misr_aod, label='MISR', color='blue', linestyle=':', linewidth=2, marker='+')
axes1.plot(modis_aod, label='MODIS', color='green', linestyle='-.', linewidth=2, marker='o')
axes1.plot(CESM_aod, label='CESM_AOD', color='red', linestyle='--', linewidth=2, marker='s')
axes1.set_xticks([-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
axes1.set_xticklabels(['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', ''])
axes1.set_ylabel('AOD at 500 nm')

axes2 = axes1.twinx()

axes2.plot(APHRO_pcp, label='APHRO', color="magenta", linewidth=2, markerfacecolor="yellow", markeredgewidth=2, markeredgecolor="blue")
axes2.plot(IMD_pcp, label='IMD', color="purple", linewidth=2)
axes2.plot(TRMM_pcp, label='TRMM', color="green", linewidth=2)
axes2.plot(CESM_pcp, label='CESM_PRECT', color="red", linewidth=2)
yticks = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
axes2.set_ylabel('PRECIPITATION (mm/day)')

tom tom
Answer Source

You can use ax.margins() to automatically pad some space before and after the data on the x axis.

For your script, you just need something like:


to add 5% of the x-range at both ends. Obviously you just need to adjust x to suit your needs.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download