-
Notifications
You must be signed in to change notification settings - Fork 616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New Visual: Bar #2394
base: main
Are you sure you want to change the base?
New Visual: Bar #2394
Changes from 2 commits
324d4ba
847963e
59a2791
e4a4aab
62b32aa
8c5de6d
7a21064
f6c3fa6
a4ca071
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import numpy as np | ||
from vispy import scene, app | ||
|
||
if __name__ == "__main__": | ||
|
||
canvas = scene.SceneCanvas(keys='interactive', vsync=False) | ||
canvas.size = 800, 600 | ||
canvas.show() | ||
|
||
grid = canvas.central_widget.add_grid() | ||
grid.padding = 10 | ||
|
||
vb1 = grid.add_view(row=0, col=1, camera='panzoom') | ||
|
||
x_axis1 = scene.AxisWidget(orientation='bottom') | ||
x_axis1.stretch = (1, 0.1) | ||
grid.add_widget(x_axis1, row=1, col=1) | ||
x_axis1.link_view(vb1) | ||
y_axis1 = scene.AxisWidget(orientation='left') | ||
y_axis1.stretch = (0.1, 1) | ||
grid.add_widget(y_axis1, row=0, col=0) | ||
y_axis1.link_view(vb1) | ||
|
||
grid_lines1 = scene.visuals.GridLines(parent=vb1.scene) | ||
|
||
bar1 = scene.Bar(height=np.arange(0, 250, 0.5), bottom=np.arange(0, 125, 0.25), width=0.8, shift=0.5, | ||
color=(0.25, 0.8, 0.), parent=vb1.scene) | ||
|
||
vb1.camera.set_range() | ||
|
||
canvas.measure_fps() | ||
app.run() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# -*- coding: utf-8 -*- | ||
# ----------------------------------------------------------------------------- | ||
# Copyright (c) Vispy Development Team. All Rights Reserved. | ||
# Distributed under the (new) BSD License. See LICENSE.txt for more info. | ||
# ----------------------------------------------------------------------------- | ||
|
||
import numpy as np | ||
from .mesh import MeshVisual | ||
|
||
|
||
class BarVisual(MeshVisual): | ||
"""Visual that calculates and displays a bar graph | ||
|
||
Parameters | ||
---------- | ||
data : array-like | ||
1st column is the height, optional 2nd column is bottom | ||
height : array-like | ||
Height of the bars | ||
bottom : array-like | ||
Bottom of the bars | ||
width : int | float | ||
Width of all bars | ||
shift : int | float | ||
Shift of all bars along the x-axis | ||
color : instance of Color | ||
Color of the bar. | ||
""" | ||
|
||
def __init__(self, height, bottom=None, width=0.8, shift=0.0, color='w'): | ||
if bottom is None: | ||
bottom = np.zeros(height.shape[0]) | ||
rr, tris = calc_vertices(height, bottom, width, shift) | ||
|
||
MeshVisual.__init__(self, rr, tris, color=color) | ||
|
||
def update_data(self, height, bottom=None, width=0.8, shift=0.0, color='w'): | ||
if bottom is None: | ||
bottom = np.zeros(height.shape[0]) | ||
rr, tris = calc_vertices(height, bottom, width, shift=shift) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing orientation here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Obviously yes, it was late yesterday |
||
|
||
MeshVisual.set_data(self, rr, tris, color=color) | ||
|
||
|
||
def calc_vertices(height, bottom, width, shift): | ||
|
||
y_position = np.zeros((height.shape[0], 2)) | ||
y_position[:, 0] = height | ||
y_position[:, 1] = bottom | ||
|
||
y_position = y_position.flatten().repeat(2) | ||
|
||
stack_n_x2 = np.arange(0, height.shape[0], dtype=int) | ||
stack_n_x2 = np.expand_dims(stack_n_x2, axis=1) | ||
stack_n_x2 = np.repeat(stack_n_x2, 1, axis=1) | ||
stack_n_x2 = np.repeat(stack_n_x2, 2, axis=0) | ||
brisvag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
vertices = np.array([ | ||
[-width/2 + shift, 1, 0], | ||
[width/2 + shift, 1, 0], | ||
[width/2 + shift, 0, 0], | ||
[-width/2 + shift, 0, 0]]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the point of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well I thought that vertices would have to be x,y,z and the resulting vertices for the mesh should be in the shape (n,3), since its a 2d plot the z axis is zero. I'm not married to it if you want to change it it would be fine with me There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I see why I was confused: this resulted in a deprecation warning from numpy due to a staggered sequence in my testing, because I was passing an array to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But yeah, I think you could remove the zero there and it would be fine, meshes work well with 2D data and we do it in several other places as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only argument for not removing the zero would be if it avoids an unnecessary copy. Another thing to consider performance-wise would be to create one giant array at the beginning and fill it in later. This is typically faster since we don't have to keep allocating new numpy arrays, but may only be true for really large arrays. |
||
|
||
vertices = np.tile(vertices, (height.shape[0], 1)) | ||
vertices[:, 0] = vertices[:, 0] + stack_n_x2[:, 0].repeat(2) | ||
vertices[:, 1] = y_position | ||
|
||
base_faces = np.array([[0, 1, 2], [0, 2, 3]]) | ||
|
||
base_faces = np.tile(base_faces, (height.shape[0], 1)) | ||
|
||
faces = stack_n_x2*4 + base_faces | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, maybe this use of |
||
|
||
return vertices, faces |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This example will need a docstring like the other examples in this gallery directory. Formatting and content is important since it appears in the final website.