Automatically fit Bokeh plot to screen? - python-3.x

Is there a simple way to automatically fit a Bokeh plot to the screen used to display your browser? Right now I'm setting the plot width and height manually.
Note: when I say fit I mean that I want the plot to fill 80% of the visible screen.

In more recent bokeh versions, yes you can do this (easily).
plots and layouts now have a sizing_mode property which by default is set to fixed. The other values include scale_width, scale_height, and scale_both.
import bokeh.plotting
import bokeh.layouts
fig1 = bokeh.plotting.figure()
fig1.sizing_mode = 'scale_width'
fig2 = bokeh.plotting.figure()
fig2.sizing_mode = 'scale_width'
column = bokeh.layouts.column([fig1, fig2])
column.sizing_mode = 'scale_width'
As in the example above, your layout will need to have its sizing_mode attribute set appropriately to let its children plots expand.
Using the above example your plot will expand to the size of its container. It's up to you to appropriately size the container (using CSS) to suit your needs.
Note that the width/height property of your figures/plots still matter: they determine the ratio at which the bokeh layout scales.

Related

glyphs appearing outside of plotting region with SVG output backend in bokeh

I recently upgraded bokeh from 0.12.4 to 0.12.10, primarily to enable Plot.output.backend = "svg" for the SaveTool. This feature works pretty well, but I've been experiencing other plotting issues when it is enabled.
I have a layout based plot composed of 2 subplots and a slider panel, but the issue seems to be plot- and glyph-independent. For example, the first plot in the layout is based on:
p1=figure(width=1500, x_range=get_xrange(rawdat), tools="box_zoom,tap,xwheel_zoom,reset,save", y_range=get_yrange(rawdat))
source = ColumnDataSource(data=dict(ps=rawdat.ps, logsp=-log10(rawdat.p_score), radii=rawdat.radii, alpha=rawdat.alpha, color=rawdat.color, mafcolor=rawdat.mafcolor, weightcolor=rawdat.weightcolor, outcol=rawdat.outcolor, outalpha=rawdat.outalpha, alpha_prevsig=rawdat.alpha_prevsig, snpid=rawdat.rs, rs=rawdat.ensembl_rs, maf=rawdat.maf, csq=rawdat.ensembl_consequence))
p1.circle(x='ps', y='logsp', radius='radii', fill_alpha='alpha', fill_color='color', line_color='outcol', line_alpha='outalpha', line_width=6, radius_units='screen', source=source)
When Plot.output.backend = "svg" is enabled, glyphs overflow outside of the plotting region (especially when zooming), so much so that my circles collide with the bokeh toolbar, as below:
This behaviour happens no matter what the glyphs are (on plot 2, segments and rectangles do the same). When the backend is set to the default, the plot does not overflow. This happens on Opera, Vivaldi, Safari and Firefox.
I'd like to see if other people have experienced and/or solved this before I raise an issue on their github page.

python pygame what does font mean?

I am now studying an online pygame tutorial. However, I am not sure how it works when trying to place text on the screen. According to the official docs for pygame.font.Sysfont():
Return a new Font object that is loaded from the system fonts. The font will match the requested bold and italic flags. If a suitable system font is not found this will fall back on loading the default pygame font. The font name can be a comma separated list of font names to look for.
What is a font?
font = pygame.font.SysFont(None, 25)
# message to the user
def message_to_screen(msg,color):
screen_text = font.render(msg, True, color)
screen.blit(screen_text, [screen_width/2,screen_height/2])
Ok, here is the simplest explanation i can give you:
Modules such as Pygame are simple (or sometimes not that simple...) codes that add new features and functions to your normal built in python functions. This means that when you import a module you also inherent from that module all of its functions and classes. So for example, the normal python does not contain the function "draw"
pygame.draw.rect(arguments)
however when you import pygame, you inherent that function from the pygame code. allowing you to draw and develop a GUI for better programs.
Same is with objects. Python is an 'object orientated programming language". Objects are a type of data store that defines and structures your code. So for example, sprites in Pygame can be anything you want. Your sprite can be anything you want from a monkey, or a freaking mummy eating zombie, to a simple rectangle. To create the exact sprite you want with the right shape, color, rect, and image, you need to structure it with a class. A class is what will create the object for your sprite. Look at this here:
#Here is the class named 'Button' of type ' pygame.sprite.Sprite'
class Button(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
#Here we define the shape of the sprite. In the case it is a simple
#150 by 75 rectangle surface. The shape can also be an image or any
#or any geometric shape you want
self.image = pygame.Surface((150, 75))
#here we define the color of the sprite
self.image.fill(green)
#and here we make sure the sprite has a rect
self.rect = self.image.get_rect()
so as you see this class defines all what we need to create this simple sprite. Of course it can have many more variables to it depending on what the sprite is, but lets stick with something simple for now
Now the class stores this information in an object, to be used later. Like this:
MySpriteObject = Button()
simple enough i would say. so now you have a sprite object and can use pygames' many function to draw it on the screen, add interaction to it, group it, and a lot of other things.
so Finally you understand the idea of an object in python. Now to you're actual question.
What is a font?
Well a font is an object that you get when you import pygame. You don't have to do the class stuff at the top as the pygame module does that for you. Just create the object and use the function 'render'. So essentially it is an object that you can change two things in as you like. the font, and the size
MyFontObject = pygame.font.Font(#Font here, #size here)
If you make the Font argument None, then it will give you the default pygame font. Thats what I usually do. However, if you want to change the font, you can either download a font (usually a .ttf) and then type in its folder path in the Font argument, or you can use a font that you have on your computer. To do that instead of
MyFontObject = pygame.font.Font(#Font here,#size here)
you use
MyFontObject = pygame.font.SysFont(#Name of font here, #size here)
Where #Name of font here is, you can replace it by any font installed on your computer. To get a list of the names of fonts on your computer that pygame can identify:
pygame.font.get_fonts()
Ok, so that is how you create the font object. Now to rendering it.
Rendering the font uses the font object to change the shape and color of the text you want to display. Here is how its done
text = MyFontObject.render(#Your Text Here,#true or false,#color)
screen.blit(tex,t(#X axis,#Y axis))
Pretty self explanatory. Except for the #true or false i guess. That pretty much asks you if you want to use a technique that helps the text look less pixeled and square-like. If you provide true it will. If you dont the text will look awefull, so always keep it true.
So that's pretty much what i have to say. Here is a short summary:
1.) An object is a type of data store which stores different variables to structure and define your code. So therefore a font object is an object that defines the different things for a font, such as size and font type
2.) to create an object we use a class as shown above
3.) A font class is already there with the pygame module so you just have to call the font object straight away:
MyFontObject = pygame.font.Font(#filepath or None for the default pygame font,#size here)
or for a font that is installed on your system such as ariel (which can all be viewed with pygame.font.get_font())
MyFontObject = pygame.font.SysFont(#Name of system font here,#size here)
4.) To put this font object to use you render it:
text = MyFontObject.render(#Text here,#True or false,#color of text)
then normally blit it on the screen and call pygame.display.update
screen.blit(text,(#X axis,#Y axis)
pygame.display.update()
I hope this helps. I know I'm not the best explainer and I write too much, but you should read the summary at least.
P.S: Sorry for using sprites to explain classes and objects. I know I went of topic but it was just an example.

PyQt Keeping QLabel Size Aspect Ratio

I have a QLabel which displays an image. Currently, I have the image set to keep it's aspect ratio, and grow as big as it can within the QLabel.
Is there any way I can also set the QLabel to maintain the image's aspect ratio? I do not want to have "blank" QLabel space either side of the image when the label is wider than the image.
I have been looking for any sort of QLabel property that would allow me to set the aspect ratio of the label, but have not managed to get anything to do what I wanted to do.
All the answers I have seen relate to keeping the aspect ratio of a resized QPixmap image, but not of the QLabel containing it.
Any help would be great!
Cheers
FP
I seem to have cracked it, so incase anyone else was wondering how to do this:
I took tmoreau's solution and modified it slightly. For it to work, you need to set the QLabel's maximum size to the image's new size prior to the paint event. Immediately afterwards, you need to set the maximum size for the QLabel to something very large, otherwise, you will not be able to enlarge the image at all as you will have specified the maximum size to be that of the current image.
def paintEvent(self, event):
size = self.size()
painter = QtGui.QPainter(self)
point = QtCore.QPoint(0,0)
scaledPix = self.pixmap.scaled(size, Qt.KeepAspectRatio, transformMode = Qt.SmoothTransformation)
self.setMaximumSize(scaledPix.size())
point.setX(0)
point.setY(0)
#print (point.x(), ' ', point.y())
painter.drawPixmap(point, scaledPix)
self.setMaximumSize(QtCore.QSize(4000,5000))
If anyone has a better solution, by all means please let me know!

Getting coordinate / shape data from pydot for layout in PyQt4

I would like to use the QGraphicsView control and QGraphicsScene to layout GraphViz generated graphs using pydot. Is there a way to generate the graph in pydot, have GraphViz do the layout, and then extract the layout information (such as is included in the various output formats generated by pydot.write_xyz)? So far in my testing the get_pos() functions for Nodes, etc. return None.
As you already said, you have to first output the graph with create_dot to a string, then generate the graph layout by passing that string to graph_from_dot_data:
graphWithPositions = pydot.graph_from_dot_data(graph.create_dot())

Display HOG feature inside Label using PYQT5 python

I am trying to display extracted features from an Image using Histogram of Oriented Gradients (HOG) on interface.
I need guidance how to display it; as for histogram we need to use (QWidget) and promote it to (Histogram class ) so how could I display histogram of oriented gradient to display extracted features
Any help in this case would be grateful
hog = cv2.HOGDescriptor(qImg, (16,16), (8,8), (8,8), 9)
hist = hog.compute(image)
sample.append(hist)
plt.plot(hist)
plt.show()
I want to display HOG at interface ..for this I need guidance how to do that

Resources