繁体   English   中英

在 Quarto 中并排显示 R 和 Python 绘图

[英]Displaying R and Python Plots Side by Side in Quarto

我试图在 Quarto 文档中并排显示两个图。 问题是一个是通过 R 生成的,另一个是通过 Python 生成的。 理想情况下,我希望这些图像在本文档中那样显示: Quarto Example
但是,这似乎只适用于通过相同方法创建的图像(两个图都是在 R 中创建的,或者两者都是在 Python 中创建的)。 这是我从中获得此示例的链接: https://quarto.org/docs/get-started/computations/rstudio.html

此外,这是我用来创建这些图的代码:
R Plot:

{r, fig.show = 'hold', out.width = '50%'}
#| label: fig-r
#| fig-cap: "Car Weight Vs. MPG Scatterplot (R)"
#| layout-ncol: 2
#| column: page
library(ggplot2)

# Color Scale
mtcars$pc <- predict(prcomp(~wt + mpg, mtcars))[,1]

# Scatter Plot
ggplot(mtcars, aes(wt, mpg, color = pc)) +
  geom_point(shape = 16, size = 5, alpha = 0.4, show.legend = FALSE) +
  theme_minimal() + 
  scale_color_gradient(low = "#0091ff", high = "#f0650e") +
  xlab('Weight (1,000 lbs)') +
  ylab('MPG') +
  ggtitle('Weight Vs. MPG')

Python Plot:

{python, fig.show = 'hold', out.width = '50%'}
#| label: fig-python
#| fig-cap: "Car Weight Vs. MPG Scatterplot (Python)"
#| layout-ncol: 2
#| column: page
import numpy as np
import pandas as pd
import seaborn as sns
import statsmodels.api as sm

mtcars = sm.datasets.get_rdataset('mtcars', 'datasets', cache = True).data
sns.scatterplot(x = "wt", 
                y = "mpg",
                data = mtcars).set_title('Weight Vs. MPG')
                
sns.regplot(x = mtcars['wt'], y = mtcars['mpg'], 
            fit_reg = False)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM