-
Notifications
You must be signed in to change notification settings - Fork 618
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 5 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,43 @@ | ||
# -*- coding: utf-8 -*- | ||
# vispy: gallery 2 | ||
# ----------------------------------------------------------------------------- | ||
# Copyright (c) Vispy Development Team. All Rights Reserved. | ||
# Distributed under the (new) BSD License. See LICENSE.txt for more info. | ||
# ----------------------------------------------------------------------------- | ||
""" | ||
Vertical bar plot with Axis | ||
=========================== | ||
""" | ||
|
||
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), # for a more traditional horizontal plot, either remove the | ||
bottom=np.arange(0, 125, 0.25), width=0.8, shift=0.5, # orientation paramenter outright | ||
color=(0.25, 0.8, 0.), orientation='v', parent=vb1.scene) # or set it to 'h' | ||
|
||
vb1.camera.set_range() | ||
|
||
app.run() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# -*- 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. | ||
orientation : {'h', 'v'} | ||
Orientation of the histogram - 'h' is default | ||
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. I find the current behaviour backwards: I would think 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 a philosophical question, on the one hand is it called horizontal if the bars got vertical up and are alined along the horizontal axis or is it called vertical. 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. This is why I mentioned #2372. I assumed we were talking about the direction a single bar "grows". So if the bin dimension (width) is on the x-axis, then the bars grow on the y-axis and the plot is "vertical". |
||
""" | ||
|
||
def __init__(self, height, bottom=None, width=0.8, shift=0.0, color='w', orientation='h'): | ||
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. Would be awesome to be able to pass a per-bar color 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. 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. Yeah no need to do this here, I just pointed out it would be cool to add in the future :) 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 just looked into it, it should be not to problematic |
||
if bottom is None: | ||
bottom = np.zeros(height.shape[0]) | ||
|
||
rr, tris = calc_vertices(height, bottom, width, shift, orientation) | ||
|
||
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, orientation): | ||
|
||
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)) | ||
|
||
if orientation == 'h': | ||
vertices[:, 0] = vertices[:, 0] + stack_n_x2[:, 0].repeat(2) | ||
vertices[:, 1] = y_position | ||
|
||
elif orientation == 'v': | ||
vertices[:, 1] = vertices[:, 0] + stack_n_x2[:, 0].repeat(2) | ||
vertices[:, 0] = y_position | ||
else: | ||
raise ValueError('orientation can only be v (vertical) or h (horizontal). You specified: ' + str(orientation)) | ||
|
||
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.