Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Closed
Labels
Milestone
Description
There is a discrepancy between vector format (pdf, svg, eps) output and image output (png, jpg, etc) for the BboxImage object. I can't attach a pdf, but the this code creates and saves a pdf and png illustrating the BboxImage object translation. Just update the save folder path to run the code.
Thanks and let me know if I should include anything else.
'''Minimum working exampleversion 1.3.1Plots a descriptive flowchart showing error@Author: wronk'''from os import path as opimport numpy as npimport matplotlib as mplimport matplotlib.pyplot as pltfrom matplotlib.image import BboxImagefrom matplotlib.offsetbox import TextArea, AnnotationBboxfrom matplotlib.transforms import Bboxfrom mpl_toolkits.axes_grid1 import make_axes_locatablefrom matplotlib.backends.backend_pdf import PdfPages################################################################################EDIT SAVE FOLDER TO AUTOSAVEsaveFig = Truesave_fname = '/home/wronk/'mpl.rcParams['mathtext.default'] = 'rm'#Box propertiesbboxProp = dict(boxstyle='round,pad=0.3', fill=False, ec='w', linewidth=8)#Colors for annotation arrowssenCol = (0.35, 0.35, 0.35)srcCol = (.8, .216, 0.0)#Define color mapcm = 'autumn'colRange = np.atleast_2d(np.arange(256)/256.)################################################################################Initialize Plotplt.ion()plt.close('all')mpl.rcParams['pdf.fonttype'] = 42figSize = (12, 6)ftSize = 32dpi = 80rowYVals = [0.085, 0.55, 0.85]rowXVals = [0, .075, .2, .34, .5, .65, .8, .875]fig = plt.figure(figsize=figSize, facecolor='white', dpi=dpi)#fig = plt.figure(figsize=figSize, facecolor='white')#ax = fig.gca()ax = plt.subplot(111)################################################################################Mid level of annotation boxes#Text for boxes in mathematical fontflowMid = r'$j_{N-1}$'midBox3 = ax.annotate(flowMid, xy=(0, .5), xycoords='axes fraction', xytext=(rowXVals[3], rowYVals[1]), textcoords='axes fraction', size=ftSize, ha='left', va='center', bbox=bboxProp, arrowprops=None, color='black')########################################HEREIN LIES THE PROBLEM#Add color (gradients) behind the boxes#Something about the bbox window extent property must get shifted when saving#as a vector format (pdf, svg, eps) but not common image types. In my experience,#every bboximage was translated and scaled uniformly.#I tried changing DPI, image size, subplot parameters as well as sychronizing#plotting and saving figures in matplotlibrc to no avail. DPI does seem to#have some sort of effect in terms of how the bboximage gets shifted though.gradient = BboxImage(midBox3.get_bbox_patch().get_window_extent, data=np.zeros_like(colRange), cmap=cm, norm=None, origin=None)ax.add_artist(gradient)#######################################plt.draw()#Save figure as pdf and png to highlight difference#Same results when saving from GUI windowif saveFig: pdfFile = PdfPages(save_fname + 'flowchart_PDF.pdf') plt.savefig(pdfFile, format='pdf', dpi=fig.dpi) pdfFile.close() plt.savefig(save_fname + 'flowchart_PNG.png', dpi=fig.dpi)plt.show()
(tacaswell edited for code markup)