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

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.

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

How to make group bars on jupyter notebook?

I am trying to plot multiple bars on the same plot on my jupyter notebook. However, due to some reason, it does not work on the notebook,but it works on a normal python editor. Here is the code that I have used.
import matplotlib.pyplot as plt
plt.bar(['A','B','C'],[72,73,88])
plt.bar(['A','B','C'],[98,77,98])
plt.show()
Any help will be highly appreciated.
(Edit)
I am looking for somthing like this on my jupyter notebook.
I presume you want to have both bar plots side by side. So here you go:
import matplotlib.pyplot as plt
fig,axs = plt.subplots(1,2)
axs[0].bar(['A','B','C'],[72,73,88])
axs[1].bar(['A','B','C'],[98,77,98])
plt.show()

%matplotlib notebook shows blank icons in Jupyter notebook

I'm trying to create a simple interactive plot with sliders and matplotlib in Jupyter, however, the icons are simply showing a blank square instead? Figure below:
Blank icons using notebook backend
Am I missing a dependency? I believe I'm using default settings for everything.
A simplified version of my code is shown below:
%matplotlib notebook
import matplotlib.pyplot as plt
from ipywidgets import FloatSlider, interact
fig = plt.figure(figsize=(10,3))
ax = fig.add_subplot(1, 1, 1)
ax.set_xlabel('time [s]')
ax.set_ylabel('displacement [cm]')
x1, = ax.plot(t,x)
def update(P_l = FloatSlider(min=0,max=4000,step=50,value=1500)):
x1.set_ydata(x)
fig.canvas.draw()
interact(update)
Thanks in advance!
Jupyter Notebook tends to minimize heavy graphs and tables when it exceeds a specific size. Not sure about the accurate answer, But did you try double clicking the left edge of graph? It does the job in my windows PC.

Matplotlib plot window is black

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

Use both matplotlib inline and qt in jupyter notebook

I am using Jupyter (with IPython) to analyze research data, as well as export figures. I really like the notebook approach offered by Jupyter: when I revisit an experiment after a long time, I can easily see how the figures correspond to the data. This is of course using the inline backend.
However, when I want to explore new data, I prefer to use the QT backend. It is faster than the inline one, and allows to easily scale, zoom in and out, and nicely displays the X and Y coordinates in the bottom left corner. Moreover, I can use the QT backend to determine good x and y limits to use in the inline backend.
I have tried using the %matplotlib notebook magic, but it is simply too slow. For some experiments I am plotting ~500 spectra (each consists of ~1000 data points), which is already slow in the inline backend. Even with less data points, the notebook backend is just too slow to use.
Therefore, I would like to use both the QT backend, and the inline backend whenever I plot something. (So, whenever I execute a cell which plots data, it should both display the inline image, and pop up a QT backend window). This way, I still have a nice overview of plots in my notebook, while also allowing me to easily explore my data. Is there a way to achieve this?
As far as I know it is not possible to use more than one backend at once.
What I do is to switch backend and replot. I have done it with matplotlib.use() or %matplotlib <backend>, according to which version or environment I was working in.
I had the impression that this sometimes doesn't work or maybe it is not responsive, so I need to restart the environment. I imagine it is not easy to maintain it, so I am even surprised it works so smoothly almost always.
Available backends can be listed with:
matplotlib.rcsetup.interactive_bk
matplotlib.rcsetup.non_interactive_bk
matplotlib.rcsetup.all_backends
Hopefully here there are other useful info:
https://matplotlib.org/stable/tutorials/introductory/usage.html
This allows you to run QtConsole, plotting with the plotSin function, both inline and through the QtConsole.
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
..
def plotChirp(Type, Exp, Rand):
# Orignal Chirp Funciton From:
# http://stackoverflow.com/questions/19410042/how-to-make-ipython-notebook-matplotlib-plot-inline
x = np.linspace(0, 3*np.pi, Rand)
plt.plot(x, np.sin(x**int(Exp)))
plt.title('A simple chirp ' + Type)
plt.show()
..
plotChirp("A", 5, 200) # Plots inline if you choose
%connect_info # For your own connection
%qtconsole
QtConsole opens and now you can call your function to plot externally..
Using %matplotlib qt allows for printing in a loop, but unfortunately it seems to overlap the plots. Looking into subplots as a possible solution.
%matplotlib qt
for i in range(0,2):
if i == 0:
plotChirp("B",1, 400)
else:
plotChirp("c",6, 1000)

Categories

Resources