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
2 changes: 1 addition & 1 deletion examples/scene/bar_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

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'
orientation='v', color=color_array, parent=vb1.scene) # or set it to 'h'

vb1.camera.set_range()

Expand Down
56 changes: 39 additions & 17 deletions vispy/visuals/bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# -----------------------------------------------------------------------------

import numpy as np

from vispy.color.color_array import ColorArray
from .mesh import MeshVisual


Expand All @@ -23,8 +25,8 @@ class BarVisual(MeshVisual):
Width of all bars
shift : int | float
Shift of all bars along the x-axis
color : instance of Color
Color of the bar.
color : ColorArray | numpy.ndarray | str
Color of the bars or you can pass a 2d array with rgb values for each individual bar
orientation : {'h', 'v'}
Orientation of the bars - 'v' is default
'v' : |
Expand All @@ -33,23 +35,33 @@ class BarVisual(MeshVisual):
[[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='v', color_array=None):
def __init__(self, height, bottom=None, width=0.8, shift=0.0, color='w', orientation='v'):
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)

color_array = None

if isinstance(color, np.ndarray):
if len(color.shape) == 2:
if color.shape[1] == 3:
if color.shape[0] > 1 and color.shape[0] != 0:
color_array = np.repeat(color, 2, axis=0)
color = None
else:
raise ValueError('If you pass an numpy array as color it needs to be rgb i.e. 3 columns')
elif isinstance(color, ColorArray):
pass
elif isinstance(color, str):
pass
else:
raise ValueError("Color has to be either a vispy ColorArray, numpy array or a string for example: 'w'")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry, maybe I wasn't very clear: using the ColorArray is specifically to get rid of all this extra logic (and unnecessary restrictions like passing only rgb). You can do this:

Suggested change
color_array = None
if isinstance(color, np.ndarray):
if len(color.shape) == 2:
if color.shape[1] == 3:
if color.shape[0] > 1 and color.shape[0] != 0:
color_array = np.repeat(color, 2, axis=0)
color = None
else:
raise ValueError('If you pass an numpy array as color it needs to be rgb i.e. 3 columns')
elif isinstance(color, ColorArray):
pass
elif isinstance(color, str):
pass
else:
raise ValueError("Color has to be either a vispy ColorArray, numpy array or a string for example: 'w'")
color = ColorArray(color).rgba
if len(color_array):
color = color_array[0]
color_array = None
else:
color = None

and then later

MeshVisual.set_data(self, rr, tris, color=color, face_colors=color_array)

Copy link
Contributor Author

@Bliss3d Bliss3d Oct 5, 2022

Choose a reason for hiding this comment

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

I'm not sure how this is supposed to work, color_array is not defiened in that context

Copy link
Collaborator

@brisvag brisvag Oct 5, 2022

Choose a reason for hiding this comment

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

sorry, wrote that in a rush. Fixed:

        color = ColorArray(color).rgba
        if len(color):
        	  color = color[0]
            color_array = None
        else:
            color = None
			  color_array = color

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 modified it a bit, but I hope it works for you


rr, tris = calc_vertices(height, bottom, width, shift, orientation)

if color_array is not None:
if color_array.shape[0] == height.shape[0] and color_array.shape[1] == 3:
color_array = np.repeat(color_array, 2, axis=0)
else:
raise ValueError('color_array needs to have the same length (' + color_array.shape[0] + ') as height ('
+ height.shape[0] + ') and 3 rows: ' + color_array.shape)

MeshVisual.__init__(self, rr, tris, color=color, face_colors=color_array)

def update_data(self, height, bottom=None, width=0.8, shift=0.0, color='w', orientation='v', color_array=None):
Expand All @@ -59,15 +71,25 @@ def update_data(self, height, bottom=None, width=0.8, shift=0.0, color='w', orie
if height.shape != bottom.shape:
raise ValueError("Height and Bottom must be same shape: Height: " + height.shape + " Bottom: "
+ bottom.shape)

color_array = None

if isinstance(color, np.ndarray):
if len(color.shape) == 2:
if color.shape[1] == 3:
if color.shape[0] > 1 and color.shape[0] != 0:
color_array = np.repeat(color, 2, axis=0)
color = None
else:
raise ValueError('If you pass an numpy array as color it needs to be rgb i.e. 3 columns')
elif isinstance(color, ColorArray):
pass
elif isinstance(color, str):
pass
else:
raise ValueError("Color has to be either a vispy ColorArray, numpy array or a string for example: 'w'")

rr, tris = calc_vertices(height, bottom, width, shift, orientation)

if color_array is not None:
if color_array.shape[0] == height.shape[0] and color_array.shape[1] == 3:
color_array = np.repeat(color_array, 2, axis=0)
else:
raise ValueError('color_array needs to have the same length (' + color_array.shape[0] + ') as height ('
+ height.shape[0] + ') and 3 rows: ' + color_array.shape)

MeshVisual.set_data(self, rr, tris, color=color, face_colors=color_array)

Expand Down