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 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions examples/scene/bar_plot.py
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
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), # 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()
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
86 changes: 86 additions & 0 deletions vispy/visuals/bar.py
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
Copy link
Collaborator

Choose a reason for hiding this comment

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

I find the current behaviour backwards: I would think h means that the bars are horizontal, but here it seems to refer to the orientation of the "baseline" of the plot. Is this typical?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
I was thinking along which axis are the bars alined as opposed to the direction of the bars
I'm have no clue, I''ll use the vispy philosophical bend so its inline with other visuals

Copy link
Member

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

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 think I implemented something kinda like that in #2388 and those PR's are very samey.
#2388 has also more comments trying to explain whats going on

Copy link
Collaborator

Choose a reason for hiding this comment

The 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 :)

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 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)
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, 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]])
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))

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