在稍后的代码中显示原始图 [英] Display seaborn plots at some point later in code

查看:70
本文介绍了在稍后的代码中显示原始图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们在代码的某个位置说一下,我有以下两个图形:ie graph_p_changes和graph_p_contrib

Let's say at some point in my code, I have following two graphs: i.e. graph_p_changes and graph_p_contrib

line_grapgh_p_changes = df_p_change[['year','interest accrued', 'trade debts', 'other financial assets']].melt('year', var_name='variables',  value_name='p_changes')
graph_p_changes = sns.factorplot(x="year", y="p_changes", hue='variables', data=line_grapgh_p_changes, height=5, aspect=2)
graph_p_changes.set(xlabel='year', ylabel='percentage change in self value across the years')

line_grapgh_p_contrib = df_p_contrib[['year','interest accrued', 'trade debts', 'other financial assets']].melt('year', var_name='variables',  value_name='p_changes')
graph_p_contrib = sns.factorplot(x="year", y="p_changes", hue='variables', data=line_grapgh_p_contrib, height=5, aspect=2)
graph_p_contrib.set(xlabel='year', ylabel='percentage chnage in contribution to total value')

现在稍后在我的c中颂歌,我只想显示以上两个图之一。但是,当我执行plt.show()时,它将在我的jupyter笔记本中显示以上两个图形。如何在代码的任何位置仅显示一个图形。

Now at some point later in my code, I just want to display one of the above two graphs. But when I do plt.show(), it displays both of the above graphs in my jupyter notebook. How can I display only one graph at any point in my code.

推荐答案

您将要引用为每个图,然后添加 .fig 以在Jupyter笔记本单元格中重新显示它。

You'll want to refer to the assigned variable for each plot and then add .fig after that to redisplay it in a Jupyter notebook cell.

具体来说,在您的情况下,您将引用 graph_p_changes.fig graph_p_contrib.fig 在一个单元格中并执行该单元格以再次查看单个图。

Specifically, in your case you'd reference graph_p_changes.fig or graph_p_contrib.fig in a cell and execute that cell to see an individual plot again.

这类似于您可以再次显示Seaborn的ClusterGrids,请参见< a href = https://stackoverflow.com/a/59935514/8508004>此处。因为您的问题的标题为 seaborn plots,为完整起见,我将添加该内容,这不适用于Seaborn的线图( lineplot )或bar图( barplot ),生成 AxesSubplot 对象。在这里,您使用 .figure ,例如 ax.figure 来调用 Seaborn的线图文档

This is similar to how you can show Seaborn's ClusterGrids again, see here. Because the title of your question said 'seaborn plots', I'll add for sake of completeness, this doesn't hold for plots like Seaborn's line plot (lineplot) or bar plot (barplot) , that produce AxesSubplot objects. There you use .figure, for example ax.figure to recall most of the examples listed on Seaborn's lineplot documentation.

这是使用此处 seaborn的猫图文档(请参见下文) )绘制两个图。如果此代码位于一个单元格中,然后运行了该单元格,您将在其下面的输出中看到两个图。

This is using example code from here and seaborn's catplot documentation (see below) to make two plots. If this code was in one cell and then that cell was run, you'd see two plots in the output below it.

import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

titanic = sns.load_dataset("titanic")
exercise = sns.load_dataset("exercise")
g = sns.catplot("alive", col="deck", 
                col_wrap=3, data=titanic[titanic.deck.notnull()], 
                kind="count", height=2.5, aspect=.8)

another_plot = sns.catplot(x="time", y="pulse", hue="kind", data=exercise)

以后,每个都可以再次显示为其他带有<$的单元格的输出c $ c> g.fig 或 another_plot.fig ,具体取决于要显示的图。

Later, each can be displayed again individually as output of other cells with g.fig or another_plot.fig, depending on which plot you want to show.

此外,我建议提高您的长期代码可行性,您可能要继续使用 catplot 在您的绘图呼叫中,因为t factorplot 现在被称为seaborn。请参见此处,其中表示 factorplot仍然存在,并将其参数传递给catplot()并带有警告。它可能最终会被删除,但过渡过程将是逐步进行的。

Additionaly, I'll suggest to improve your long-term code viability, you may want to move on to using catplot in your plotting calls as that is what factorplot is now called in seaborn. See here where it says "factorplot still exists and will pass its arguments through to catplot() with a warning. It may be removed eventually, but the transition will be as gradual as possible."

OP评论说,所需要的是允许代码散布的stdout / stderr输出,并在该流中的精确点处(而不只是在结尾处)进行绘图。

OP commented that what was desired was code allowing interspersed stdout/stderr output with plots at precise points among that stream and not just at the end.

由于某些原因,Seaborn图(甚至是简单的线图)似乎也不能通过 io.capture_output()正确地捕获。 / code>,因此我不得不在生产单元中使用 %% capture 单元魔术命令,并将输出合并到一个单独的单元中。但是,我根据示例代码尝试绘制的地块由 io.capture_output()并允许在同一单元格中轻松混合所有内容。 这里;最好以静态形式此处,因为Github不会渲染nlotviewer绘制的Plotly图。该笔记本的顶部包含一个链接,您可以在其中启动将在其中运行的活动Jupyter会话。

For some reason, Seaborn plots (even simple line plots) don't seem to get 'captured' correctly with io.capture_output(), and so I had to use the %%capture cell magic command in the producing cell and combine the output in a separate cell. However, Plotly plots I tried based on example code are captured by io.capture_output() and allow facile intermixing all in the same cell. This is all illustrated in an example notebook available here; it is best viewed in static form here as Github doesn't render the Plotly plots while nbviewer does. The top of that notebook includes a link where you can launch an active Jupyter session where it will run.

这篇关于在稍后的代码中显示原始图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆