-
Notifications
You must be signed in to change notification settings - Fork 618
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
Bliss3d
wants to merge
9
commits into
vispy:main
Choose a base branch
from
Bliss3d:bar_chart
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
New Visual: Bar #2394
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
324d4ba
New Visual: Bar
Bliss3d 847963e
flake8 corrected
Bliss3d 59a2791
Added: Docstring, Lincense and Copyright
Bliss3d e4a4aab
Underline same number as characters
Bliss3d 62b32aa
Added: Vertical bar ploting
Bliss3d 8c5de6d
Apply and added suggested changes
Bliss3d 7a21064
flake8, requested changes and swaped h and v
Bliss3d f6c3fa6
Consolidated color and color_array parameters
Bliss3d a4ca071
changes to color logic
Bliss3d File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# -*- 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 | ||
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([[0, 1, 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=color_array, parent=vb1.scene) # or set it to 'h' | ||
|
||
vb1.camera.set_range() | ||
|
||
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
# -*- 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 | ||
import warnings | ||
|
||
from vispy.color.color_array import ColorArray | ||
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 : 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' : | | ||
'h' : - | ||
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='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 | ||
|
||
color = ColorArray(color).rgba | ||
|
||
if len(color) == 1: | ||
pass | ||
elif len(color) != len(height): | ||
warnings.warn("Color array does not match the length of height, this means we can't match a color to each\ | ||
bar - we will use the first color for all bars") | ||
color = color[0] | ||
else: | ||
color_array = np.repeat(color, 2, axis=0) | ||
color = None | ||
|
||
rr, tris = calc_vertices(height, bottom, width, shift, orientation) | ||
|
||
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): | ||
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 | ||
|
||
color = ColorArray(color).rgba | ||
|
||
if len(color) == 1: | ||
pass | ||
elif len(color) != len(height): | ||
warnings.warn("Color array does not match the length of height, this means we can't match a color to each\ | ||
bar - we will use the first color for all bars") | ||
color = color[0] | ||
else: | ||
color_array = np.repeat(color, 2, axis=0) | ||
color = None | ||
|
||
rr, tris = calc_vertices(height, bottom, width, shift, orientation) | ||
|
||
MeshVisual.set_data(self, rr, tris, color=color, face_colors=color_array) | ||
|
||
|
||
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 == 'v': | ||
vertices[:, 0] = vertices[:, 0] + stack_n_x2[:, 0].repeat(2) | ||
vertices[:, 1] = y_position | ||
|
||
elif orientation == 'h': | ||
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.