Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Matplotlib not working
#1
My program is written in python 2.7, and I am not able to use matplotlib. It executes without any errors. But doesn't plot the pie chart
Reply
#2
The basic matplotlib's pie chart demo works for me with python 2.7. Does it also work for you?
Reply
#3
Hi!

In matplotlib you need to select a backend (the real place where the plot is created) The typical backends are Gtk3, Qt5, Wx...
If you do not specify one the default backend is 'Agg', designed to plot to a file and not to a window.
So your script is creating the plot and, as you are using a file-only backend and there is no instruction to save to file just leaving silently.

You need to select a graphical backend... some typical ones are 'GTK3Agg', 'Qt5Agg', 'TkAgg' (see the doc of matplotlib for the full list), but not all of them are normally installed, so try some of them or install the one you want)

There are 3 basic ways to select the backend.
  1. Force it in the script. Your 1st import must be like:
    import matplotlib
    matplotlib.use('Gtk3Agg')
    
    import matplotlib.pyplot as plt
    Only recommended when you really want to force the backend (for example because all the other elements in your application are in Gtk3) although for novices is quite clear what you are doing.

  2. Variable environment. You can add to your environment MPLBACKEND=Qt5Agg to select the backend used. In bash there is a nice way to do this:
    Output:
    ~> MPLBACKEND=Qt5Agg python3 my_script.py
  3. In the general matplotlibrc Useful when you are used to Matplotlib as you can also add your custom options.

And remember, in general, if you use plt.show() you need a GUI backend, if you just use figure.saveas() it might be enough with the 'Agg' backend (that is always present)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Matplotlib: How do I convert Dates from Excel to use in Matplotlib JaneTan 1 3,163 Mar-11-2021, 10:52 AM
Last Post: buran
  hatching not working properly with matplotlib Staph 3 2,950 Jul-28-2019, 07:17 AM
Last Post: ThomasL

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020