2

I am using interactive python with plt.ion() for generating figures (v2.7) and have noticed that the figure looks different from the figure exported by savefig (this is not a DPI issue (cf. matplotlib savefig() plots different from show()) - I think it might be a backend issue, but would appreciate help as I don't understand this properly).

Specifically, I wanted visualise the importance of a series of points by the intensity of their colour, which I thought I could do with the "alpha" keyword in matplotlib.

When I just do this, this works fine, enter image description here

but when I want to add a line to the figure, the alpha keyword seemed to not work any more, and plt.ion() shows this: enter image description here

I initially thought that perhaps the following issue on github may be related: https://github.com/matplotlib/matplotlib/issues/4580 but then I noticed that exporting the figure actually produced the following file (i.e. as desired): enter image description here

It would be great to understand a bit better what is going on, and how I can avoid such issues in the future. Is plt.ion()/plt.show() not the best way to show figures in interactive python, or is this an issue with the alpha keyword?

The code is here:

import numpy as np
from numpy import random as random
from matplotlib import pyplot as plt
fig2,ax2=plt.subplots(1,1,figsize=(3,3),sharey=True)
for ii in range(1):
    ax2.plot(np.linspace(0,200,200), [0.1]*200, c= 'k')
    for i in range(200):
        test2=random.randint(5)
        ydata= random.rand(test2)
        test = random.rand(test2)
        for j in range(test2):
            ax2.plot(i,ydata[j],'o',ms=4, c= 'Darkblue',alpha=test[j],markeredgecolor='None')
mzzx
  • 1,964
  • 4
  • 16
  • 26
  • 1
    What you observe is definitely unexpected. Also it does not happen when I run the code, where it looks [like this](https://i.stack.imgur.com/zyzSJ.png). Now it seems you use an older version of matplotlib and also a different dpi and figure size. So anything you can provide as info about your system might be helpful explaining this. – ImportanceOfBeingErnest Feb 14 '19 at 23:34
  • Interesting. I am running this on a mac 10.10.5, with matplotlib version 1.5.1. Which version do you use? – mzzx Feb 14 '19 at 23:54
  • I am able to reproduce this exact behaviour with my ipython version (not sure if that is exactly the same as python -i ...) which is also running matplotlib 1.5.1. Maybe it is just the matplotlib version which is outdated? – mzzx Feb 15 '19 at 00:06
  • The image above is with matplotlib 3.0.2. When using matplotlib 1.5.3, the output looks [like this](https://i.stack.imgur.com/0A2XB.png) for me. – ImportanceOfBeingErnest Feb 15 '19 at 00:06
  • Ok, so then maybe version 1.5.1 is just too old. Good spot. I'll try to upgrade this somehow... thank you!! – mzzx Feb 15 '19 at 00:08
  • I mean 1.5.3 and 1.5.1 are not too different. I think there is also something else in the game, some setting for figure size or similar. Did you try a different backend? – ImportanceOfBeingErnest Feb 15 '19 at 00:09
  • Just tried it - that's it! When I use the backend recommended in that github thread (matplotlib.use("tkagg")), it works and looks like your figure/the figure produced by savefig. Do you know what this does? – mzzx Feb 15 '19 at 00:12
  • It's the part that renders the figure (on screen or as file). Apparently the backend you have set by default is errorneous in 1.5.1. – ImportanceOfBeingErnest Feb 15 '19 at 00:15
  • I'm apparently using the "u'MacOSX'" backend, whatever that is. That's great, I'll change that to tkagg, and will look into what backends actually do. Thank you! – mzzx Feb 15 '19 at 00:18

0 Answers0