MoviePy + VisPy only saving black screen to .gif - python

I have a script that animates a simple canvas using VisPy (in the "bad" way) successfully. However, when i export the animation to file, whether it's a .gif, or video formats (.MP4, etc) most of the time it produces a completely black image that doesn't change. Other, seemingly random times it does actually save the correct animation. I have literally tried everything I can think of.
import vispy
import vispy.scene
from make_shape import Make_shape
from vispy.scene import visuals
from vispy import app, gloo
from moviepy.editor import VideoClip
from vispy.gloo.util import _screenshot
# Canvas setup
canvas = vispy.scene.SceneCanvas(keys='interactive', show=True, resizable=True)
view = canvas.central_widget.add_view()
# Initial shape data
cube = Make_shape(3)
verts = cube.verts()
linepos = cube.frame(verts)
# Adds initial visuals to shape data
# lines can be done as a function with LinePlot
scatter = visuals.Markers()
lines = visuals.Line()
axis = visuals.XYZAxis(parent=view.scene)
scatter.set_data(verts, edge_color=None, face_color=(1, 1, 1, .5), size=10)
lines.set_data(pos=linepos, connect="segments")
view.add(scatter)
view.add(lines)
# Animation of visuals
def update(ev):
global verts, scatter
verts += 0.0005
linepos = cube.frame(verts)
scatter.set_data(pos=verts)
lines.set_data(pos=linepos)
#Ttime = 5
#intv = 0.000001
#intr = int(Ttime / intv)
timer = app.Timer()
timer.connect(update)
timer.start(interval=.01, iterations=500)
# Setup of camera
# view.camera = 'turntable'
# view.camera = 'perspective'
view.camera = 'arcball'
view.camera.center = (0.5, 0.5, 0.5)
view.camera.distance = 100
#view.camera.set_range(x=(-0.5, 1))
view.camera.fov = (1)
# Runs animation
if __name__ == '__main__':
import sys
if sys.flags.interactive != 1:
vispy.app.run()
# Writes animation to file
def make_frame(t):
update(t)
canvas.on_draw(None)
return _screenshot((0, 0, canvas.size[0], canvas.size[1]))[:, :, :3]
animation = VideoClip(make_frame, duration=5)
animation.write_gif("test_animation2.gif", fps=24) # export as GIF (slow)
# animation.write_videofile(fname+".mp4", fps=24) # export as video

Related

Size in pixels of x-axis from a matplotlib figure embedded in a PyQt5 window

I've got a live matplotlib graph in a PyQt5 window:
You can read more about how I got this code working here:
How to make a fast matplotlib live plot in a PyQt5 GUI
Please copy-paste the code below to a python file, and run it with Python 3.7:
#####################################################################################
# #
# PLOT A LIVE GRAPH IN A PYQT WINDOW #
# #
#####################################################################################
from __future__ import annotations
from typing import *
import sys
import os
from PyQt5 import QtWidgets, QtCore
from matplotlib.backends.backend_qt5agg import FigureCanvas
import matplotlib as mpl
import matplotlib.figure as mpl_fig
import matplotlib.animation as anim
import matplotlib.style as style
import numpy as np
style.use('ggplot')
class ApplicationWindow(QtWidgets.QMainWindow):
'''
The PyQt5 main window.
'''
def __init__(self):
super().__init__()
# 1. Window settings
self.setGeometry(300, 300, 800, 400)
self.setWindowTitle("Matplotlib live plot in PyQt")
self.frm = QtWidgets.QFrame(self)
self.frm.setStyleSheet("QWidget { background-color: #eeeeec; }")
self.lyt = QtWidgets.QVBoxLayout()
self.frm.setLayout(self.lyt)
self.setCentralWidget(self.frm)
# 2. Place the matplotlib figure
self.myFig = MyFigureCanvas(x_len=200, y_range=[0, 100], interval=20)
self.lyt.addWidget(self.myFig)
# 3. Show
self.show()
return
class MyFigureCanvas(FigureCanvas, anim.FuncAnimation):
'''
This is the FigureCanvas in which the live plot is drawn.
'''
def __init__(self, x_len:int, y_range:List, interval:int) -> None:
'''
:param x_len: The nr of data points shown in one plot.
:param y_range: Range on y-axis.
:param interval: Get a new datapoint every .. milliseconds.
'''
FigureCanvas.__init__(self, mpl_fig.Figure())
# Range settings
self._x_len_ = x_len
self._y_range_ = y_range
# Store two lists _x_ and _y_
x = list(range(0, x_len))
y = [0] * x_len
# Store a figure and ax
self._ax_ = self.figure.subplots()
self._ax_.set_ylim(ymin=self._y_range_[0], ymax=self._y_range_[1])
self._line_, = self._ax_.plot(x, y)
# Call superclass constructors
anim.FuncAnimation.__init__(self, self.figure, self._update_canvas_, fargs=(y,), interval=interval, blit=True)
return
def _update_canvas_(self, i, y) -> None:
'''
This function gets called regularly by the timer.
'''
y.append(round(get_next_datapoint(), 2)) # Add new datapoint
y = y[-self._x_len_:] # Truncate list _y_
self._line_.set_ydata(y)
# Print size of bounding box (in pixels)
bbox = self.figure.get_window_extent().transformed(self.figure.dpi_scale_trans.inverted())
width, height = bbox.width * self.figure.dpi, bbox.height * self.figure.dpi
print(f"bbox size in pixels = {width} x {height}")
return self._line_,
# Data source
# ------------
n = np.linspace(0, 499, 500)
d = 50 + 25 * (np.sin(n / 8.3)) + 10 * (np.sin(n / 7.5)) - 5 * (np.sin(n / 1.5))
i = 0
def get_next_datapoint():
global i
i += 1
if i > 499:
i = 0
return d[i]
if __name__ == "__main__":
qapp = QtWidgets.QApplication(sys.argv)
app = ApplicationWindow()
qapp.exec_()
1. The problem
I need to know the nr of pixels from x_min to x_max:
Please notice that the x-axis actually goes beyond the x_min and x_max borders. I don't need to know the total length. Just the length from x_min to x_max.
2. What I tried so far
I already found a way to get the graph's bounding box. Notice the following codelines in the _update_canvas_() function:
# Print size of bounding box (in pixels)
bbox = self.figure.get_window_extent().transformed(self.figure.dpi_scale_trans.inverted())
width, height = bbox.width * self.figure.dpi, bbox.height * self.figure.dpi
print(f"bbox size in pixels = {width} x {height}")
That gave me a bounding box size of 778.0 x 378.0 pixels. It's a nice starting point, but I don't know how to proceed from here.
I also noticed that this bounding box size isn't printed out correctly from the first go. The first run of the _update_canvas_() function prints out a bouding box of 640.0 x 480.0 pixels, which is just plain wrong. From the second run onwards, the printed size is correct. Why?
Edit
I tried two solutions. The first one is based on a method described by #ImportanceOfBeingErnes (see Axes class - set explicitly size (width/height) of axes in given units) and the second one is based on the answer from #Eyllanesc.
#####################################################################################
# #
# PLOT A LIVE GRAPH IN A PYQT WINDOW #
# #
#####################################################################################
from __future__ import annotations
from typing import *
import sys
import os
from PyQt5 import QtWidgets, QtCore
from matplotlib.backends.backend_qt5agg import FigureCanvas
import matplotlib as mpl
import matplotlib.figure as mpl_fig
import matplotlib.animation as anim
import matplotlib.style as style
import numpy as np
style.use('ggplot')
def get_width_method_a(ax, dpi, canvas):
l = float(ax.figure.subplotpars.left)
r = float(ax.figure.subplotpars.right)
x, y, w, h = ax.figure.get_tightbbox(renderer=canvas.get_renderer()).bounds
return float(dpi) * float(w - (l + r))
def get_width_eyllanesc(ax):
""" Based on answer from #Eyllanesc"""
""" See below """
y_fake = 0
x_min, x_max = 0, 200
x_pixel_min, _ = ax.transData.transform((x_min, y_fake))
x_pixel_max, _ = ax.transData.transform((x_max, y_fake))
return x_pixel_max - x_pixel_min
class ApplicationWindow(QtWidgets.QMainWindow):
'''
The PyQt5 main window.
'''
def __init__(self):
super().__init__()
# 1. Window settings
self.setGeometry(300, 300, 800, 400)
self.setWindowTitle("Matplotlib live plot in PyQt")
self.frm = QtWidgets.QFrame(self)
self.frm.setStyleSheet("QWidget { background-color: #eeeeec; }")
self.lyt = QtWidgets.QVBoxLayout()
self.frm.setLayout(self.lyt)
self.setCentralWidget(self.frm)
# 2. Place the matplotlib figure
self.myFig = MyFigureCanvas(x_len=200, y_range=[0, 100], interval=20)
self.lyt.addWidget(self.myFig)
# 3. Show
self.show()
return
class MyFigureCanvas(FigureCanvas, anim.FuncAnimation):
'''
This is the FigureCanvas in which the live plot is drawn.
'''
def __init__(self, x_len:int, y_range:List, interval:int) -> None:
'''
:param x_len: The nr of data points shown in one plot.
:param y_range: Range on y-axis.
:param interval: Get a new datapoint every .. milliseconds.
'''
FigureCanvas.__init__(self, mpl_fig.Figure())
# Range settings
self._x_len_ = x_len
self._y_range_ = y_range
# Store two lists _x_ and _y_
x = list(range(0, x_len))
y = [0] * x_len
# Store a figure and ax
self._ax_ = self.figure.subplots()
self._ax_.set_ylim(ymin=self._y_range_[0], ymax=self._y_range_[1])
self._line_, = self._ax_.plot(x, y)
self._line_.set_ydata(y)
print("")
print(f"width in pixels (first call, method is 'method_a') = {get_width_method_a(self._ax_, self.figure.dpi, self)}")
print(f"width in pixels (first call, method is 'eyllanesc') = {get_width_eyllanesc(self._ax_)}")
# Call superclass constructors
anim.FuncAnimation.__init__(self, self.figure, self._update_canvas_, fargs=(y,), interval=interval, blit=True)
return
def _update_canvas_(self, i, y) -> None:
'''
This function gets called regularly by the timer.
'''
y.append(round(get_next_datapoint(), 2)) # Add new datapoint
y = y[-self._x_len_:] # Truncate list _y_
self._line_.set_ydata(y)
print("")
print(f"width in pixels (method is 'method_a') = {get_width_method_a(self._ax_, self.figure.dpi, self)}")
print(f"width in pixels (method is 'eyllanesc') = {get_width_eyllanesc(self._ax_)}")
return self._line_,
# Data source
# ------------
n = np.linspace(0, 499, 500)
d = 50 + 25 * (np.sin(n / 8.3)) + 10 * (np.sin(n / 7.5)) - 5 * (np.sin(n / 1.5))
i = 0
def get_next_datapoint():
global i
i += 1
if i > 499:
i = 0
return d[i]
if __name__ == "__main__":
qapp = QtWidgets.QApplication(sys.argv)
app = ApplicationWindow()
qapp.exec_()
Conclusions:
The correct answer is 550 pixels, which is what I measured on a printscreen. Now, I get the following output printed when I run the program:
width in pixels (first call, method is 'method_a') = 433.0972222222222
width in pixels (first call, method is 'eyllanesc') = 453.1749657377798
width in pixels (method is 'method_a') = 433.0972222222222
width in pixels (method is 'eyllanesc') = 453.1749657377798
width in pixels (method is 'method_a') = 540.0472222222223
width in pixels (method is 'eyllanesc') = 550.8908177249887
...
The first call for both methods gives the wrong result.
From the third(!) call onwards, they both give pretty good results, with the method from #Eyllanesc being the winner.
How do I fix the problem of the wrong result for the first call?
For an old answer I had to do calculation, which in your case is:
y_fake = 0
x_min, x_max = 0, 200
x_pixel_min, _ = self._ax_.transData.transform((x_min, y_fake))
x_pixel_max, _ = self._ax_.transData.transform((x_max, y_fake))
print(
f"The length in pixels between x_min: {x_min} and x_max: {x_max} is: {x_pixel_max - x_pixel_min}"
)
Note:
The calculations take into account what is painted, so in the first moments it is still being painted so the results are correct but our eyes cannot distinguish them. If you want to obtain the correct size without the animation you must calculate that value when the painting is stabilized, which is difficult to calculate, a workaround is to use a QTimer to make the measurement a moment later:
# ...
self._ax_ = self.figure.subplots()
self._ax_.set_ylim(ymin=self._y_range_[0], ymax=self._y_range_[1])
self._line_, = self._ax_.plot(x, y)
QtCore.QTimer.singleShot(100, self.calculate_length)
# ...
def calculate_length(self):
y_fake = 0
x_min, x_max = 0, 200
x_pixel_min, _ = self._ax_.transData.transform((x_min, y_fake))
x_pixel_max, _ = self._ax_.transData.transform((x_max, y_fake))
print(
f"The length in pixels between x_min: {x_min} and x_max: {x_max} is: {x_pixel_max - x_pixel_min}"
)

How do I integrate the pyqtgraph image example into a class?

I am trying to integrate the pyqtgraph example into a class.
However, since the example uses "global" to acces important methods, I am having trouble translating it into a class.
The example:
import initExample ## Add path to library (just for examples; you do not need this)
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
# Interpret image data as row-major instead of col-major
pg.setConfigOptions(imageAxisOrder='row-major')
pg.mkQApp()
win = pg.GraphicsLayoutWidget()
win.setWindowTitle('pyqtgraph example: Image Analysis')
# A plot area (ViewBox + axes) for displaying the image
p1 = win.addPlot()
# Item for displaying image data
img = pg.ImageItem()
p1.addItem(img)
# Custom ROI for selecting an image region
roi = pg.ROI([-8, 14], [6, 5])
roi.addScaleHandle([0.5, 1], [0.5, 0.5])
roi.addScaleHandle([0, 0.5], [0.5, 0.5])
p1.addItem(roi)
roi.setZValue(10) # make sure ROI is drawn above image
# Isocurve drawing
iso = pg.IsocurveItem(level=0.8, pen='g')
iso.setParentItem(img)
iso.setZValue(5)
# Contrast/color control
hist = pg.HistogramLUTItem()
hist.setImageItem(img)
win.addItem(hist)
# Draggable line for setting isocurve level
isoLine = pg.InfiniteLine(angle=0, movable=True, pen='g')
hist.vb.addItem(isoLine)
hist.vb.setMouseEnabled(y=False) # makes user interaction a little easier
isoLine.setValue(0.8)
isoLine.setZValue(1000) # bring iso line above contrast controls
# Another plot area for displaying ROI data
win.nextRow()
p2 = win.addPlot(colspan=2)
p2.setMaximumHeight(250)
win.resize(800, 800)
win.show()
# Generate image data
data = np.random.normal(size=(200, 100))
data[20:80, 20:80] += 2.
data = pg.gaussianFilter(data, (3, 3))
data += np.random.normal(size=(200, 100)) * 0.1
img.setImage(data)
hist.setLevels(data.min(), data.max())
# build isocurves from smoothed data
iso.setData(pg.gaussianFilter(data, (2, 2)))
# set position and scale of image
img.scale(0.2, 0.2)
img.translate(-50, 0)
# zoom to fit imageo
p1.autoRange()
# Callbacks for handling user interaction
def updatePlot():
global img, roi, data, p2
selected = roi.getArrayRegion(data, img)
p2.plot(selected.mean(axis=0), clear=True)
roi.sigRegionChanged.connect(updatePlot)
updatePlot()
def updateIsocurve():
global isoLine, iso
iso.setLevel(isoLine.value())
isoLine.sigDragged.connect(updateIsocurve)
## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
What I tried: (only changed parts)
def updatePlot(img, roi, data, p2):
#global img, roi, data, p2
selected = roi.getArrayRegion()
p2.plot(selected.mean(axis=0), clear=True)
roi.sigRegionChanged.connect(updatePlot(img, roi, data, p2))
updatePlot(img, roi, data, p2)
def updateIsocurve(isoLine, iso):
# global isoLine, iso
so.setLevel(isoLine.value())
isoLine.sigDragged.connect(updateIsocurve(isoLine, iso))
This gives an error, since the "img" object I am giving it instead of accessing it through "global" seems to be of type None.
I don't know how to give the update function access to the necessary objects.
Make all the variables into instance variables by using self
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
class ImageWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(ImageWidget, self).__init__(parent)
# Interpret image data as row-major instead of col-major
pg.setConfigOptions(imageAxisOrder='row-major')
pg.mkQApp()
self.win = pg.GraphicsLayoutWidget()
self.win.setWindowTitle('pyqtgraph example: Image Analysis')
# A plot1 area (ViewBox + axes) for displaying the image
self.plot1 = self.win.addPlot()
# Item for displaying image data
self.item = pg.ImageItem()
self.plot1.addItem(self.item)
# Custom ROI for selecting an image region
self.ROI = pg.ROI([-8, 14], [6, 5])
self.ROI.addScaleHandle([0.5, 1], [0.5, 0.5])
self.ROI.addScaleHandle([0, 0.5], [0.5, 0.5])
self.plot1.addItem(self.ROI)
self.ROI.setZValue(10) # make sure ROI is drawn above image
# Isocurve drawing
self.iso = pg.IsocurveItem(level=0.8, pen='g')
self.iso.setParentItem(self.item)
self.iso.setZValue(5)
# Contrast/color control
self.hist = pg.HistogramLUTItem()
self.hist.setImageItem(self.item)
self.win.addItem(self.hist)
# Draggable line for setting isocurve level
self.isoLine = pg.InfiniteLine(angle=0, movable=True, pen='g')
self.hist.vb.addItem(self.isoLine)
self.hist.vb.setMouseEnabled(y=False) # makes user interaction a little easier
self.isoLine.setValue(0.8)
self.isoLine.setZValue(1000) # bring iso line above contrast controls
# Another plot1 area for displaying ROI data
self.win.nextRow()
self.plot2 = self.win.addPlot(colspan=2)
self.plot2.setMaximumHeight(250)
self.win.resize(800, 800)
self.win.show()
# Generate image self.data
self.data = np.random.normal(size=(200, 100))
self.data[20:80, 20:80] += 2.
self.data = pg.gaussianFilter(self.data, (3, 3))
self.data += np.random.normal(size=(200, 100)) * 0.1
self.item.setImage(self.data)
self.hist.setLevels(self.data.min(), self.data.max())
# build isocurves from smoothed self.data
self.iso.setData(pg.gaussianFilter(self.data, (2, 2)))
# set position and scale of image
self.item.scale(0.2, 0.2)
self.item.translate(-50, 0)
# zoom to fit imageo
self.plot1.autoRange()
self.ROI.sigRegionChanged.connect(self.updatePlot)
self.updatePlot()
self.isoLine.sigDragged.connect(self.updateIsocurve)
# Callbacks for handling user interaction
def updatePlot(self):
selected = self.ROI.getArrayRegion(self.data, self.item)
self.plot2.plot(selected.mean(axis=0), clear=True)
def updateIsocurve(self):
self.iso.setLevel(self.isoLine.value())
## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
app = QtGui.QApplication([])
app.setStyle(QtGui.QStyleFactory.create("Cleanlooks"))
image_widget = ImageWidget()
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()

tkinter keep a handle alive

Hey there I have the following code:
import matplotlib as mpl
import numpy as np
import sys
if sys.version_info[0] < 3:
import Tkinter as tk
else:
import tkinter as tk
import matplotlib.backends.tkagg as tkagg
from matplotlib.backends.backend_agg import FigureCanvasAgg
def draw_figure(canvas, figure, loc=(0, 0)):
""" Draw a matplotlib figure onto a Tk canvas
loc: location of top-left corner of figure on canvas in pixels.
Inspired by matplotlib source: lib/matplotlib/backends/backend_tkagg.py
"""
figure_canvas_agg = FigureCanvasAgg(figure)
figure_canvas_agg.draw()
figure_x, figure_y, figure_w, figure_h = figure.bbox.bounds
figure_w, figure_h = int(figure_w), int(figure_h)
photo = tk.PhotoImage(master=canvas, width=figure_w, height=figure_h)
# Position: convert from top-left anchor to center anchor
canvas.create_image(loc[0] + figure_w/2, loc[1] + figure_h/2, image=photo)
# Unfortunately, there's no accessor for the pointer to the native renderer
tkagg.blit(photo, figure_canvas_agg.get_renderer()._renderer, colormode=2)
# Return a handle which contains a reference to the photo object
# which must be kept live or else the picture disappears
return photo
# Create a canvas
w, h = 300, 200
window = tk.Tk()
window.title("A figure in a canvas")
canvas = tk.Canvas(window, width=w, height=h)
canvas.pack()
# Generate some example data
X = np.linspace(0, 2 * np.pi, 50)
Y = np.sin(X)
# Create the figure we desire to add to an existing canvas
fig = mpl.figure.Figure(figsize=(2, 1))
ax = fig.add_axes([0, 0, 1, 1])
ax.plot(X, Y)
# Keep this handle alive, or else figure will disappear
fig_x, fig_y = 100, 100
fig_photo = draw_figure(canvas, fig, loc=(fig_x, fig_y))
fig_w, fig_h = fig_photo.width(), fig_photo.height()
# Add more elements to the canvas, potentially on top of the figure
canvas.create_line(200, 50, fig_x + fig_w / 2, fig_y + fig_h / 2)
canvas.create_text(200, 50, text="Zero-crossing", anchor="s")
Code from here:
https://matplotlib.org/2.1.1/gallery/user_interfaces/embedding_in_tk_canvas_sgskip.html
However I would like to draw different figures in a function. But when I try to do that my handle always dies and I do not see any figure on my tkinter....
Any ideas how to do keep a handle alive inside a function?

Matplotlib Embed a graph in to a UI PyQt5

I have a function in another script that draws a graph, so the graph is already predrawn, I just want to place it in to a widget on my interface in PyQt5. I have imported it, and when it runs it opens two windows, one with the graph in and one with the user interface in. Any ideas?
Here is the code:
def minionRatioGraph(recentMinionRatioAvg):
x = recentMinionRatioAvg
a = x*10
b = 100-a
sizes = [a, b]
colors = ['#0047ab', 'lightcoral']
plt.pie(sizes, colors=colors)
#determine score colour as scolour
if x < 5:
scolour = "#ff6961" #red
elif 5 <= x < 5.5:
scolour = "#ffb347" #orange
elif 5.5 <= x < 6.5:
scolour = "#77dd77" #light green
elif 6.5 <= x:
scolour = "#03c03c" # dark green
#draw a circle at the center of pie to make it look like a donut
centre_circle = plt.Circle((0,0),0.75, fc=scolour,linewidth=1.25)
fig = plt.gcf()
fig.gca().add_artist(centre_circle)
# Set aspect ratio to be equal so that pie is drawn as a circle.
plt.axis('equal')
plt.show()
This is in one script. In my GUI script, I have imported these:
from PyQt5 import QtCore, QtGui, QtWidgets
import sqlite3
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as
FigureCanvas
from graphSetup import *
And at the start of my window class, before the setup function, I have this function:
def minionGraphSetup(self, recentMinionRatioAvg):
minionRatioGraph(recentMinionRatioAvg)
Instead of calling plt.show you need to place the figure that is produced by your imported script into the FigureCanvas of your PyQt GUI.
So in your plotting script do
def minionRatioGraph(recentMinionRatioAvg):
...
fig = plt.gcf()
fig.gca().add_artist(centre_circle)
plt.axis('equal')
#plt.show() <- don't show window!
return fig
In your GUI script use the figure obtained to place it into the canvas.
def minionGraphSetup(self, recentMinionRatioAvg):
fig = minionRatioGraph(recentMinionRatioAvg)
...
self.canvas = FigureCanvas(fig, ...)
If you want to return an image, you can save it to a Byte buffer,
import io
def minionRatioGraph(recentMinionRatioAvg):
...
fig = plt.gcf()
fig.gca().add_artist(centre_circle)
plt.axis('equal')
buff = io.BytesIO()
plt.savefig(buff, format="png")
return buff
and then show it as image in the PyQt GUI. (I haven't tested the below, so it may work a bit differently.)
def minionGraphSetup(self, recentMinionRatioAvg):
image = minionRatioGraph(recentMinionRatioAvg)
label = QLabel()
pixmap = QPixmap(image)
label.setPixmap(pixmap)

My PyQt plot's Y axes are upside down (even the text)?

Today is the first day I have tried using PyQtGraph. I really like it so far except I can't seem to fully comprehend how things work..
I am trying to place two FFT plot widgets into the same window. After much trial and error I found what I thought was the proper way to do it. However now I have two plots which show the correct information but everything on the Y axis is inverted.
Also it seems zooming and panning are not correct either (the whole plot moves, not just the data within it).
This image shows the two real-time audio fft plots both within a single GraphicsWindow. On the left I use addPlot with addItem and on the right I use addViewBox with addItem.
To be thorough I have tried using item.invertY(True) and item.scale(1,-1).
In both cases it will invert the Y axis data but not the text or axes, nor does it address the panning/zooming issues..
This Python script is everything I was able to write.
It was based off of this file: pyqtgraph live running spectrogram from microphone
import numpy as np
import pyqtgraph as pg
import pyaudio
from PyQt4 import QtCore, QtGui
FS = 44100 #Hz
CHUNKSZ = 1024 #samples
class MicrophoneRecorder():
def __init__(self, signal):
self.signal = signal
self.p = pyaudio.PyAudio()
self.stream = self.p.open(format=pyaudio.paInt16,
channels=1,
rate=FS,
input=True,
frames_per_buffer=CHUNKSZ)
def read(self):
data = self.stream.read(CHUNKSZ)
y = np.fromstring(data, 'int16')
self.signal.emit(y)
def close(self):
self.stream.stop_stream()
self.stream.close()
self.p.terminate()
class SpectrogramWidget2(pg.PlotWidget):
read_collected = QtCore.pyqtSignal(np.ndarray)
def __init__(self):
super(SpectrogramWidget2, self).__init__()
self.img = pg.ImageItem()
self.addItem(self.img)
self.img_array = np.zeros((1000, CHUNKSZ/2+1))
# bipolar colormap
pos = np.array([0., 0.5, 1.])
color = np.array([[0,0,0,255], [0,255,0,255], [255,0,0,255]], dtype=np.ubyte)
cmap = pg.ColorMap(pos, color)
pg.colormap
lut = cmap.getLookupTable(0.0, 1.0, 256)
# set colormap
self.img.setLookupTable(lut)
self.img.setLevels([0,100])
# setup the correct scaling for y-axis
freq = np.arange((CHUNKSZ/2)+1)/(float(CHUNKSZ)/FS)
yscale = 1.0/(self.img_array.shape[1]/freq[-1])
self.img.scale((1./FS)*CHUNKSZ, yscale)
self.setLabel('left', 'Frequency', units='Hz')
# prepare window for later use
self.win = np.hanning(CHUNKSZ)
#self.show()
def update(self, chunk):
# normalized, windowed frequencies in data chunk
spec = np.fft.rfft(chunk*self.win) / CHUNKSZ
# get magnitude
psd = abs(spec)
# convert to dB scaleaxis
psd = 20 * np.log10(psd)
# roll down one and replace leading edge with new data
self.img_array = np.roll(self.img_array, -1, 0)
self.img_array[-1:] = psd
self.img.setImage(self.img_array, autoLevels=False)
class SpectrogramWidget(pg.PlotWidget):
read_collected = QtCore.pyqtSignal(np.ndarray)
def __init__(self):
super(SpectrogramWidget, self).__init__()
self.img = pg.ImageItem()
self.addItem(self.img)
self.img_array = np.zeros((1000, CHUNKSZ/2+1))
# bipolar colormap
pos = np.array([0., 0.5, 1.])
color = np.array([[0,0,0,255], [0,255,0,255], [255,0,0,255]], dtype=np.ubyte)
cmap = pg.ColorMap(pos, color)
pg.colormap
lut = cmap.getLookupTable(0.0, 1.0, 256)
# set colormap
self.img.setLookupTable(lut)
self.img.setLevels([0,100])
# setup the correct scaling for y-axis
freq = np.arange((CHUNKSZ/2)+1)/(float(CHUNKSZ)/FS)
yscale = 1.0/(self.img_array.shape[1]/freq[-1])
self.img.scale((1./FS)*CHUNKSZ, yscale)
self.setLabel('left', 'Frequency', units='Hz')
# prepare window for later use
self.win = np.hanning(CHUNKSZ)
#self.show()
def update(self, chunk):
# normalized, windowed frequencies in data chunk
spec = np.fft.rfft(chunk*self.win) / CHUNKSZ
# get magnitude
psd = abs(spec)
# convert to dB scaleaxis
psd = 20 * np.log10(psd)
# roll down one and replace leading edge with new data
self.img_array = np.roll(self.img_array, -1, 0)
self.img_array[-1:] = psd
self.img.setImage(self.img_array, autoLevels=False)
if __name__ == '__main__':
app = QtGui.QApplication([])
win = pg.GraphicsWindow(title="Basic plotting examples")
#win.resize(1000,600)
w = SpectrogramWidget()
w.read_collected.connect(w.update)
spectrum1 = win.addPlot(title="Spectrum 1")#win.addViewBox()
item = w.getPlotItem()
spectrum1.addItem(item)
w2 = SpectrogramWidget2()
w2.read_collected.connect(w2.update)
spectrum2 = win.addViewBox()
spectrum2.addItem(w2.getPlotItem())
mic = MicrophoneRecorder(w.read_collected)
mic2 = MicrophoneRecorder(w2.read_collected)
# time (seconds) between reads
interval = FS/CHUNKSZ
t = QtCore.QTimer()
t.timeout.connect(mic.read)
t.start((1000/interval) ) #QTimer takes ms
t2 = QtCore.QTimer()
t2.timeout.connect(mic2.read)
t2.start((1000/interval) ) #QTimer takes ms
app.exec_()
mic.close()
Thank you for any help!
I have no idea why doing this causes things to be mirrored, but the issue is related to using the plotItem from a plot in another plot (I think that's what you're doing?)
Anyway, PlotWidgets shouldn't be used like that. They are just normal Qt Widgets, so add them to a Qt layout like you would with any other Qt Widget.
if __name__ == '__main__':
app = QtGui.QApplication([])
win = QtGui.QMainWindow()
widget = QtGui.QWidget()
win.setCentralWidget(widget)
layout = QtGui.QHBoxLayout(widget)
win.show()
w = SpectrogramWidget()
w.read_collected.connect(w.update)
layout.addWidget(w)
w2 = SpectrogramWidget2()
w2.read_collected.connect(w2.update)
layout.addWidget(w2)
# .... etc
P.S. Is there a reason you have two identical classes with a different name? You could just instantiate multiple copies of the same class. E.g.
w = SpectrogramWidget()
w2 = SpectrogramWidget()

Categories