Matplotlib plot window is black - python

I recently started studying python and matplotlib. The problem I face is that I always get a black window as a plot output.
See simple code below.
import numpy as np
import matplotlib.pyplot as plt
import pylab
x = np.arange(0, 5, 0.1)
y = np.sin(x)
#plot the x and y and you are supposed to see a sine curve
plt.plot(x, y)
pylab.show()
I get the same result executing code samples from matplotlib pages.
Does anyone have any idea where this comes from and how to overcome this?
I'm using the Operating system Linux Mint.

I executed code in python shell and it works, the problem comes executing code in python editors. I worked with two different editors.
Best regards
Robert

Related

Matplotlib bbox_inches issue

I have this very simple code:
import math
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2*math.pi)
fig, ax = plt.subplots()
ax.plot(x,np.sin(x))
plt.savefig('sinwave.png', bbox_inches='tight')
plt.show()
So what I would expect is that there is white background inside my plotting area, but no background around it, e.g. transparent background (only) behind the ticks and tick labels.
However, something is going wrong and it isn't working, the whole background is white. When I send it to a friend it works exactly as expected.
So I thought creating a new environment might fix it but the issue persisted. I tried it in ipython, jupyter lab and jupyter notebook, all with the same result (as to be expected but I just thought I would try). I'm using python 3.10.8 and matplotlib 3.6.2 on a Mac (2019 MacBook Pro) but it also doesn't work with other versions of python or matplotlib.
This is just a simple example but I have some 3-4 month old code which did exactly what I want back then and is now not doing it any more. Very odd.
Any ideas what might be going wrong? I really thought the new environment would fix the issue but no luck.
I'm fairly new to the community so let me know if you need any further details about my system (as it seems to be an issue with my computer/python install maybe??).
Cheers
Markus
Below are both images, you can't see the difference here as it's on white background but it's straight forward if opened e.g. in illustrator or photoshop.
fig = plt.figure()
fig.patch.set_facecolor('white')
fig.patch.set_alpha(0.6) #play with the alpha value
ax = fig.add_subplot(111)
ax.patch.set_facecolor('white')
ax.patch.set_alpha(0.0) #play with the alpha value
Then simply plot here
Plt.scatter(x,y)
Plt.show()

Visual Stdio Code, PyCharm, and Thonny behave differently running simple matplotlib demo code

I want to use Python's matplotlib to display some sensor data in 3D and in real time. My starting place was to try do just plot a sine wave from one of the many examples from the web. A "Hello World" if you will.
I started with a fresh install of PyCharm on my reasonably fresh Windows 11 PC. This setup works fine for things that don't involve matplotlib.
After a while I came to the conclusion that PyCharm was just not working the way I expected, and for the fun of it ran the code using Thonny (yeah, I have several Raspberry Pis), and it worked as expected.
Just because I could, I tried the same code using Visual Studio Code 2022. It didn't work the same way PyCharm didn't.
PyCharm and VS Code would output the printed "value" and open the "figure" window, but would not display the sine wave plot. Eventually the "figure" window would say "not responding" for both.
The Thonny output was as expected, and there was no "not responding" message.
The only difference I see is that the PyCharm and VS Code use virtual environments, and Thonny does not.
I'll admit to not being the sharpest knife in the drawer, and will be appreciative of suggestions.
Edit -- Also, running the code from the command line (Windows Terminal (Admin) acts the same way as for PyCharm and VS Code. /Edit
The code in question is:
import numpy as np
import time
import matplotlib.pyplot as plt
figure, ax = plt.subplots(figsize=(4,5))
x = np.linspace(0, 20, 80)
y = np.sin(x)
plt.ion()
plot1, = ax.plot(x, y)
plt.xlabel("X-Axis",fontsize=18)
plt.ylabel("Y-Axis",fontsize=18)
for value in range(25):
update_y_value = np.sin(x-2.5*value)
plot1.set_xdata(x)
plot1.set_ydata(update_y_value)
figure.canvas.draw()
figure.canvas.flush_events()
time.sleep(0.05)
print(value)
plt.show()
I was confused by this at one point, basically for displaying graphics on the screen it's much easier if running directly on the machine since virtual terminals don't have access to your screen by default. There are ways to get around it and give them this access (https://virtualizationreview.com/articles/2017/02/08/graphical-programs-on-windows-subsystem-on-linux.aspx), but often it's much easier to just run the program from Windows natively using something like CMD (or it seems like Thonny worked for you). You also might want to consider trying out Jupyter Notebook depending on what you're doing. Using virtual subsystems like WSL has given me lots of problems with this in the past.
I finally found an example that I can use as a starting place for what I really want to do. The following sample runs reliably in all of VS Code, PyCharm and Thonny IDEs.
That snealy little "plt.pause(1e-17)" seems to make a lot of difference.
import numpy as np
import matplotlib.pyplot as plt
xdata = []
ydata = []
axes = plt.gca()
axes.set_xlim(0, 100)
axes.set_ylim(-2, +2)
line, = axes.plot(xdata, ydata, 'r-')
x = np.linspace(0, 19, 100)
y = np.sin(x)
plt.xlabel("X-Axis", fontsize=18)
plt.ylabel("Y-Axis", fontsize=18)
for value in range(100):
xdata.append(value)
ydata.append(y[value])
line.set_xdata(xdata)
line.set_ydata(ydata)
plt.pause(1e-17)
plt.show()

Simple Python animation using pause command not working

I wanted to create a simple animation using plt.pause() command in python. So I tried the first example from this page. If you go there, it shows you a nice animation of a graph growing smoothly. I copy-paste it below
from matplotlib import pyplot as plt
x = []
y = []
for i in range(100):
x.append(i)
y.append(i)
# Mention x and y limits to define their range
plt.xlim(0, 100)
plt.ylim(0, 100)
# Ploting graph
plt.plot(x, y, color = 'green')
plt.pause(0.01)
plt.show()
However, it is giving me 100 different plots with an incremental increase until it completes the range. I think the problem is with figures appearing without waiting for the plt.show() command? Help me. I am using Jupiter 6.0.3 and sypder 4.1.3
Without the .show() command the plot will disappear at the completion of the code. however the animation should still be visible as it is created by the .plot() command.
I am not able to reproduce your problem using Python 3.9.7.
You can always try running the script outside of the IDE using python pathtoscript and see if that resolves the issue.
To build on #Regretful's answer, if you're running in a Jupyter notebook, use %matplotlib qt or some other non-inline backend to get the usual pop-up window, rather than static images.
No idea for Spyder, though!

Is the "retina" configuration of Matplotlib dependent on subplot arrangement?

I am getting what seems to be a very annoying bug on Matplotlib when working on Jupyter Notebook. First consider this simple code:
import numpy as np
import matplotlib.pyplot as plt
%config InlineBackend.figure_format='retina'
plt.style.use('ggplot')
x = np.linspace(0, 1, 100)
y = np.cos(x)
z = np.sin(x)
Let's say I want to plots: (X, Y) and (X, Z) in the same figure. Apparently, the resolution at which the plot is displayed on Jupyter Notebook depends on whether I use two columns or two rows.
This plot is not "retina" at all. Now, If I try another configuration, I have the following:
This latter image is displayed with much more resolution (not sure if one can see that here on the website but I can clearly see on the screen of my laptop).
When I try to save the figure, no wonder both have the same resolution. This makes me wonder if there is something fishy in the connection between Matplotlib and the Jupyter notebook. Am I missing something? Can someone reproduce the error?
I could reproduce the error on a Google Colab notebook by the way.
I am on matplotlib version 3.2.2, with Jupyter notebook on version 6.4.6.

Incorrect backend configuration with macosx in (old) matplotlib: plt.ion different from savefig, here overwritten alpha keyword

I am using interactive python with plt.ion() for generating figures (v2.7) and have noticed that the figure looks different from the figure exported by savefig (this is not a DPI issue (cf. matplotlib savefig() plots different from show()) - I think it might be a backend issue, but would appreciate help as I don't understand this properly).
Specifically, I wanted visualise the importance of a series of points by the intensity of their colour, which I thought I could do with the "alpha" keyword in matplotlib.
When I just do this, this works fine,
but when I want to add a line to the figure, the alpha keyword seemed to not work any more, and plt.ion() shows this:
I initially thought that perhaps the following issue on github may be related:
https://github.com/matplotlib/matplotlib/issues/4580
but then I noticed that exporting the figure actually produced the following file (i.e. as desired):
It would be great to understand a bit better what is going on, and how I can avoid such issues in the future. Is plt.ion()/plt.show() not the best way to show figures in interactive python, or is this an issue with the alpha keyword?
The code is here:
import numpy as np
from numpy import random as random
from matplotlib import pyplot as plt
fig2,ax2=plt.subplots(1,1,figsize=(3,3),sharey=True)
for ii in range(1):
ax2.plot(np.linspace(0,200,200), [0.1]*200, c= 'k')
for i in range(200):
test2=random.randint(5)
ydata= random.rand(test2)
test = random.rand(test2)
for j in range(test2):
ax2.plot(i,ydata[j],'o',ms=4, c= 'Darkblue',alpha=test[j],markeredgecolor='None')

Categories

Resources