Matplotlib does not show labels or numbers - python

My plots in matplotlib don't show more than one letter/digit.
I do need to use matplotlib from the anaconda folder which is why I'm including sys.path at the beginning. (When I don't include the first 2 lines everything works fine since I have a different matplotlib on my computer. Both are up to date.) Any ideas what I can do to make it work? I'm using Ubuntu and Sublime Text.
Here's my simple code:
import sys
sys.path.insert(0, "/home/mariusz/anaconda/lib/python2.7/site-packages")
import matplotlib.pyplot as plt
grade_fast = [1,7,3,5,6,7,10,11,12]
bumpy_fast = [0,2,3,5,6,7,10,11,12]
grade_slow = [0,2,3,5,6,7,10,11,12]
bumpy_slow = [0,2,3,5,6,7,10,11,12]
plt.scatter(bumpy_fast, grade_fast, label="f")
plt.scatter(grade_slow, bumpy_slow, label="slow")
plt.legend()
plt.xlabel("bumpiness")
plt.ylabel("g")
plt.show()

I re-installed matplotlib, numpy, scikit-learn and scipy and it worked :)
I don't know why though.

Related

Matplotlib styles not being applied

I'm using Spyder v5 within Anaconda and having issues with any styles being applied on charts. Basic chart below (deliberately commented out the sns import to see if it makes any difference -it doesn't)
Basic code below - gives a normal plot as expected
import matplotlib.pyplot as plt
#import seaborn as sns
x_values = [0,1,2,3,4,5,6,7,8,9,10]
x_squared = [x ** 2 for x in x_values]
plt.plot(x_values,x_squared, label = "X-Squared")
plt.plot(x_values,x_cubed, label = "X-Cubed")
plt.show()
If i then try
plt.style.use("seaborn-deep")
plt.plot(x_values,x_squared, label = "X-Squared")
plt.plot(x_values,x_cubed, label = "X-Cubed")
plt.show()
Nothing changes? The style is avaialble (from plt.styles.available) and there is no error when applying the style so something is amiss here.
If i import seaborn then nothing changes
If i try any or all of the following from other solutions in various places in my plt code then nothing much changes (all of the below lines of code work with no errors
%matplotlib inline
plt.rcParams.update(plt.rcParamsDefault)
sns.set()
One time somehow this did seem to force it to change to a seaborn-paper style but that's then the only one i can use and i can't use any other seaborn or other styles at all?? Not sure what forced it to change, think it was sns.set() so I suspect the one it then shows is the default seaborn style, but then why won't it let me use any other styles?
As i'm using SPyder in anaconda the packages are installed to do this plot
I do have Python installed on my base desktop as well... but documentation insists this won't cause a problem and i've not had this problem on any other code ever of having another installation
any tips?

Edit curves lines and axes parameter option in Spyder [duplicate]

I'm using Python 2.6 and PyQt4. The matplotlib backend is set to "Qt4Agg".
The plot shows up, but the option to edit the curve parameters (the one that looks like a green ticked box) is not showing up.
Any ideas?
Edit:
Here is the checkbox that I am NOT seeing:
Sample code:
import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
matplotlib.rcParams['interactive'] = True
plt.ion()
x=[1,2,3]
plt.plot(x,x)
inp = input('Press enter to exit ')
Edit 2:
I've found a clue, but I still don't know what the problem is. I tried importing the NavigationToolbar by
from matplotlib.backends.backend_qt4 import NavigationToolbar2QT
and then accessed the documentation with
print(help(NavigationToolbar2QT))
On Windows where I am using the latest version of Pyzo, a method called "edit_parameters(self)" is present. When I do the same for Python 2.6 on the Linux machine, that method is missing altogether. What could be wrong?
Problem identified as an old version of matplotlib.

Edit curves lines and axes parameter option not showing in matplotlib

I'm using Python 2.6 and PyQt4. The matplotlib backend is set to "Qt4Agg".
The plot shows up, but the option to edit the curve parameters (the one that looks like a green ticked box) is not showing up.
Any ideas?
Edit:
Here is the checkbox that I am NOT seeing:
Sample code:
import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
matplotlib.rcParams['interactive'] = True
plt.ion()
x=[1,2,3]
plt.plot(x,x)
inp = input('Press enter to exit ')
Edit 2:
I've found a clue, but I still don't know what the problem is. I tried importing the NavigationToolbar by
from matplotlib.backends.backend_qt4 import NavigationToolbar2QT
and then accessed the documentation with
print(help(NavigationToolbar2QT))
On Windows where I am using the latest version of Pyzo, a method called "edit_parameters(self)" is present. When I do the same for Python 2.6 on the Linux machine, that method is missing altogether. What could be wrong?
Problem identified as an old version of matplotlib.

matplotlib.pyplot.draw() and matplotlib.pyplot.show() have no effect

In the past I was able to do simple animations with matplotlib with a for loop, but this hasn't worked for some time now.
The standard answer is that you have to turn interactive mode on and/or force a redraw with matplotlib.pyplot.draw(). Here is my minimal working example:
import numpy as np
import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as mplot
mplot.ion()
fig = mplot.figure(1)
ax = fig.add_subplot(111)
for ii in np.arange(0,10):
x = 200*np.random.rand(30)
ax.plot(x)
mplot.draw()
filename = ("img_%d.png" % ii)
mplot.savefig(filename)
When I run this in Interactive Python Editor, I get one figure at the very end with all the plots in it (this also happens with mplot.show())
When I run this in IPython 3.1 (with Python 3.3.5) from the command line, I get nothing at all.
The mplot.savefig(filename) line does seem to work, as the images are generated.
(It's possible this is a bug in the Qt4 backend.)
Try deleting the line matplotlib.use('Qt4Agg'). Works for me. Also works with matplotlib.use('TkAgg'). So it is a backend problem. There is another way to do animations.

make matplotlib plotting window pop up as the active one

I'm working with python and matplotlib on mac os x.
When I'm working on many different windows and I have to run a script which produces a plot, the plot window always open behind the active window and is very frustration having to switch between windows for looking at the image.
Is it any why to decide the location of the plot window, and/or pop up it as foreground window?
thanks
For me (OSX 10.10.2, Matplotlib 1.4.3), what works is changing the matplotlib backend to TkAgg. Before importing pyplot or anything, go:
import matplotlib
matplotlib.use('TkAgg')
Plot windows now pop-up, and can be Command-Tab'ed to.
I was bothered by exactly the same problem. I found finally a solution (in pylab mode, with qt4agg backend):
get_current_fig_manager().window.raise_()
or
fig = gcf()
fig.canvas.manager.window.raise_()
Regards,
Markus
I found this solution was so often needed (e.g. when using Spyder IDE), I wrapped it into a function.
def show_plot(figure_id=None):
if figure_id is None:
fig = plt.gcf()
else:
# do this even if figure_id == 0
fig = plt.figure(num=figure_id)
plt.show()
plt.pause(1e-9)
fig.canvas.manager.window.activateWindow()
fig.canvas.manager.window.raise_()
I found a good answer on this thread:
How to make a Tkinter window jump to the front?
Basically, the idea is to use window attributes - set the '-topmost' attribute to True (1) to make the window come to the foreground, and then set it to False (0) so that it later allows other windows to appear in front of it. Here's code that worked for me:
import matplotlib.pyplot as plt
wm = plt.get_current_fig_manager()
wm.window.attributes('-topmost', 1)
wm.window.attributes('-topmost', 0)
For MacOS Sierra and python 3.6, Matplotlib 2.0.0
import matplotlib.pyplot as plt
plt.get_current_fig_manager().show()
the above line does the job no need of anything else.
This worked for me!!
(Tested on Mac OS X 10.11, Spyder 2.3.5.2 - Python 3.4)
Go to Preferences > IPython console > Graphics and set a backend to Qt (after that you need to restart the kernel).
Make a file that contains:
def raise_window(figname=None):
if figname: plt.figure(figname)
cfm = plt.get_current_fig_manager()
cfm.window.activateWindow()
cfm.window.raise_()
and import it at startup (Preferences > IPython console > Startup > Run a file). Now, just call function raise_window() below your code.
Example:
import numpy as np
import matplotlib.pyplot as plt
X = np.linspace(-np.pi, np.pi, 256)
C, S = np.cos(X), np.sin(X)
plt.figure()
plt.plot(X, C)
plt.plot(X, S)
raise_window()
For me only the following works (with TkAgg backend):
plt.gcf().canvas.get_tk_widget().focus_force()
As of matplotlib 1.5.1 on MacOSX 10.11.6, if you start an iPython (5.0.0, Python: 3.5.2) shell and use %matplotlib you can bring a matplotlib plot to the front using:
>>> %matplotlib
Using matplotlib backend: MacOSX
>>> import matplotlib.pyplot as plt
>>> plt.plot([1,3,2,4])
>>> plt.show()
** Edit: Advice seems to be not to use %pylab as it pollutes the global name space **
.. shell and use %pylab you can bring a matplotlib plot to the front using:
>>> %pylab
Using matplotlib backend: MacOSX
Populating the interactive namespace from numpy and matplotlib
>>> plot([1,3,2,4])
>>> show()
You can set
backend : MacOSX
in your matplotlibrc file for a permanent solution.
It works for me on macos mojave, with matplotlib 2.1.2. However, other users have complained that it does not work for them, so it might be affected by other settings
The following worked on Jupyter notebook with Qt5 backend on Windows. I tested it with Python 3.7, matplotlib 3.2.1.
%matplotlib qt5
import matplotlib.pyplot as plt
import numpy as np
from PyQt5 import QtCore
plt.plot(np.linspace(0,1))
window = plt.get_current_fig_manager().window
window.setWindowFlags(window.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
plt.show()
window.setWindowFlags(window.windowFlags() & ~QtCore.Qt.WindowStaysOnTopHint)
plt.show()

Categories

Resources