2

I'm using getdist to plot some simulation results. In jupyter writing just this line

g = plots.getSubplotPlotter()
g.triangle_plot([samples, samples2], filled=True)

Python will show plots as we can see here.

Now if we want to write it in Python shell and run it with IDLE, this does not produce any plot. plt.show() does not work here.

How to instruct python or matplotlib to show the plots and save them?

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
Ma Y
  • 696
  • 8
  • 19

1 Answers1

1

The problem is that getdist sets the backend to Agg (in this line), which is a non-interactive backend and hence cannot produce an interactive figure via plt.show().
This is pretty bad style, because the user should select the backend, not the package. You might want to inform the developpers about this design flaw.

Anyways it would be possible to switch the backend after importing getdist, via plt.switch_backend(..). As backend you would need to use any interactive backend you have available, e.g. "Qt5Agg" or "TkAgg".

import numpy as np
from getdist import plots, MCSamples
import matplotlib.pyplot as plt

plt.switch_backend("Qt5Agg")

# .. some code ..

g = plots.getSubplotPlotter()
g.triangle_plot([samples, samples2], filled=True)

plt.show()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Thank you but `ImportError: Failed to import any qt binding` – Ma Y Mar 07 '19 at 11:19
  • And when I use `TkAgg`, I get `FileNotFoundError: [WinError 2] The system cannot find the file specified` – Ma Y Mar 07 '19 at 11:20
  • Are you able to plot matplotlib plots without getdist at all? If you start a new console and do `import matplotlib.pyplot as plt; print(plt.get_backend())` what does it show for you? – ImportanceOfBeingErnest Mar 07 '19 at 11:21
  • with so many wonders, Now I tested it and no I cannot. I was using jupyter without any problem and after installing `git-bash` and `gnuparallel` the plotting task has been broken down. I found out now that even plotting a simple line could not be accomplished. Now could you help me what should I do please? – Ma Y Mar 07 '19 at 11:27
  • Answering your question: `TkAgg` – Ma Y Mar 07 '19 at 11:28
  • Then it looks like your tk install is broken. Maybe you can reinstall tk? – ImportanceOfBeingErnest Mar 07 '19 at 11:29
  • I reinstalled, `Ipython`, `jupyter`, `matplotlib`, `kernel` and the problem persists. Now I should do ` pip uninstall python-tk`? and then install? Could you tell me how to install it? I am unacquainted with this parts of Python – Ma Y Mar 07 '19 at 11:31
  • [Yes.](https://stackoverflow.com/questions/4783810/install-tkinter-for-python) For python 3 it might be `python3-tk` though. Not sure which system you are using. – ImportanceOfBeingErnest Mar 07 '19 at 11:34
  • I am on windows 64 bit, python 3.6.3. But the `pip install python3-tk` does not work for me] – Ma Y Mar 07 '19 at 11:36
  • This link was for linux, I installed ACTIVETCL but there is the same problem. should I reinstall the windows? – Ma Y Mar 07 '19 at 12:06
  • No, don't reinstall windows. This is a python problem, so in the limit you can completely reinstall python and all packages if nothing else helps. I'm sorry I cannot be of more help here, I never experienced problems with tk not working correctly. – ImportanceOfBeingErnest Mar 07 '19 at 12:10
  • thank you, I will do it. But I think it cannot work. Because I install a python 2.7 while I had py3.6and that one had this problem on my system. – Ma Y Mar 07 '19 at 12:12