Python 2.7.12
matplotlib 1.5.1
Seaborn 0.8.0
Ubuntu 16.04, with the pip package manager.
I have tried running the codes in seaborn gallery.
They simply do not work when I run a script from terminal. They just say
"seaborn.axisgrid.FacetGrid object at 0x7fdfa554c1d0". Matplotlib works though.
Seaborn does work, however, from the jupyter notebook in my browser. But even then, the plots are really bland and simple, not many interactive customisation options. Nothing like the attractive, feature rich and aesthetically pleasing things you see on tutorials, or the gallery.
Can someone tell me what's wrong?
To show a plot, you need to call
plt.show()
where plt is import matplotlib.pyplot as plt.
Related
When trying to import pyplot from matplotlib, I get this error (python 3.6). It works fine on my other computer, which has python 3.5, but I don't think it changed much.
I've tried both:
from matplotlib import pyplot as plt
and
import matplotlib.pyplot as plt
It's really annoying me that I can't figure this out.
matplotlib targets many different use cases and output formats. Some people use matplotlib interactively from the python shell and have plotting windows pop up when they type commands. Some people embed matplotlib into graphical user interfaces like wxpython or pygtk to build rich applications. Others use matplotlib in batch scripts to generate postscript images from some numerical simulations, and still others in web application servers to dynamically serve up graphs.
To support all of these use cases, matplotlib can target different outputs, and each of these capabilities is called a backend; the “frontend” is the user facing code, i.e., the plotting code, whereas the “backend” does all the hard work behind-the-scenes to make the figure. There are two types of backends: user interface backends (for use in pygtk, wxpython, tkinter, qt4, or macosx; also referred to as “interactive backends”) and hardcopy backends to make image files (PNG, SVG, PDF, PS; also referred to as “non-interactive backends”).
In yout case you have to choose backend "Agg" to use pyplot.
Solution:
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
reference:
https://matplotlib.org/tutorials/introductory/usage.html#what-is-a-backend
https://github.com/matplotlib/matplotlib/issues/9954
Let me guess:
1:Maybe you have not properly installed matplotlib, you can try this:
pip3.6 install --upgrade matplotlib
Or just deleted the package and reinstall
2:Check whether you have use the same config environment and execution environment:
In some cases, you have installed packages for your local python interpreter, but you actually run your program on python virtual env. Maybe you have not properly installed the package on virtual env.
There are many links which state that Matplotlib's plots would look more pleasing due to seaborn's color palette just by importing seaborn like
import seaborn as sns
However, when I do this, the colors and formatting still looks like plain matlotlib defaults for both matplotlib's plot as well as that of seaborn. That is unless the command sns.set() is executed to force reset the seaborn's default palette.
Why is this happening and how to fix this?
How seaborn was installed:
This happens in my Ubuntu 14.04 running Anaconda, although seaborn was installed through pip since the Anaconda version in the system did not come with it preinstalled. I tried installing seaborn through conda for both my environments, py27 and py35. Though the installation was successful, the import did not work, so I proceeded with pip.
Though seaborn works perfectly fine, both environments still import with the same unpleasing matplitlib default colours.
You are using seaborn version 0.8 or higher. The change log tells us:
The default style is no longer applied when seaborn is imported. It is now necessary to explicitly call set() or one or more of set_style(), set_context(), and set_palette(). Correspondingly, the seaborn.apionly module has been deprecated.
There is a very good reason for this. Previously, just because you imported seaborn it would completely alter the matplotlib style, possibly even overwriting previously set matplotlib styles. Both is usually undesired.
As you found out yourself, you need to call seaborn.set() to get the default style of seaborn or use any other of the seaborn styles.
You can also use seaborn styles with matplotlib and not using seaborn at all. E.g.
import matplotlib.pyplot as plt
plt.style.use("seaborn-darkgrid")
or any other style from the style sheet reference
As of late, I can't get my Matplotlib plots to show up. I have a very simple script:
import matplotlib.pyplot as plt
plt.plot([1,2,3])
but nothing ever shows up. If I include the line
plt.show()
then my Python process hangs.
In my ~/.config/matplotlib/matplotlibrc file I have
backend : MacOSX
interactive : True
I'm a little embarrassed to ask this question. I've been a Matplotlib user for many years and have never had this problem. I don't know where to begin to fix this problem. Help!
I'm using Matplotlib 2.0.0 with Python 3.5.2 from Anaconda.
The answer, as pointed out by #ImportanceOfBeingErnest is that the backend configuration for me wasn't working. I'm not sure if I need to install some additional libraries or not. I decided not to use the MacOSX backend and used the Qt5Agg backend. This worked just fine and I didn't have to install any new libraries.
I've just remove the line
interactive: True
enter code here
from my ~/matplotlib/matplotlibrc. It works fine with only backend: MacOSX using the plt.show() command.
I want buttons and other interactive matplotlib objects to appear from within my ipython notebook.
Here is what I've done:
Installed http://datasciencetoolbox.org, it is a vagrant box with ipython installed and version 1.3.1 of matplotlib.
I needed to upgrade matplotlib to the latest version, because it has this capability to do inline interactive plots. What's new in Matplotlib 1.4.1
I needed to run sudo apt-get install pkg-config and
sudo pip install matplotlib --upgradein order to get that going.
Then, in order to produce the nice (i.e. error-free) screenshot below, I went into the .ipython/dst-profile/ipython_notebook_config.py file and erased the line about IPKernelApp.pylab='inline' to be able to run the matplotlib.use('nbagg') command.
Then I was able to create the screenshot below. However, things still look poor. Those buttons are not buttons. That is an image of buttons. Please advise on how to make those buttons come to life!
Oh... and check this out if this helps you help me.
Thanks!
Basically you are facing two issues
the %pylab inlinecall overrides the matplotlib.use('nbagg')call, to use the inline backend instead of the nbagg backend which you are actually wanting. If you use a recent version of IPython (2.3) you can directly use %matplotlib nbagg (or %matplotlib notebook) to load the nbagg backend instead of your %pylabcall.
once you enabled the nbagg backend you will need to explicitly show it, ie. add a plt.show() call at the end of your script -> Update: with IPython 2.3.1 this is no longer needed (thanks #tcaswell for the hint)
With this you get the interactive matplotlib experience embedded in the IPython notebook. However, a quick try of your code does't yield to the desired result. The Button reacts and the callback is executed but the print call doesn't show anything. Anyway, to see that it's working try the following simple example (requires IPython 2.3):
%matplotlib nbagg
from matplotlib.widgets import Button
import matplotlib.pyplot as plt
def callback(event):
plt.text(event.xdata, event.ydata, 'clicked')
f,a = plt.subplots(1)
b1 = Button(a,'Button1')
b1.on_clicked(callback)
plt.show()
Btw. it is highly recommended to use %matplotlib instead of %pylab as later leads to some side effects, see here.
Quite new to python and programming and tried to search for answers to matplotlib plots, but couldn't find answers to my question.
I use Spyder and have anaconda installed. Installed matplotlib but when i run simple plot commands I just get <"matplotlib.figure.Figure at 0x11090c18"> and no plot
If I run it in a dedicated python interpreter i just get an empty plot
The simple code is:
from matplotlib import pyplot as plt
plt.plot([1,2,3,4,5], [2,4,6,8,10])
plt.show()
Is this just the result of some stupid error I made in the installation process?
Hope someone can help med out
I think the error you're seeing is generated by a change the Matplotlib team did some months ago. I tried to fix it in Spyder 2.3.0/1 but maybe it's not working correctly.
To see a plot the code you need to run is
from matplotlib import pyplot as plt
plt.ion()
plt.plot([1,2,3,4,5], [2,4,6,8,10])
plt.show()
Notice the second line, that's what's needed to get interactive plots.