I need to update an already existing figure. However I want it to be completely redrawn. I currently have:
import matplotlib.pyplot as plt
def plot_func(xdata, ydata, subplot_kw=None, axis_setters=None):
fig, ax = plt.subplots(**subplot_kw)
fig.clf()
ax.cla()
fig, ax = plt.subplots(**subplot_kw)
if axis_setters:
for setter_name, setter_value in axis_setters.items():
setter_func = getattr(ax, setter_name)
setter_func(setter_value)
plot(xdata, ydata)
return fig, ax
and then at later times it will be called with:
plot_func([1,2,3], [1,2,3], subplot_kw={'num': 'name'}, axis_setters={'set_title': 'Title'})
The problem is, the awkward:
fig, ax = plt.subplots(**subplot_kw)
fig.clf()
ax.cla()
fig, ax = plt.subplots(**subplot_kw)
I need it because I need to call plot_func an arbitrary amount of times, and each time I want it to produce the correct plot. If I don't have the second: fig, ax = plt.subplots(**subplot_kw)
axis_setters entries don't work.
If I don't have the fig.clf()
recalls of the function overwrite the old figure and lines and text gets blurry.
So what is the "correct" way of doing it?
Okay it seems to be misunderstandable:
Here is the plot without the first 3 lines after 10 calls:
and here after 10 calls with the strange 3 lines at the beginning:
Notice how the fonts get blurry, because, they get redrawn each time. This gets worse with each call. Its not an compression artifact or anything. It is, because the figure get not redrawn correctly.
Now you might say: "Hey then just don't update the complete figure but only its data content" like they do here. The problem here is, that I cant change axes/figure properties any more. Take the e.g. axis_setters={'set_title': 'Title'}
I gave in the beginning. If I remove the second fig, ax = plt.subplots(**subplot_kw)
this call of a setter doesn't do anything any more. Same happens when you add a fig.canvas.draw()
the title will still not show up.
Edit: So this brings me almost there:
import matplotlib.pyplot as plt
def plot_func(xdata, ydata, figure_kw={}, subplot_args=[111], subplot_kw={}, axis_setters=None):
fig = plt.figure(**figure_kw)
ax = fig.add_subplot(*subplot_args, **subplot_kw)
ax.cla()
if axis_setters:
for setter_name, setter_value in axis_setters.items():
setter_func = getattr(ax, setter_name)
setter_func(setter_value)
plot(xdata, ydata)
return fig, ax
and call with:
for i in range(50):
plot_func([1,2,3], [1,2,3], figure_kw={'num': 'name'}, axis_setters={'set_title': 'Title'})
works, now the only "issue" is the: subplot_args=[111], subplot_kw={}
any way of combining this into one keyword. This is now only for convenience, because thats the way I handle almost all other keyword arguemnts through the code this is in, and it just sticks out as strange.