In a GTK application using matplotlib, how can I set the color that appears while the figure is being redrawn after a window resize? - matplotlib

I can embed a matplotlib figure in a GTK application by following some of the examples, like this one in the matplotlib documentation. However, when I resize the application window, there is a flicker of white as the figure is being redrawn.
This isn't that much of a problem if the figure I am going to show also has a white background. But if I am using a dark figure style (i.e. to match the appearance of the rest of my application), there is always a white flicker until the figure is drawn in the correct style. This is somewhat jarring if the rest of the application is using a darker theme.
Is there a way I can control the background that is shown by a matplotlib backend while the figure is being drawn?
If I could set the color that appears while drawing, there would still be a flickering effect of the data and axes of the plot being redrawn, but at least the background would stay the same color before and after the figure is drawn.
To illustrate the unwanted effect, if I add a dark plot style to the example linked above, each time I resize the window I can see the white flicker.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from matplotlib.backends.backend_gtk3agg import (
FigureCanvasGTK3Agg as FigureCanvas)
from matplotlib.figure import Figure
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('dark_background') # use the dark style so flicker is more apparent
win = Gtk.Window()
win.connect("delete-event", Gtk.main_quit)
win.set_default_size(400, 300)
win.set_title("Embedding in GTK")
fig = Figure(figsize=(5, 4), dpi=100)
ax = fig.add_subplot()
t = np.arange(0.0, 3.0, 0.01)
s = np.sin(2*np.pi*t)
ax.plot(t, s)
sw = Gtk.ScrolledWindow()
win.add(sw)
# A scrolled window border goes outside the scrollbars and viewport
sw.set_border_width(10)
canvas = FigureCanvas(fig) # a Gtk.DrawingArea
canvas.set_size_request(800, 600)
sw.add(canvas)
win.show_all()
Gtk.main()
(Resizing the window to very small sizes doesn't cause the flickering, presumably because once the figure is drawn at the smallest size, it does not get redrawn, just clipped.)
I'm using GTK 3, matplotlib 3.4.3, and Python 3.8.10 on Ubuntu.

Related

How to keep auto aspect ratio with PowerBI python custom visual matplotlib plot, when facecolor set to transparent?

I have a Python custom visual inside a PowerBI report. Normally, this automatically fills the available space - and set the aspect ratio accordingly. However, when I set a facecolor background color (say None for a transparent visual, but also any other color), then the visual does not fill the space anymore, just keep the default matplotlib aspect ratio. Any ideas on how to force the chart to autofill?
This renders correctly with autofill:
import matplotlib.pyplot as plt
plt.plot(dataset['Column1'],dataset['Column2'])
plt.show()
But this has the aspect ratio problem:
import matplotlib.pyplot as plt
plt.figure(facecolor='red')
plt.plot(dataset['Column1'],dataset['Column2'])
plt.show()

Mpld3: blurry image

I created a matplotlib figure (matplotlib on server) and displaying it with mpld3.draw_figure (nodejs on client) only gives blurry images, similar to the ones here:
How to 'turn off' blurry effect of imshow() in matplotlib?
mpld3.draw_figure(`chartBox-${round}`,chart,false,true)
Interpolation setting 'nearest', 'none', did not change anything:
plt.imshow(G,interpolation='nearest')
I assume its some interpolation problem... maybe also there is a 0.5 shift, so that gridpoints 1 -> 1.5 and are then interpolated.
First switching back to matplotlib 1.5.1 and then
mpl.use('Agg')
solved it
see here:
Generating a PNG with matplotlib when DISPLAY is undefined

How can you scroll through numpy array images in PyQt5?

How can you scroll through Numpy array images in PyQt5? This is the shape of my input images array:
X = [512, 512, 100]
I tried using the onscroll method which uses mouse scroll event and we can mpl_connect it on the canvas (Matplotlib). This seems to work fine but when I put this canvas on PyQt5 canvas, the scrolling stops. Is there any way I can implement this in PyQt5?
I have tried to keep this question simple because I want a generic answer, but if someone is interested, I am ready to share the (non-working) code.

speed up embedded matplotlib plots

I have a fairly complicated qt4 gui with a number of matplotlib plots as shown in the picture. Each plot plots one channel of data and there are 12 channels in total. The vertical scroll bar on the right adjusts the range of channels to plot. The problem is the gui works pretty sluggishly. When the scroll bar is moved, it takes about 2 seconds to refresh the plots. Is there any way to speed it up?
The entire code is fairly large to post here. But I am using
matplotlib.backends.backend_qt4agg as the backend and use canvas.draw() to refresh each plot. Thanks!

Plots Frame (including axes) blackened in Qtconsole

When I launch qtconsole with the --colors=linux option and plot something the frame of the plot is blackened so I cannot see the axes because the qtconsole background is also black.
I used to launch this before without problem but have this problem after a recent update of pandas. I am not sure about what changed but I thought there might be a setting I can change to fix this anyway without worrying about what the update modified that broke this.
It looks like the axes are set to transparent by default (this was not happening before).
The following plots as desired, showing the white axes on black background:
import pandas as pd
import matplotlib.pylab as plt
fig = plt.figure()
fig.patch.set_alpha(1)
temp = pd.Series(range(100))
temp.plot()
I would also like to set this behavior as the default one. I have not been able to do that yet. This seemed like a good lead,
http://matplotlib.org/users/customizing.html
but I could not find an option for exactly that yet.
Any suggestion is welcome. Thank you.