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

legends do not refresh #6166

Closed
7 tasks
emdete opened this issue Mar 16, 2016 · 11 comments
Closed
7 tasks

legends do not refresh #6166

emdete opened this issue Mar 16, 2016 · 11 comments
Labels
status: needs clarification Issues that need more information to resolve. topic: animation

Comments

@emdete
Copy link

emdete commented Mar 16, 2016

  • matplotlib 1.5.1
  • python2.7 2.7.11
  • running on Linux
  • installed via Debian package
  • i will check if i can create a sample showing the issue
  • it's an animation, so no screenshot, a screencapturevideo could be attached (a video writer does not work, it doesnt show the legend at all)
  • i just start learning Matplotlib so i assume it's not a regression

i created an animation that shows a live result-code distribution parsed from logfiles. my frames function reads a logfile (tail -f like), parses each line for the resultcode and returns just the resultcode. the animate function increments a counter for that code from which stacked bars are drawn.

the result-codes are unknown at startup so the animate function "invents" new bars on the fly with new labels. the legend is created in the init_func (and complains that there are no labels in my plots - it does not know they show up later).

the legend shows only one label - the first one that was added. this does not change when the program runs for a while until i resize the window. in that moment the correct number of labels will be shown. is this a bug or do i have to issue some 'refresh()' method to wake up the legend?

@tacaswell
Copy link
Member

Are you using blitting?

@tacaswell tacaswell added topic: animation status: needs clarification Issues that need more information to resolve. labels Mar 16, 2016
@tacaswell tacaswell added this to the unassigned milestone Mar 16, 2016
@emdete
Copy link
Author

emdete commented Mar 16, 2016

i have a blit=True in the FuncAnimation call, so probably: yes.

just checked: not setting it or setting to False does not work at all, the windows stays gray.

@tacaswell
Copy link
Member

Can you make a minimal example? Starting from one of the example might be a good starting point if your actual use case is very involved.

There is enough subtlety in the animation code that my trying to guess what you are doing would not go well 😉

@emdete
Copy link
Author

emdete commented Mar 16, 2016

ok, i cut it down to this:

https://gist.github.com/emdete/8212d76bf574eb281b97

@emdete
Copy link
Author

emdete commented Mar 17, 2016

i just found that the init_func() is called after each resize... it seems that the call to the legend() function at the moment where the bars are already there refreshes the legend. calling it when a bar is created doesn't help either, in that case several legends are created which seem to cover each other.

@emdete
Copy link
Author

emdete commented Mar 17, 2016

i now added a legend.remove() and a plt.legend() in case a new bar was created. and now it gets scary: i get a "AttributeError: 'NoneType' object has no attribute 'remove'" exception. i found out that the animate function is called /before/ the init_func! this seems to be a bug as well... but it's the cause the first legend entry show up properly because a call to animate will create a bar in my code.

@emdete
Copy link
Author

emdete commented Mar 17, 2016

ok, i found it. it seems the legend must be returned in the list of artists from the animation function to be updated.

@emdete emdete closed this as completed Mar 17, 2016
@tacaswell
Copy link
Member

I am glad you found a work around 😄

One comment from looking at your code is that you should use the OO interface in this sort of situation. Where you have

self.figure = plt.figure()

do

self.figure, self.ax = plt.subplots()

and then the rest of the calls to plt.* can be replaced by self.ax.*.

I also suggest creating all of the artists you will need in init (and return them) and then return any artists you plan to update from animate. That is how to get the best performance out of blitting.

@emdete
Copy link
Author

emdete commented Mar 18, 2016

yes, i was wondering why matplotlib was so non-py and non-oo. i found the first method in most of the samples while i found the second on some stackoverflow-snippets. i was wondering that the second works (saw it only in a multi-plot) and what the benefit is because it's so seldom in the samples.

and: it's not working. i replaced most plt. for self.ax. and get errors, AxesSubplot does not have xticks, xlabel, ... :(

@WeatherGod
Copy link
Member

Here is my explanation about pyplot vs. axes methods:
http://nbviewer.jupyter.org/github/WeatherGod/AnatomyOfMatplotlib/blob/master/AnatomyOfMatplotlib-Part1-Figures_Subplots_and_layouts.ipynb#Axes-methods-vs.-pyplot

tl;dr;

Matplotlib has its roots in interactive plotting via a REPL a la MatLab.
So, having concise, implicit semantics is what it started off with. As it
grew beyond those initial design goals, the OO-interface came about and it
makes everything explicit and unambiguous at the expense of conciseness.

On Fri, Mar 18, 2016 at 4:23 AM, M. Dietrich notifications@github.com
wrote:

yes, i was wondering why matplotlib was so non-py and non-oo. i found the
first method in most of the samples while i found the second on some
stackoverflow-snippets. i was wondering that the second works (saw it only
in a multi-plot) and what the benefit is because it's so seldom in the
samples.


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
#6166 (comment)

@tacaswell
Copy link
Member

some of those do get renamed to set_xlabel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs clarification Issues that need more information to resolve. topic: animation
Projects
None yet
Development

No branches or pull requests

3 participants