-
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 6 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,45 @@ | ||
# -*- 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) | ||
|
||
color_array = np.array([[1,0,0], [0,1,0], [0, 0, 1], [0.5, 1, 0.25], [1, 0.5, 0.25], [0.25, 0.5, 1], [1,0,0], [0,1,0], [0, 0, 1], [0.5, 1, 0.25], [1, 0.5, 0.25], [0.25, 0.5, 1]]) | ||
|
||
bar1 = scene.Bar(height=np.arange(0, 6, 0.5), # for a more traditional horizontal plot, either remove the | ||
bottom=np.arange(0, 3, 0.25), width=0.8, shift=0.5, # orientation paramenter outright | ||
orientation='v', color_array=color_array, 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,99 @@ | ||||||||||||||||
# -*- 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 bars - 'h' is default | ||||||||||||||||
color_array : array-like | ||||||||||||||||
[[1, 0, 0], [0, 1, 0], [0, 0, 1]] exactly one rgb array for each bar. This would be for 3 Bars | ||||||||||||||||
""" | ||||||||||||||||
|
||||||||||||||||
def __init__(self, height, bottom=None, width=0.8, shift=0.0, color='w', orientation='h', color_array=None): | ||||||||||||||||
if bottom is None: | ||||||||||||||||
bottom = np.zeros(height.shape[0]) | ||||||||||||||||
|
||||||||||||||||
if height.shape != bottom.shape: | ||||||||||||||||
raise ValueError("Height and Bottom must be same shape: Height: " + height.shape + " Bottom: " + bottom.shape) | ||||||||||||||||
|
||||||||||||||||
rr, tris = calc_vertices(height, bottom, width, shift, orientation) | ||||||||||||||||
|
||||||||||||||||
if color_array is not None and color_array.shape[0] == height.shape[0] and color_array.shape[1] == 3: | ||||||||||||||||
MeshVisual.__init__(self, rr, tris, face_colors=np.repeat(color_array, 2, axis=0)) | ||||||||||||||||
else: | ||||||||||||||||
MeshVisual.__init__(self, rr, tris, color=color) | ||||||||||||||||
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. Great! To get rid of this if clause (and get extra perks like string inputs and aplha values) I would use 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. @Bliss3d you think you can get this to work with a single vispy/vispy/visuals/markers.py Lines 607 to 613 in f13d7da
As you can see, you can use 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. I'm guessing something like this |
||||||||||||||||
|
||||||||||||||||
def update_data(self, height, bottom=None, width=0.8, shift=0.0, color='w', orientation='h', color_array=None): | ||||||||||||||||
if bottom is None: | ||||||||||||||||
bottom = np.zeros(height.shape[0]) | ||||||||||||||||
|
||||||||||||||||
if height.shape != bottom.shape: | ||||||||||||||||
raise ValueError("Height and Bottom must be same shape: Height: " + height.shape + " Bottom: " + bottom.shape) | ||||||||||||||||
|
||||||||||||||||
rr, tris = calc_vertices(height, bottom, width, shift, orientation) | ||||||||||||||||
|
||||||||||||||||
if color_array is not None and color_array.shape[0] == height.shape[0] and color_array.shape[1] == 3: | ||||||||||||||||
MeshVisual.set_data(self, rr, tris, face_colors=np.repeat(color_array, 2, axis=0)) | ||||||||||||||||
else: | ||||||||||||||||
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(height.shape[0]).repeat(2).reshape(-1, 1) | ||||||||||||||||
|
||||||||||||||||
vertices = np.array([ | ||||||||||||||||
[-width/2 + shift, 1], | ||||||||||||||||
[width/2 + shift, 1], | ||||||||||||||||
[width/2 + shift, 0], | ||||||||||||||||
[-width/2 + shift, 0]]) | ||||||||||||||||
|
||||||||||||||||
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 | ||||||||||||||||
|
||||||||||||||||
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.