I am trying to record sound from mic using pyaudio and plotting it at the same time (in parallel with recording).
I have created a horizontal plot window, who's size is bigger than the main application window.
Code section:
self.fig, self.ax1 = plt.subplots()
rect = self.fig.patch
rect.set_facecolor('white')
#self.ax1.autoscale_view(True,True,True)
self.ax1.set_autoscale_on(True)
plt.subplots_adjust(left=0.0, bottom=0.0, top=0.95, right=10.0)
I am not able to move plot area to the current location of plotting or may be at the end of horizontal plot window. I also tried to use scrolled window. That also didn't work.
any idea?
Related
I am using matplotlib.h to draw graphs with C++,
vector<double> Cx;
vector<double> Cy;
plt::plot(Cx,Cy,"*");
plt::show();
The problem is that I want to display the evolution of the two vectors with an animation like, so the function is here:
plt::show();
Is blocking the main thread and until the window is closed everything is blocked.
So is there anyway to change the plotted data without closing the Window and display to the user an animation
I have only used matplotlib in python but from what you have there it seems similar.
When to use cla(), clf() or close() for clearing a plot in matplotlib? <- using plt::clf() from here may be a good start. Then once the figure is cleared maybe have a time delay and replot.
plt.ion() # needed to say you want to reuse the same window
def display(some_list):
plt.clf()
plt.scatter(range(len(some_list)),some_list)
plt.draw()
Somthing like this is used in python as an example note plt.draw() is being used to redraw on the same window rather than show().
I would like to create a matplotlib widget that changes the data and axes. It would be great to go even farther and change out a set of subplots.
I've tried modifying:
matplotlib display only one graph of a set of 10 like a slideshow
Python Matplotlib: change the displayed figure interactively using a widget
But both of these answers change the data not the axes or subplots.
I want to pre generate many plots with all sorts of things like pcolormesh, polygons, colorbars, scatter plots, legends, text and annotations. And swap them out on the push of a button or slide bar.
I have to segment a few things in a dataset I have (.nrrd-file) by drawing a rectangle around the area of interest and saving the segments (also as .nrrd-files).
I tried everything in the Segmentation-Tool that comes with MITK but I cannot seem to find a way to draw rectangles. I also tried to do some key combos (like holding shift, ctrl or alt) while drawing but in vain.
I know I can use the Measurement-Tool to select rectangles and save them (as .pf-files), but using that I'd have to write a some code to convert those selected rectangles into rectangle segmentations later on.
Does anyone know whether there's a possibility (that I didnt find yet) to draw rectangles in the Segmentation tool, or some other way so there's no need to write a workaround?
You can use the Image Cropper plugin in MITK 2016.11 for rectangular image masking and cropping (scissors icon).
Open the plugin, select your image in the Data Manager and click on the New button in the plugin to create a bounding object. You can modify the rectangular bounding shape in the render windows by dragging its red handles. You can move the whole shape by hovering over the bounding shape (it will turn green), click, and than drag.
Click on the Mask button if you want to get an image with the same dimensions in which all pixels outside the bounding shape are set to a user defined value (see the Advanced settings in the plugin). Click on the Crop button otherwise.
Note that you can always press F1 in any active MITK plugin to open a help page with detailed instructions.
I create a screen in pygame using:
screen = pygame.display.set_mode((width,height))
Then I draw 200 random gray scale circles on it and save the screen as an image. (I need to save this screen as an image so I can compare it to a target image later on):
pygame.image.save(screen, "Circles.png")
Now, I need to randomly change different parameters of different circles by putting the numpy array that contains the circle parameters for all the circles in a loop with a big number as its iteration value.
And I need to redraw the mutated circles on a screen (at the end of each iteration) and save that screen as a png image again.
Now the problem is, every time I use pygame.display.set_mode((width,height)) it opens up the display window and it significantly slows down my program.
I would like to create a screen and save it in a variable but don't need to display that screen at each iteration. I haven't been able to figure out what command to use to avoid displaying the screen.
I appreciate your help.
If you don't want a window to appear, try creating the surface like this:
screen = pygame.Surface((width,height))
Then you can draw your circles on this surface and save it the same way as you did before. As others have said, clear the surface with the fill() method.
I would probably end up using something like this. This will still popup a screen but it will be tiny and never updated.
import pygame
from pygame.locals import *
pygame.init()
def circle_drawing(surf):
pass
#your circle drawing function using passed in surface
DISPLAY = (1, 1)
DEPTH = 32
FLAGS = 0
screen = pygame.display.set_mode(DISPLAY, FLAGS, DEPTH)
work_surface = pygame.Surface((500,500))
count = 0
while True:
ws = circle_drawing(work_surface.copy())
pygame.image.save(ws, "circles_" + str(count)+".png")
count +=1
I'm new to Qt development so I've being trying to research a solution to a user interface I need to design. My project is to simulate players in an online game moving around a global map. To represent the map I need to display a 2D grid, with each space in the grid representing a region of a map. I then need to display the location of each player in the game. The back-end is all fully working, with the map implemented as a 2D array. I'm just stuck on how to display the grid.
The research I have done has led me to believe a QGraphicsView is the best way to do this, but I can't seem to find a tutorial relevant to what I need. If anyone has any tips on how to implement this it would be much appreciated.
Thanks, Dan
A 2D Grid is nothing more than a set of horizontal and vertical lines. Suppose you have a 500x500 map and you want to draw a grid where the distance between the lines in both directions is 50. The sample code that follows shows you how you can achieve it.
// create a scene and add it your view
QGraphicsScene* scene = new QGraphicsScene;
ui->view->setScene(scene);
// Add the vertical lines first, paint them red
for (int x=0; x<=500; x+=50)
scene->addLine(x,0,x,500, QPen(Qt::red));
// Now add the horizontal lines, paint them green
for (int y=0; y<=500; y+=50)
scene->addLine(0,y,500,y, QPen(Qt::green));
// Fit the view in the scene's bounding rect
ui->view->fitInView(scene->itemsVBoundingRect());
You should check the QGraphicsView and the QGraphicsScene documentation as well as the corresponding examples. Also you can watch the graphics view training videos or some graphics view related videos from the Qt developer days.
Well if you have a constant grid size or even a limited number of grid sizes what i like to do is to draw a grid block in gimp or any other program and then set that as the background brush (draw only bottom and right side of the block) qt will repeat the image and will give you a full grid. I think this is good for performance too.
This is the grid image i used in one of my programs it's 10x10 pixels.
Then call QGraphicsScene setBackgroundBrush as the follwing:
scene->setBackgroundBrush(QBrush(QPixmap(":/grid/grid10.png")));
The more native way is this:
scene = self.getScene() # Your scene.
brush = QBrush()
brush.setColor(QColor('#999'))
brush.setStyle(Qt.CrossPattern) # Grid pattern.
scene.setBackgroundBrush(brush)
borderColor = Qt.black
fillColor = QColor('#DDD')
rect = QRectF(0.0, 0.0, 1280, 720) # Screen res or whatever.
scene.addRect(rect,borderColor,fillColor) # Rectangle for color.
scene.addRect(rect,borderColor,brush) # Rectangle for grid.
Sorry by PyQt...
Suppose a scene is set to the graphicsview then simply below one line will show the grid.
ui->graphicsView->scene()->setBackgroundBrush(Qt::CrossPattern);
There several other values can be passed for ex: Qt::Dense7Pattern
These are members of enum BrushStyle, just click on any used value in Qt creator and it will take you to the enum declaration where you can see all other possible values.
PS:
A scene can be set like this:
ui->graphicsView->setScene(new QGraphicsScene());