enthought / chaco

Chaco is a Python package for building interactive and custom 2-D plots.
http://docs.enthought.com/chaco/
Other
292 stars 99 forks source link

BarPlot with vertical axis in time issue #818

Closed fred4ets closed 2 years ago

fred4ets commented 2 years ago

Hello,

I would like to display data using BarPlot but with vertical axis in time.

Please see the CME below to reproduce the issue shown in attached snapshots.

#! /usr/bin/env python3

# Major library imports
import numpy

# Enthought library imports
from enable.api import ComponentEditor
from traits.api import Bool, HasTraits, Instance
from traitsui.api import UItem, View

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

from chaco.scales.api import CalendarScaleSystem
from chaco.scales.time_scale import HMSScales, MDYScales
from chaco.scales_tick_generator import ScalesTickGenerator

class PlotExample(HasTraits):

    plot = Instance(Plot)

    time_scale_flag = Bool(True)

    def _plot_default(self):
        index = numpy.array([1, 2, 3, 4, 5])
        a = index * 1000
        if self.time_scale_flag:
            a -= 3600
        plot_data = ArrayPlotData(index=index)
        plot_data.set_data("a", a)
        plot = Plot(plot_data)
        plot.plot(
            ("index", "a"), type="bar", bar_width=0.8, color="auto"
        )

        # set the plot's value range to 0, otherwise it may pad too much
        plot.value_range.low = 0

        if self.time_scale_flag:
            y_axis = plot.underlays[3]
            y_axis.tick_generator = ScalesTickGenerator(scale=CalendarScaleSystem(scales=HMSScales))

            low = plot.range2d.y_range.low
            low -= 3600
            plot.range2d.y_range.low = low

        return plot

    traits_view = View(
        UItem("plot", editor=ComponentEditor()),
        width=800,
        height=800,
        resizable=True,
    )

demo = PlotExample()

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

If you set time_scale_flag to False, display is good: snapshot1

But if you set it to True, display should look like the first, but this is not the case, it is bad: snapshot2

Am I doing something wrong here, or missed something?

Thanks in advance.

Regards,

Debian x86_64, Python 3.9.2, ETS source from git

fred4ets commented 2 years ago

Hello,

Any news about this issue? I'm stuck with it :/

Thanks.

rahulporuri commented 2 years ago

We're sorry @fred4ets but we haven't been able to look into this yet and we're not sure when we will be able to. Have you been able to investigate further? We are definitely open to external contributions which might fix this issue

corranwebster commented 2 years ago

I can shed some light I think - time axes in Chaco are for absolute times (ie. the value is seconds since the Unix Epoch in UTC) which are then converted to the current computer timezone for display - I think that the "negative" values are coming from the timezone conversion where the Epoch occurred at 1am in your timezone. But there is a question of why the zero baseline isn't being mapped the same way so there may be a bug here (if you're on Windows and in the UK there could be additional issues because of daylight savings experimentation in the UK in 1970 which Windows doesn't correctly handle). You might be able to fix by providing a starting_value data source which is all zeros.

More deeply, what I suspect is happening is that the data you have are really timedeltas, rather than absolute times. Chaco doesn't currently have a timedelta scale system, so you may need to roll your own. The main benefit of doing this would be aesthetics around having tick marks at values like 15, 30, 45 rather than 25, 50, 75 for example, and displaying values as HH:MM:SS (although you could do this with a custom formatter). This is likely a fair bit of work to implement.

fred4ets commented 2 years ago

Hello,

Thanks for the hint, setting starting_value to -3600 fixes the issue.

Thanks a lot!

Regards