X Tutup
Skip to content

Figure.savefig() not respecting bbox_inches='tight' when dpi specified #7820

@sullivanrs

Description

@sullivanrs

Bug summary

Figure.savefig() with bbox_inches='tight' and PNG output is incorrectly clipping text annotations when a dpi argument is passed. When no dpi argument passed, bbox_inches='tight' functions as expected.

Python version:
Python 3.5.2 |Anaconda 4.1.1 (32-bit)| (default, Jul 5 2016, 11:45:57) [MSC v.1900 32 bit (Intel)]
Using matplotlib version 1.5.3

Example

import matplotlib
matplotlib.use('TkAgg')

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

df = pd.read_csv('http://www.who.int/childgrowth/standards/wfa_boys_p_exp.txt', sep='\t', index_col=0)

DAYS_IN_MONTH = 30.4375
DAYS_IN_YEAR = DAYS_IN_MONTH * 12

percentile_list = []

for i in range(7):
    percentile_list.append((tuple(df.columns[[3 + i,17 - i]])))

df.index = df.reset_index()['Age'].div(DAYS_IN_MONTH)
df = df.loc[df.index < 12]

fig, ax = plt.subplots()
df['P50'].plot(ax=ax, color='w')

x = df.index

color_list = sns.color_palette("Blues", len(percentile_list))

for i, j in enumerate(percentile_list):    
    y1 = df[j[0]]
    y2 = df[j[1]]
    ax.fill_between(x, y1, y2, color=color_list[i])

text_annotations= []

for i in df.columns[3:]:
    annotation = ax.annotate(i, xy=(12, df.loc[:,i].iloc[-1]),  
                             xytext=(5,0), textcoords="offset points",
                             va="center", ha="left", size=8)
    text_annotations.append(annotation)

fig.savefig('weight_chart.png', bbox_inches='tight')
fig.savefig('weight_chart_dpi.png', bbox_inches='tight', dpi=300)

weight_chart.png
weight_chart

weight_chart_dpi.png
weight_chart_dpi

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup