如何抑制打印不需要的 plotly figs?

How can I supress unwanted plotly figs from printing?

我无法在我正在处理的 r markdown 文档中绘制图表(使用 python 和 plotly)。使用下面的代码,我得到五个以不同状态打印的数字,例如

  1. 第一个没有横线,
  2. 第二个有第一条横线,
  3. 第三个数字有第二条横线
  4. 第四个具有更正的轴名称,并且
  5. 最后一张是正确的(与 fig.show() 相关)。

有没有办法 'mute' 输出到最后一行代码?也许这是与网纹有关的问题?

谢谢!

# plotting the bar chart
fig = px.bar(df2122, x="DateYear", y="kg", template="plotly_white") 
fig.add_hline(y=169117.6839, annotation_text="Monthly average 2019/20", annotation_position="top right")
fig.add_hline(y=75584.48002, annotation_text="Monthly average 2020/21", annotation_position="top right")

fig.update_layout(xaxis_title="Date", yaxis_title="kg")

# showing the plot
fig.show()

为了简单起见,我使用了 pandas 和 numpy 而不是 plotly。在你的 Rmarkdown 块中,你可以使用 include = FALSEmute 生成你的 python 图的 R 块。您可以将每个图放在它自己的块中,但每个块都需要一个唯一的名称(块 1、块 2),或者您可以将前 4 个图放在 include = FALSE 的 R 块中,然后将要显示的图放在普通块。请参阅下面的代码有 2 个图,但在呈现文档后仅显示其中 1 个。

---
title: "Untitled"
author: "author"
date: "10/1/2021"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(reticulate)
```

```{python, chunk1}
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(10,4),index=pd.date_range('1/1/2000',
   periods=10), columns=list('ABCD'))

df.plot()
```

```{python, chunk2, include = FALSE}
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(10,4),index=pd.date_range('1/1/2000',
   periods=10), columns=list('ABCD'))

df.plot()
```