Absolute positions in mpld3 graphs - python

I'm trying to define a custom plugin for mpld3, and I'm struggling with positions. More particularly, by default, SVG generated by mpld3 come with Move and Zoom buttons, which change the view of the graph. When I was trying to display things (like text), I stumbled upon a strange behavior where positions are set relatively to the current view. It means that if I display a text next to a point A(10,10) and then move the view, the point will still be at (10,10) but the text will move because its position is relative to the current view.
So I wanted to think again and start with a minimal example which should work : this example from the mpld3 documentation. In the demo, you can see the behavior I want : positions are absolute (when you move the view, cursor position is set according to the grid)..
But if I copy-paste the exact same code in Pycharm and execute it, positions are relative to the view which is even more strange :
My guess is it's a version problem. I don't know which versions are used in the hosted demo example. I use mpld3==0.5.1, matplotlib==3.2.1 and numpy==1.19.2.
Do you have an idea so I can manage to deal with absolute positions like in the example ?

I've managed to make it work. It turns out that downgrading mpld3 to version 0.3 does what I want. I'm sure there is a better solution (because I can't take advantage of the latest version) but it gets the job done.

Related

Using PyMuPDF "draw_rect" function is working inconsistent

I'm blacking out some information from several PDF's but there are some of these that the rectangles made by "draw_rect" functions are't being drawn correctly. I have checked the rectangles and they look right, that and I'm also usind the "add_redact_annot" with the exact same rectangle and works good.
def hide_text_rects(page, rects):
for rect in rects:
page.add_redact_annot(rect)
page.draw_rect(rect, color=(0,0,0), fill=(0,0,0))
The rectangles seem to be mirrored and zoomed (scaled). I really don't know what to do because I don't find any info related in the docs.
Edit: I found that the PDF's with version 1.7 are the ones working correctly. And the other ones are version 1.5.
The probable reason for this behaviour is a sloppy specification of the page's coordinate system.
For example the standard point (0,0) = bottom-left in PDF may have been redefined to be top-left.
If this type of coordinate change is not wrapped within PDF stacking operators q / Q (as it should), then any insertions (of text, drawing etc.) appended to the page /Contents act under wrong assumptions, and appear dislocated.
Heal this by executing page.clean_contents() before do any insertion.
You can also check if this is required at all by page.is_wrapped. Please also consult the documentation - there is an own section dealing with this.

Use python to identify elements at screen

I want to start a python project thats identify some elements in screen, like a square's size or all elements in screen with a certain color, and i don't even know from where should i start.
For example, all the top points above certain value at the graph.
Anyone could please give me a north?
If you're looking to interact with elements inside a browser, look at Selenium. If you want to control the desktop itself, look into Sikuli.
In either case, you can use OpenCV to identify elements and do template matching.
Edit: more comments after more details from OP
If you're just looking to identify the peaks in the graph, you can take a screenshot of the display at regular intervals using Sikuli or PyScreenshot and then use template matching in OpenCV (either directly or using Sikuli) to get the coordinates of the peaks in the screenshot. The horizontal line across the graph might throw off some of the template matching, but you can play around with the various parameters to get the results you want.
Check out this tutorial for template matching.

In Brainrender is there a way to at labels and stop the brain from load upside down?

I am using brainrender to show where I've placed some electrodes. However, whenever I load brainrender my brain always loads upside down:
brainrender
Is there a way to fix this? Or do I just have to manually flip it every time?
I was also wondering if any one knew of a way to display/add text labels to the 3D image the brainrender makes? I did look at these functions, but as far as I can tell neither one actually places a label on a brain area.
labels:
Below I've copied the important parts of how to resolve the issues/questions posted here. All credit for an answer to this question goes to the developer of BrainRender(FedeClaudi on GitHub). I am copying the key information he provide my team in case the following link to the developers answer stops working: https://github.com/BrancoLab/BrainRender/issues/70
The developer of the BrainRender tool updated this tool to version 0.4.0.0 which allows users to permanently add labels to brain areas:
pip install -U brainrender
You should also update using:
https://github.com/BrancoLab/BrainRender.git
Some key things to know about how the labels work:
Labels appear on the right hemisphere by default
added xoffset, yoffset and zoffset to manually alter the location of the label
added a colored sphere to the point on the mesh that corresponds to the label location [optional]
This is how we resolved our brain loading upside down, it turns out this is just an issue that occurs with Jupyter notebook:
from vtkplotter import embedWindow
from brainrender.scene import Scene
# Change 1 Disable scene embedding with k3d in vtkplotter
embedWindow(False)
# Change 2 set jupyter to False when creating the scene
scene = Scene(jupyter=False)
# Populate and render
scene.add_brain_regions(['MOp', 'MOs', 'VISl', 'VISp', 'RSPagl'], add_labels=True)
scene.add_text('title')
scene.render(camera='top')
The developer also warned "to avoid import vtkplotter as vtk because there's another package called vtk".

Color area under graph in manim

I am following this tutorial for manim: https://talkingphysics.wordpress.com/2019/01/08/getting-started-animating-with-manim-and-python-3-7/. Under heading 7.0: Graphing functions, the example shows code for plotting sine and cosine functions.
I was wondering if I could also fill the area covered between, let's say, sine function and the x-axis from x_min to x_max. I realized that the used PlotFunctions class has following hierarchy: PlotFunctions -> GraphScene -> Scene -> Container -> object (where -> denotes child of). But in this entire chain of hierarchy, I do not see a config option such as fill_color that is present in VMobject.
I'm also not readily able to locate any code that helps in doing so, although I'm sure that some really easy 1 line code must exist since this is used in so many 3blue1brown videos. I would really appreciate some help with this!
You can check this github issue, it might be something your'e looking for.
It hasn't been merged onto the main branch so this could be a work-around for now.
After looking more into the code, I still don't see a 1-liner code per se but did find that to color the area under the graph, a set of riemann rectangles of very small width (~0.01) are used. This makes the graph look colored.
I used get_point_from_function() option to get the points and passed them to create a Polygon filled with colour.
you can check it here , look at def get_region()

Bokeh shows plot blurred on windows

I am using bokeh for plotting. With my current settings, bokeh shows some text and lines kind of blurred (anti-aliased?). This is not really noticeable on my monitor, but on some projectors, especially when doing screenshots and inserting them into presentations, it looks weird.
As requested, a minimum working example:
from bokeh.plotting import figure, show
p = figure(plot_width=1000, plot_height=600,
title="TestTitle", x_axis_label = "Length [cm]", y_axis_label="Height [m]")
p.xaxis.axis_label_text_font_style = "normal"
p.xaxis.axis_label_text_font_size = "12pt"
show(p)
I am using Windows 7 and tried this in the current versions of Chrome and IE. Python 3.6, bokeh version 0.13.0. The first image is taken from the example, the second is taken from my real code:
I tried different figure sizes, but the problem persists - maybe I am trying the wrong ones? Anything I can do about this, except simply trying different sizes until something "works"? Setting px instead of pts seems to make it worse...
Regarding the text, it is rendered on to a raster HTML canvas, and the details of how this is done are entirely dependent on the browser canvas implementation. (FWIW things look better on any browser on OSX than the above image.) There's not anything we can to change how a specific browser renders text, and not much I can suggest except to make much bigger canvas sizes if you need to show something extremely magnified.
Regarding the aliasing of the axes and tick marks, I can't reproduce anything like that on any OSX browser (Safari, FF, or Chrome). It definitely appears to be a Windows-specific issue. It's possible there are issues that could be addressed, e.g a different HiDPI setting, or half-pixel offsets, but it would take investigation to try and determine what can be done. A Github issue with details would be appropriate, but I can't speculate when it might be addressed (we are under-resourced and no core contributors are regular windows users).

Categories

Resources