Giter Club home page Giter Club logo

Comments (5)

spencerkclark avatar spencerkclark commented on June 3, 2024

Perhaps it says something about the nature of these tests I added in #84, but I tested things locally and, as written, they fail if I do not use matplotlib.use("agg"). For example:

___________________________________ test_set_ticks_with_CFTimeFormatter[List[CalendarDateTime]-x] ___________________________________

axis = 'x', ticks = [<CalendarDateTime: datetime=1986-02-01 00:00:00, calendar={self.calendar}>]

    @pytest.mark.parametrize("axis", ["x", "y"])
    @pytest.mark.parametrize("ticks", TICKS.values(), ids=list(TICKS.keys()))
    def test_set_ticks_with_CFTimeFormatter(axis, ticks):
        times = [cftime.Datetime360Day(1986, month, 30) for month in range(1, 6)]
        data = range(len(times))
        fig, ax = plt.subplots(1, 1)
        formatter = nc_time_axis.CFTimeFormatter("%Y-%m", "360_day")
        if axis == "x":
            ax.plot(times, data)
            ax.set_xticks(ticks)
            ax.xaxis.set_major_formatter(formatter)
            fig.canvas.draw()
            ticklabels = ax.get_xticklabels()
        else:
            ax.plot(data, times)
            ax.set_yticks(ticks)
            ax.yaxis.set_major_formatter(formatter)
            fig.canvas.draw()
            ticklabels = ax.get_yticklabels()
        result_labels = [label.get_text() for label in ticklabels]
        expected_labels = ["1986-02"]
>       assert result_labels == expected_labels
E       AssertionError: assert [''] == ['1986-02']
E         At index 0 diff: '' != '1986-02'
E         Full diff:
E         - ['']
E         + ['1986-02']

nc_time_axis/tests/integration/test_plot.py:139: AssertionError

It appears in order to get these tests to pass I need to use plt.show() instead of fig.canvas.draw() when using a GUI backend. This results in a GUI plot showing up for each test, which I need to close by hand before the testing code continues (not ideal).

Testing that the tick labels were set appropriately is a bit of a tricky problem. The solution I used came from this StackOverflow answer, but perhaps there is a more robust way. In lieu of a better approach, I would say based on @rcomer's comment -- #66 (comment) -- and my experience here that I would be inclined to keep matplotlib.use("agg") for now.

from nc-time-axis.

mathause avatar mathause commented on June 3, 2024

There is of course no harm at all in leaving it in, and if it causes issues by removing there is no reason to do so. So feel free to ignore my comment below.


I was interested to see if I could reproduce your issue and wasn't able - I tried a small example with ipython and a small test and both worked for me as expected (see below). But I only use matplotlib proper - is this something nc-time-axis specific? Or what version of mpl do you use?

in ipython:

In [1]: import matplotlib.pyplot as plt
In [2]: fig, ax = plt.subplots(1, 1)
In [3]: x = [1, 2, 3]
In [4]: y = [4, 5, 6]
In [5]: ax.plot(x, y)
In [6]: ax.get_xticklabels()
Out[6]: 
[Text(0, 0, ''),
...]
In [7]: fig.canvas.draw()
In [8]: ax.get_xticklabels()
Out[8]: 
[Text(0.75, 0, '0.75'),
...]

using pytest:

import warnings

import matplotlib.pyplot as plt

def test_ticklabels():
    fig, ax = plt.subplots(1, 1)
    x = [1, 2, 3]
    y = [4, 5, 6]
    ax.plot(x, y)
    warnings.warn(str(ax.get_xticklabels()))
    fig.canvas.draw()
    warnings.warn(str(ax.get_xticklabels()))

from nc-time-axis.

spencerkclark avatar spencerkclark commented on June 3, 2024

Very interesting. I'm using matplotlib version 3.4.3. I can reproduce your example in an IPython shell, but not if I convert it to a script. I also get empty labels both times if I use your pytest example. Locally matplotlib chooses to use the "MacOSX" backend for me -- maybe you are using a different backend?

from nc-time-axis.

mathause avatar mathause commented on June 3, 2024

I am on linux and it selects Qt5Agg. Yes that seems to be a bug of the "MacOSX" backend: matplotlib/matplotlib#19197 & matplotlib/matplotlib#19268

from nc-time-axis.

spencerkclark avatar spencerkclark commented on June 3, 2024

Ah I didn't see those issues -- thanks! I think that is probably enough to convince me that we should stick with matplotlib.use("agg").

from nc-time-axis.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.