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 6 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
45 changes: 45 additions & 0 deletions examples/scene/bar_plot.py
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
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)

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()
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
99 changes: 99 additions & 0 deletions vispy/visuals/bar.py
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 ColorArray just like you did in the Candle PR.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@Bliss3d you think you can get this to work with a single color argument? I think it would be more intuitive, kinda like Markers does in these few lines:

edge_color = ColorArray(edge_color).rgba
if len(edge_color) == 1:
edge_color = edge_color[0]
face_color = ColorArray(face_color).rgba
if len(face_color) == 1:
face_color = face_color[0]

As you can see, you can use the ColorArray object from vispy.color, which automatically takes care of all the weird cases (a string like 'w', a (n, 3) rgb array, or an rgba, and so on). You can then simply check if it's a single color, and if so pass it to color, otherwise pass it to face_colors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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