matplotlib windows 10 old looking plot window - python

I have python 2.7.14 on Win10, and installed matplotlib 2.1.2 via pip. I don't like the way the buttons and the whole plot window look like. How can I fix this. It should rather look like in 1.
supposed to be plot window
my plot window

As of matplotlib version >= 2.0 using
import matplotlib.pyplot as plt
plt.gca()
plt.show()
you get this window now:
You can use the classic style (plt.style.use("classic")) to get the old figure style back:
import matplotlib.pyplot as plt
plt.style.use("classic")
plt.gca()
plt.show()
The buttons however are now still different.
In order to change the buttons, you could locate the folder with the buttons,
<path to python>\Lib\site-packages\matplotlib\mpl-data\images
and replace the images in it with the images of your choice. E.g. you might want to download an older matplotlib version and take the images from there.

Related

How to get good plots in Spyder python ? (Inline and Qt5)

When i plot in Spyder with the Backend Qt5 in graphics ( the window that pops with the plot) i don't get things aesthetically beautiful. The title sometimes is not showing, the labels, ... All in all it is just not good looking. But when it is plotted "inline" it is very well presented.
What I want is :
I want the plot with the Backend Qt5 option very well presented like the Backend Inline option.
The code is this :
import numpy as np
from matplotlib import pyplot as plt
plt.plot(np.linspace(0,2, num = 5))
plt.title('Blabla')
plt.xlabel('Anything')
plt.ylabel('Everything')
What I get with the Backend Qt5 option
What I get with the Backend Inline option
I think the command %matplotlib qt might be the solution to this problem. You should write the line in the IPython Console.
The problem was the use of this line in order to increase the quality of the plots:
plt.rcParams['figure.dpi'] = 300
In Qt5 it's not required.

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()

How can I open plot viewer in vs code jupyter?

I am using python to plot in VS code Jupiter on my Mac. The plot is shown in the Interactive View, while I cannot do more thing like zoom or save in the view. I refer to Python Interactive window(see Plot Viewer)and it says:
Within the Python Interactive window, double-click any plot to open it in the viewer, or select the expand button on the upper left corner of the plot.
Expand Button
Plot Viewer
The expand button is supposed to be shown and open the Plot Viewer, while I don't have the button and there is nothing happen after I double-click the plot.
No Button Shows
My test code:
import seaborn as sns
import numpy as np
x = np.random.randn(100)
ax = sns.distplot(x)
Could you please help with my issue? I wonder whether I miss any extension. Thank you very much!
If still does not work please check the version of the Python and Jupyter extensions.
Default the renderer used in Interactive Python windows uses the PNG renderer.
You can change it to use the TkAgg renderer (external window)
Add %matplotlib to the script
%matplotlib
import seaborn as sns
import numpy as np
x = np.random.randn(100)
ax = sns.distplot(x)
You can use other plot renderers

Why the plots look blurry with "TkAgg" backend on retina screen?

I'm using matplotlib to show plots in a GUI app done with tkinter. I've noticed that plots look blurry on the retina screen of my Mac Book Pro.
Below is a minimal example to reproduce my problem. Why am I having the blurry plots with "TkAgg" backend? Is there a way to fix this?
MacOS 10.13.2, Python version: 3.6.4, Matplotlib version: 2.1.2, tkinter.TkVersion: 8.5.
Notice that the default "MacOSX" backend produces nice sharp plots. Besides, if I create a tkinter app, other elements like labels, menus are sharp as well, only the plot is blurry.
import matplotlib
matplotlib.use('TkAgg') # Remove this to compare with MacOSX backend
import matplotlib.pyplot as plt
f = plt.figure(figsize=(6, 4))
a = f.add_subplot(111)
a.plot([1,2,3,4])
plt.show()

Matplotlib - Tcl_AsyncDelete: async handler deleted by the wrong thread?

I'm asking this question because I can't solve one problem in Python/Django (actually in pure Python it's ok) which leads to RuntimeError: tcl_asyncdelete async handler deleted by the wrong thread. This is somehow related to the way how I render matplotlib plots in Django. The way I do it is:
...
import matplotlib.pyplot as plt
...
fig = plt.figure()
...
plt.close()
I extremely minimized my code. But the catch is - even if I have just one line of code:
fig = plt.figure()
I see this RuntimeError happening. I hope I could solve the problem, If I knew the correct way of closing/cleaning/destroying plots in Python/Django.
By default matplotlib uses TK gui toolkit, when you're rendering an image without using the toolkit (i.e. into a file or a string), matplotlib still instantiates a window that doesn't get displayed, causing all kinds of problems. In order to avoid that, you should use an Agg backend. It can be activated like so --
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot
For more information please refer to matplotlib documentation -- http://matplotlib.org/faq/howto_faq.html#matplotlib-in-a-web-application-server
The above (accepted) answer is a solution in a terminal environment. If you debug in an IDE, you still might wanna use 'TkAgg' for displaying data. In order to prevent this issue, apply these two simple rules:
everytime you display your data, initiate a new fig = plt.figure()
don't close old figures manually (e.g. when using a debug mode)
Example code:
import matplotlib
matplotlib.use('TkAgg')
from matplotlib import pyplot as plt
fig = plt.figure()
plt.plot(data[:,:,:3])
plt.show()
This proves to be the a good intermediate solution under MacOS and PyCharm IDE.
If you don't need to show plots while debugging, the following works:
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt
However, if you would like to plot while debugging, you need to do 3 steps:
1.Keep backend to 'TKAgg' as follows:
import matplotlib
matplotlib.use('TKAgg')
from matplot.lib import pyplot as plt
or simply
import matplotlib.pyplot as plt
2.As Fábio also mentioned, you need to add fig(no. #i)=plt.figure(no.#i) for each figure #i. As the following example for plot no.#1, add:
fig1 = plt.figure(1)
plt.plot(yourX,yourY)
plt.show()
3.Add breakpoints. You need to add two breakpoints at least, one somewhere at the beginning of your codes (before the first plot), and the other breakpoint at a point where you would like all plots (before to the second breakpoint) are plotted. All figures are plotted and you even don't need to close any figure manually.
For me, this happened due to parallel access to data by both Matplotlib and by Tensorboard, after Tensorboard's server was running for a week straight.
Rebotting tensorboard tensorboard --logdir . --samples_per_plugin images=100 solved this for me.
I encountered this problem when plotting graphs live with matplotlib in my tkinter application.
The easiest solution I found, was to always delete subplots. I found you didn't need to instantiate a new figure, you only needed to delete the old subplot (using del subplot), then remake it.
Before plotting a new graph, make sure to delete the old subplot.
Example:
f = Figure(figsize=(5,5), dpi=100)
a = f.add_subplot(111)
(For Loop code that updates graph every 5 seconds):
del a #delete subplot
a = f.add_subplot(111) #redefine subplot
Finding this simple solution to fix this "async handler bug" was excruciatingly painful, I hope this helps someone else :)

Categories

Resources