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
username
  • 3
  • 1
  • 4
  • 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. – ImportanceOfBeingErnest Mar 15 '19 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 – username Mar 15 '19 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. – ImportanceOfBeingErnest Mar 15 '19 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? – username Mar 15 '19 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. – ImportanceOfBeingErnest Mar 15 '19 at 23:03
  • Yes, I agree it's not readable in detail. It's a map of geologic features that can't readily be simplified. It is there for completion and general reference; the other subplots are where the real info is, and those have ~100 points. I think iterating will work; if you want to copy your comment to an answer, I will accept it. Thank you for your help. – username Mar 15 '19 at 23:13
  • Even if you think you cannot compress the information into fewer points, still drawing less artists with the same amount of total points will be beneficial. See e.g. [this answer](https://stackoverflow.com/questions/54492963/many-plots-in-less-time-python/54544965#54544965) – ImportanceOfBeingErnest Mar 15 '19 at 23:22
  • I agree it is beneficial from drawing perspective, but something like [this](https://imgur.com/Y8wqdQS) cannot be readily simplified for 50+ datasets all for the purpose quicker drawing. – username Mar 15 '19 at 23:28
  • Well, it can. It's even all the same color, so you will need exactly one `LineCollection` with a single color. That is much quicker than 9000 individual lines (as I've tried to show in the linked answer). Of course creating the `LineCollection` is a bit more cumbersome. – ImportanceOfBeingErnest Mar 15 '19 at 23:40
  • I see, I thought you meant manually simplifying the lines in an editor. That was a lazy example; there are actually two different line colors. I didn't know about `LineCollection`--I will look through the documentation (looks like multiple line colors is do able), thanks for mentioning that. Even with using line collection, I still like the idea of iteratively updating the other subplots. Many thanks for your help – username Mar 15 '19 at 23:56

0 Answers0