【问题标题】:How can I supress unwanted plotly figs from printing?如何抑制不想要的无花果打印?
【发布时间】:2021-11-22 14:55:53
【问题描述】:

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

  1. 第一个是没有任何水平线,
  2. 第二个有第一条水平线,
  3. 第三个图有第二条水平线
  4. 第四个具有更正的轴名称,并且
  5. 最后一个打印是正确的(与fig.show() 相关联)。

有没有办法在最后一行代码之前“静音”输出?也许这是与网状有关的问题?

谢谢!

# 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()

【问题讨论】:

    标签: python r plotly r-markdown reticulate


    【解决方案1】:

    为了简单起见,我使用 pandas 和 numpy 而不是 plotly。在您的 Rmarkdown 块中,您可以使用 include = FALSE静音生成 Python 绘图的 R 块。您可以将每个图放在它自己的块中,但每个块都需要一个唯一的名称(chunk1,chunk2),或者您可以将前 4 个图放在 R 块中,include = FALSE 和您想要在普通块中显示的图.请看下面的代码有 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()
    ```
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-28
      • 1970-01-01
      相关资源
      最近更新 更多