Repository navigation
Description
Issue (?)
When plotting a arrow (with FancyArrowPatch from matplotlib.patches), and exporting it to a picture, the size of its head is not consistent with what is displayed in the interactive figure. (The linewidth seems to remain OK between interactive and exported file, whatever format is used.)
As far as I understand, it seems to be DPI-related:
- If the DPI value is the same between the interactive figure and the export made with
savefig, then everything is fine. - However, if I want to export with DPI 3 times bigger than in the interactive figure (for example 300 DPI instead of 100 DPI), one workaround I found is to use a mutation_scale 3 times bigger than before when plotting the interactive figure. Unfortunately, it is neither really convenient nor satisfying as it affects what is displayed in the interactive figure (the arrow head is 3 times bigger...)
I don't know if it is a bug or the expected behavior, but I find it disturbing to have an export that is (that) different from the interactive display.
Environment infos
- Linux, Python 2.7 (from Anaconda2), Matplotlib v1.5.0
- Interactive backend is Qt4Agg
Example script
Here is a script that generates a figure with 2 arrows (FancyArrowPatch), and exports it to different formats:
import matplotlib.pyplot as plt
from matplotlib.patches import FancyArrowPatch
plt.ion() # My interactive backend is Qt4Agg, with default @ 80 DPI
""" Prepare and plot 2 FancyArrowPatch instances, the 2nd one with a
mutation_scale parameter 3 times bigger than the 1st one.
"""
common_opts = dict(arrowstyle=u'->', lw=3)
arrow_patch_0 = FancyArrowPatch(posA=(0.2, 0.8), posB=(0.8, 0.65),
mutation_scale=50, **common_opts)
arrow_patch_1 = FancyArrowPatch(posA=(0.2, 0.2), posB=(0.8, 0.45),
mutation_scale=150, **common_opts)
fig, ax = plt.subplots(figsize=(8, 6), dpi=100)
ax.text(0.2, 0.85, "mutation_scale = 50", ha='left', va='bottom')
ax.text(0.2, 0.15, "mutation_scale = 150", ha='left', va='top')
for arrow_patch in [arrow_patch_0, arrow_patch_1]:
ax.add_patch(arrow_patch)
""" Export to different formats, with different DPI values if rasterized.
"""
common_prefix = 'FancyArrowPatch_testfile'
fig.savefig(common_prefix + '.eps')
fig.savefig(common_prefix + '.pdf')
fig.savefig(common_prefix + '_300dpi.png', dpi=300)
fig.savefig(common_prefix + '_100dpi.png', dpi=100)
fig.savefig(common_prefix + '_300dpi.jpg', dpi=300)
fig.savefig(common_prefix + '_100dpi.jpg', dpi=100)``
And below is a picture that compares the 6 different output files (2 x 3 bottom panels) with a screenshot of the interactive figure (top center panel).

Observations about the example
One can see that with the raster formats (PNG and JPG):
- @ 100 DPI (blue labels), the exports are the same as in the screenshot of the interactive display with 100 DPI.
- @ 300 DPI (red labels), the exports differ from the interactive display. Besides, the arrow head with mutation_scale = 150 seems to be as big as the arrow head with mutation_scale = 50 @ 100 DPI.
Concerning the (at least partially) vector formats that I tested (PDF and EPS, with purple labels), one can observe that the arrow heads do not have exactly the same size as in the interactive display either (they are slightly bigger).
Activity
efiring commentedon Feb 20, 2016
Thank you for your clear presentation of this problem. I think it is a combination of a confusing API plus one or more genuine bugs, and it is in parts of the codebase that most of us find difficult to work with. You are correct in thinking that changing the output dpi or the file format should not change the relative dimensions of plot elements, so we will need to track down whatever is violating this rule.
efiring commentedon Feb 20, 2016
After a quick scan of patches.py, I see a mysterious
dpi_corkwarg, and a 'FIXME' comment suggesting "There could be room for improvement." Perhaps an understatement. Either the behavior reported here is a consequence of recent changes, or it is an inherent limitation of the present implementation. I suspect the latter. One of the problems is that there is very little in the way of hints as to which units the different variables and kwargs are using. My guess is that some things are being calculated a little bit too early based on the present figure dpi and are not being automatically recalculated to reflect the actual renderer dpi; maybe dpi_cor was a hack added to allow specification of the expected renderer dpi, or the ratio of that to the present dpi, or...afvincent commentedon Feb 22, 2016
I played a bit with the
FancyArrowPatchclass, and I found some kind of workaround (described below), that does seem to achieve DPI-independent head size of the arrow.This idea is basically to update the mutation_scale value each time the
_dpi_corattribute is set (through the ad-hoc setter). I derived a tweaked class from the vanilla FancyArrowPatch, to demonstrate this (see the following script).set_dpi_coris essentially called in thedrawmethod:self.set_dpi_cor(renderer.points_to_pixels(1.)). As the only other use of the setter is in the__init__, I don't understand why one can pass adpi_corvalue as an argument when creating an instance: the value will be overwritten when rendering the figure (i.e. when calling thedrawmethod) anyway, won't it be?I don't really know how to test if tweaking
FancyArrowPatchlike above has border effects. I looked intotest_patches.pybut it seems to me that there is currently no unit test related toFancyArrowPatch(which seems a bit weird to me).Here is a summary of the output pictures. The two top panels are screenshots of the interactive figure, with two different DPI values (50 and 150). The other panels correspond to the output figures (with different backends and different DPI values), that are produced when exporting the interactive top panels (one column per top panel). The tweaked

FancyArrowPatchversion seems to result in identical arrows whatever the DPI value is, which the vanilla class does not (I added a small red scale marker to better demonstrate this).As a conclusion, I have to admit that I don't fully understand how
FancyArrowPatchworks under the hood, so there may be a cleaner way to achieve this.tacaswell commentedon Feb 22, 2016
That seem relatively clean to me.
Very nice analysis.
Could you open a pull request with those changes to
FancyArrowPatch?First draft to correct issue matplotlib#6035 and to write relevant un…
afvincent commentedon Feb 23, 2016
I have started to work on a branch that includes the proposed workaround in
FancyArrowPatch.Besides I tried to write some
@image_comparisonunit test (I still don't fully understand the workflow, but well everything-- even git & Co-- is quite new for me, so have to start somewhere anyway). I finally discovered that contrary to what I wrote before, there are some unit tests, intest_arrow_patches.py(and not intest_patches.pyas I expected). Unfortunately, it seems that tweakingset_dpi_coras I suggested breaks the former unit tests :(. A wild guess is that I only tested the arrowstyle'->', and something breaks in the other available styles (that are indeed tested in the former unit tests).tacaswell commentedon Feb 23, 2016
It is not impossible that we have tests that are ensuring wrong behavior.
afvincent commentedon Feb 23, 2016
That was my first hope. But definitively, the proposed workaround breaks
test_fancyarrow, that makes use ofannotate. I finally managed to find in the classAnnotation(intext.py) something that seems similar to the aforementioned fix:It updates

mutation_scalebased on the conversion from points to pixels too, but from outside of thedrawmethod of theFancyArrowPatchinstance (contrary to the tweakedset_dpi_cor, essentially called indraw). And indeed, the arrows produced throughannotateare already (i.e. in mpl 1.5) dpi-independent. Here are some exports from a function more or less directly copy-pasted fromtest_fancyarrow:So the good news is that there seems to be some convergence toward the same solution :). The bad news is that for the moment I still didn't manage to merge the two approaches into one single solution that both wouldn't break
annotateand would fix the raw classFancyArrowPatch.PS: I guess there is a way to cite some lines from a source code file without copy-pasting them like I did… Where can I find infos on how to do it?
efiring commentedon Feb 23, 2016
On 2016/02/23 8:23 AM, afvincent wrote:
Quick thought, tentative, since I haven't had time to look into this
yet, and won't for a little while: it seems wrong to be changing
mutation_scale, which is an input argument. There should instead be
some internal, private variable that takes into account mutation scale
and dpi to control the rendered result. Correct?
(Longer term, I hope we can make the whole API much clearer and easier
to understand and remember.)
Eric
afvincent commentedon Feb 23, 2016
Based on what you wrote Eric, I think I finally found a way to correct this bug without altering the attribute
mutation_scaleor breaking the arrows produced withannotate(which were fine). Details are given in PR #6054.First draft to correct issue matplotlib#6035 and to write relevant un…
First draft to correct issue matplotlib#6035 and to write relevant un…
First draft to correct issue matplotlib#6035 and to write relevant un…
QuLogic commentedon May 30, 2016
Fixed by #6504.