Skip to content
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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions examples/scene/bar_plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import numpy as np
Copy link
Member

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.

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()
1 change: 1 addition & 0 deletions vispy/scene/visuals.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ def generate_docstring(subclass, clsname):

Arrow = create_visual_node(visuals.ArrowVisual)
Axis = create_visual_node(visuals.AxisVisual)
Bar = create_visual_node(visuals.BarVisual)
Box = create_visual_node(visuals.BoxVisual)
ColorBar = create_visual_node(visuals.ColorBarVisual)
Compound = create_visual_node(visuals.CompoundVisual)
Expand Down
1 change: 1 addition & 0 deletions vispy/visuals/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"""

from .axis import AxisVisual # noqa
from .bar import BarVisual # noqa
from .box import BoxVisual # noqa
from .cube import CubeVisual # noqa
from .ellipse import EllipseVisual # noqa
Expand Down
74 changes: 74 additions & 0 deletions vispy/visuals/bar.py
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing orientation here

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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]])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the point of the 0 at index 2? I'm having a hard time following the flow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Collaborator

Choose a reason for hiding this comment

The 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 width rather than a single value... My bad :P

Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, maybe this use of stack_n_x2 counters my proposal of using repeat(4) above, if I'm following.


return vertices, faces