0

I am creating about one dozen figures with subplots. There is one subplot that is the same for all the figures. However, it takes a loonnggg time to draw. Is there a way to only draw this subplot once and then replicate it in each figure? (Not the same thing as this accepted answer, which just defines a function that redraws the subplot every time.)

A snippet of the large source data file for the slow-drawing plot is below, as well as my current code for generating the subplot.

Code:

fig1 = plt.figure()
ax1 = plt.subplot2grid((2,2), (0,0), rowspan=2, colspan=1)
for ii in df_lines.Line_ID: ## df_lines.Line_ID = max(df_points.ID)
    temp = df_points.loc[df_points.ID == ii]
    df_myline = temp.sort_values(by='Order_ID', ascending=True)
    del temp
    x = df_line.X
    y = df_line.Y
    ax1.plot(x, y)

df_points Snippet: Note there are decimals here in X,Y they just got cut off here

ID  Order_ID    X   Y
1   1   -116    35
1   2   -116    35
2   1   -116    35
2   2   -116    35
3   1   -116    35
3   2   -116    35
3   3   -116    35
4   1   -116    35
4   2   -116    35
5   1   -116    35
5   2   -116    35
6   1   -116    35
6   2   -116    35
7   1   -116    35
7   2   -116    35
8   1   -116    35
8   2   -116    35
9   1   -116    35
9   2   -116    35
10  1   -116    35
10  2   -116    35
10  3   -116    35
10  4   -116    35
10  5   -116    35
10
  • If this is for showing in an interactive backend on screen you could blit the respective region. But in general, the answer to this is no, it's not possible. Commented Mar 15, 2019 at 22:30
  • @ImportanceOfBeingErnest I'm not sure what an interactive backend is, but I don't think that's the use here; I am just saving the figures as pdf Commented Mar 15, 2019 at 22:33
  • Ok, so saving means you necessarily need to draw the figure (and blitting will not help). Since each artist can only live in one figure, you cannot use the same artist for several figures. I'm not sure what exactly takes so long in your case; if it is the creation of the artist, you might benefit from creating one figure with this subplot in it, and changing all the rest around it iteratively, each time saving the figure. But if the bottleneck is the drawing of the artist, that procedure will also not help. Commented Mar 15, 2019 at 22:46
  • @ImportanceOfBeingErnest drawing the 'artist' once isn't a big deal, but drawing it 12x is. Depending on the dataset, it has up to ~300k rows that become ~9k lines to plot on subplot ax1 (worst case). Changing iteratively may work; you mean just drawing ax2 = plt.subplot2grid((2,2), (0,1), rowspan=1, colspan=1), plotting those xy, then delete ax2, redraw, all without closing fig1, right? Commented Mar 15, 2019 at 22:53
  • Right, that's what I meant. Though I wonder if plotting 9000 lines actually makes much sense. Are you sure you cannot simplify the plotting? E.g. creating 9000 lines is significantly more costly than plotting a single line with the same amount of points; also, I have some doubts if a plot with that many lines is readable at all. Commented Mar 15, 2019 at 23:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.