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

Fix stacked bar plot example, by using the correct starting_value #498

Merged
merged 2 commits into from
Jan 2, 2020
Merged
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: 4 additions & 4 deletions examples/demo/basic/bar_plot_stacked.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from traitsui.api import UItem, View

# Chaco imports
from chaco.api import LabelAxis, Plot, ArrayPlotData
from chaco.api import LabelAxis, Plot, ArrayPlotData, ArrayDataSource

class PlotExample(HasTraits):
plot = Instance(Plot)
Expand All @@ -28,8 +28,8 @@ def __init__(self, index, series_a, series_b, series_c, **kw):
plot_data.set_data('series_c', series_c)
self.plot = Plot(plot_data)
self.plot.plot(('index', 'series_a'), type='bar', bar_width=0.8, color='auto')
self.plot.plot(('index', 'series_b'), type='bar', bar_width=0.8, color='auto')
self.plot.plot(('index', 'series_c'), type='bar', bar_width=0.8, color='auto')
self.plot.plot(('index', 'series_b'), type='bar', bar_width=0.8, color='auto', starting_value=ArrayDataSource(series_a))
self.plot.plot(('index', 'series_c'), type='bar', bar_width=0.8, color='auto', starting_value=ArrayDataSource(series_b))

# set the plot's value range to 0, otherwise it may pad too much
self.plot.value_range.low = 0
Expand All @@ -47,7 +47,7 @@ def __init__(self, index, series_a, series_b, series_c, **kw):


index = numpy.array([1,2,3,4,5])
demo = PlotExample(index, index*10, index*5, index*2)
demo = PlotExample(index, index*10, index*15, index*17)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it not possible to do this without changing the input data?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Docstring for starting_value says the following:

    #: The data source to use as "starting" values for bars (along value axis).
    #: For instance, if the values are [10, 20] and starting_value
    #: is [3, 7], BarPlot will plot two bars, one  between 3 and 10, and
    #: one between 7 and 20

I think BarPlot is designed not to change the value when stacking, so the user may have to do it explicitly.

Copy link
Contributor

Choose a reason for hiding this comment

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

You could reverse the input data instead. The original version of this relied on the rendering order (the tallest bar was drawn first, etc.).

Even better might be to sum the data as part of the process of creating the plots (ie. barplot 1 is from 0 to series_a, barplot 2 is from series_a to series_a + series_b, etc.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, that's better, made the change.


if __name__ == "__main__":
demo.configure_traits()