0

I am using Python 2.7 with the error stemming from the lines below:

fig.canvas.draw()
img = np.array(fig.canvas.renderer._renderer)

I have tried all other solutions from changing the backend to WXAgg or GTKAgg and explicitly including matplotlib.use("WXAgg") and it still does not work.

I am running on Fedora as well.

Any suggestions? thanks

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
Py_Mel
  • 85
  • 4
  • 12
  • Are you [trying to get the matplotlib figure as numpy array](https://stackoverflow.com/questions/7821518/matplotlib-save-plot-to-numpy-array)? What have you tried to change the backend? In any case this is probably related to using PyCharm, outside of PyCharm this should work, right? – ImportanceOfBeingErnest Jul 06 '18 at 16:23
  • yes it is passing a numpy array... but i have not tried using another ide yet so will try that option. ironically it works on other machines using pycharm too. I have just tried to change the matplotlibrc file to GTKAgg or Agg or WXAgg. nothing works – Py_Mel Jul 08 '18 at 16:15
  • I think PyCharm will ignore the backend set within the matplotlibrc. Not sure how it will react when the backend is set within the scrip with `matplotlib.use`. It's currently not clear from the question if this a PyCharm-only issue but I still suspect it is. – ImportanceOfBeingErnest Jul 08 '18 at 22:00
  • I have tried to run it on Jupyter or Spyder but I need to load the project as a whole to run with its dependencies on other files. the error is just coming from the img line code as shown above. When I click on the renderer there are options on what kind of backend i can use but it doesn't seem to make any impact and the error stays the same. – Py_Mel Jul 09 '18 at 12:02
  • I pretty much gave up on trying to understand how PyCharm manipulated matplotlib to make it work for what they think is "better". So I tagged the question with the PyCharm tag; maybe someone from PyCharm can comment on why their backend does not have a renderer. However for your project, you probably just want to use a different way to get the array, e.g. as shown in the link I provided in the first comment. – ImportanceOfBeingErnest Jul 09 '18 at 12:06
  • Thanks for adding the tag. I followed the solution from the link you provided but the error still stays. I don't understand one reply suggesting: matplotlib.use('agg') before import matplotlib.pyplot as plt which i tried and returned error that matplotlib is not defined. – Py_Mel Jul 09 '18 at 12:41
  • Oh so you did not actually manage to change the backend at all? In order to do so you need `import matplotlib; matplotlib.use("TkAgg"); import matplotlib.pyplot as plt` in that order. Replace `"TkAgg"` with any other backend if you like. – ImportanceOfBeingErnest Jul 09 '18 at 12:46
  • Yes i intuitively corrected the code to import matplotlib first but it still gave me the same renderer error.. now i have tried every config : 'pgf', 'Qt4Cairo', 'cairo', 'MacOSX', 'TkCairo', 'gdk', 'ps', 'GTKAgg', 'nbAgg', 'GTK', 'Qt5Agg', 'template', 'Qt5Cairo', 'WXCairo', 'GTK3Cairo', 'GTK3Agg', 'WX', 'Qt4Agg', 'TkAgg', 'agg', 'svg', 'GTKCairo', 'WXAgg', 'WebAgg', 'pdf' and nothing works :( – Py_Mel Jul 09 '18 at 13:15
  • I had a similar issue once, I just set the python scientific->"show plots in tool window" to false. – user1589759 Oct 28 '19 at 20:53

3 Answers3

5

Simply go to File -> Settings -> Tools -> Python Scientific uncheck the item. Good Luck

3

Made my account just to answer your question. Here is a quick and dirty fix:

Insert FigureCanvasAgg.draw(self) to draw() method to class FigureCanvasInterAgg(FigureCanvasAgg) in %PycharmInstallDir%\helpers\pycharm_matplotlib_backend\backend_interagg.py

I inserted it to line 92 to the version I used. Though not necessary, I suggest making a backup of backend_interagg.py file before modification. The file on my system is read only, so to modify it, you may have to copy it to somewhere else, add this line, and copy it back and overwrite with admin rights.

As ImportanceOfBeingErnest said, it IS a PyCharm problem: the helper function author overrode draw() method of the FigureCanvasAgg class without maintaining its intended functionality.

XY.Li
  • 46
  • 4
1
plt.gcf().canvas.get_renderer()
fig.show()

It worked for me with the Qt5Agg backend and PyCharm IDE.