1

I want to plot a graph in xkcd style in my PyQt application. Since it is not recommended to use pyplot together with the Qt5Agg-backend I am struggling to find a right way to import the xkcd package so that I can use it without pyplot.

The normal way to use the xkcd-package would be

from matplotlib import pyplot as plt
plt.xkcd()

But I am using

import matplotlib
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg

So is there a way plotting in xkcd style without using pyplot?

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
Tho Re
  • 195
  • 1
  • 2
  • 6
  • How do you "plot" if you don't use `pyplot`? That's not a joke question, I'm genuinly interested how you do the data visualization without it. :) – MSeifert Jun 04 '17 at 10:39
  • @MSeifert matplotlib has a completely object-oriented API, which does not need pyplot. – ImportanceOfBeingErnest Jun 04 '17 at 10:53
  • @ MSeifert As far as I understood it 'pyplot' is just one possible way of plotting mith matplotlib. So it provides the handling of e.g. opening the figure in a separate window, saving it etc. But instead you can directly access the Qt backend when you program a GUI (e.g. with PyQt) by importing the QtAgg. This will let you create a canvas where all the plotting takes place, and which is inserted into a figure. The figure is directly imported from matplotlib (from matplotlib import Figure). And finally you put the figure into your application and visualize your data there. – Tho Re Jun 04 '17 at 10:53
  • @ThoRe What is bad about `plt.xkcd()`? Did you try using it? Do you want to plots some graph in this style and some not? Could you give more details about the actual problem? – ImportanceOfBeingErnest Jun 04 '17 at 10:54
  • @ImportanceOfBeingErnest I know, but without knowing the "how to plot then" it's impossible to answer the question "how to plot using `xkcd` there". At least for me. :) – MSeifert Jun 04 '17 at 10:57
  • In fact, I just tried it and it worked perfectly. I guess what kept me from trying it was that when I tried to import pyplot before, python complained about using pyplot and the Qt5Agg at the same time (maybe in connection with plt.plot). So, honestly I still don't eaxctly know why this simply works. – Tho Re Jun 04 '17 at 11:13
  • No, the question is the inverse. Why would you expect it not to work? [This answer](https://stackoverflow.com/a/42624276/4124317) would be an example of using pyplot in a PyQt GUI. – ImportanceOfBeingErnest Jun 04 '17 at 12:10

1 Answers1

0

There is no danger in importing pyplot. You may just be recommended not using pyplot for plotting tasks when working with figures inside GUIs. I guess this recommendation is the more valid the less experienced you are. Even using pyplot for plotting tasks in GUIs is in general possible as long as you know what you're doing.

But in any case there is no danger in using plt.xkcd(), since it does not interfere with anything and justs sets the rcParams to follow a certain style.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712