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 1 commit
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
8 changes: 5 additions & 3 deletions examples/scene/bar_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@

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'
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()

Expand Down
43 changes: 28 additions & 15 deletions vispy/visuals/bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,39 @@ class BarVisual(MeshVisual):
color : instance of Color
Color of the bar.
orientation : {'h', 'v'}
Orientation of the histogram - 'h' is default
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'):
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)

MeshVisual.__init__(self, rr, tris, color=color)
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'):
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])
rr, tris = calc_vertices(height, bottom, width, shift=shift)

MeshVisual.set_data(self, rr, tris, color=color)
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):
Expand All @@ -54,16 +70,13 @@ def calc_vertices(height, bottom, width, shift, orientation):

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)
stack_n_x2 = np.arange(height.shape[0]).repeat(2).reshape(-1, 1)

vertices = np.array([
[-width/2 + shift, 1, 0],
[width/2 + shift, 1, 0],
[width/2 + shift, 0, 0],
[-width/2 + shift, 0, 0]])
[-width/2 + shift, 1],
[width/2 + shift, 1],
[width/2 + shift, 0],
[-width/2 + shift, 0]])

vertices = np.tile(vertices, (height.shape[0], 1))

Expand All @@ -81,6 +94,6 @@ def calc_vertices(height, bottom, width, shift, orientation):

base_faces = np.tile(base_faces, (height.shape[0], 1))

faces = stack_n_x2*4 + base_faces
faces = stack_n_x2 * 4 + base_faces

return vertices, faces