我尝试使用块选项
fig.height
和 fig-height
设置图像大小,但图像大小不受影响。
---
title: 'image size'
author: 'Joseph Powers'
date: 2024-05-07
format:
html:
toc: true
toc-depth: 3
html-math-method: webtex
editor: source
fig.align: 'center'
dpi: 300
include: TRUE
echo: FALSE
message: FALSE
warning: FALSE
error: FALSE
cache: FALSE
code-fold: true
df-print: kable
---
```{python}
import pandas as pd
import numpy as np
from plotnine import *
from mizani.formatters import percent_format
from scipy import stats
my_blue = '#0177c9'
my_red = '#bd0707'
x = np.random.normal(.05, .025, 4000)
# Estimate pdf using kde
pdf = stats.gaussian_kde(x)
# Evaluate pdf at a set of points
points = np.linspace(min(x), max(x), 1000)
estimated_density = pdf.evaluate(points)
df_dens = pd.DataFrame({
'x': points,
'y': estimated_density
})
p = (ggplot(df_dens, aes('x', 'y')) +
geom_line() +
geom_area(data = df_dens[df_dens['x'] >= 0], fill = my_blue) +
geom_area(data = df_dens[df_dens['x'] <= 0], fill = my_red) +
theme_bw() +
theme(panel_grid=element_blank(), axis_text_y=element_blank(), axis_ticks_y=element_blank()) +
labs(y='', x="\nPlausible Treatment Effects") +
scale_x_continuous(
breaks=np.arange(-1, 1.01, 0.01),
labels=percent_format()
)
)
p
```
```{python}
#| out-width: '20%'
#| out-height: '20%'
p
```
```{python}
#| fig-height: 3
#| fig-width: 10
p
```
```{python, fig.height=1, fig.width=1}
#| fig-height: 1
#| fig-width: 1
p
```
如果我们查看 Quarto 文档中的 python cellsfigure chunk 选项列表,就会发现没有列出这样的
fig-height
、fig-width
选项。而且从之前的遭遇来看,我可以假设 fig-height
、fig-width
选项对于 python 单元不能按预期工作。
相反,我建议对特定绘图使用plotnine主题选项
theme(figure_size = (a, b))
,或对所有绘图使用默认选项plotnine.options.figure_size = (a, b)
。
---
title: 'image size'
author: 'Joseph Powers'
date: 2024-05-07
format:
html:
toc: true
toc-depth: 3
html-math-method: webtex
editor: source
fig.align: 'center'
dpi: 300
include: TRUE
echo: FALSE
message: FALSE
warning: FALSE
error: FALSE
cache: FALSE
code-fold: true
df-print: kable
---
```{python}
import pandas as pd
import numpy as np
from plotnine import *
from scipy import stats
my_blue = '#0177c9'
my_red = '#bd0707'
x = np.random.normal(.05, .025, 4000)
# Estimate pdf using kde
pdf = stats.gaussian_kde(x)
# Evaluate pdf at a set of points
points = np.linspace(min(x), max(x), 1000)
estimated_density = pdf.evaluate(points)
df_dens = pd.DataFrame({
'x': points,
'y': estimated_density
})
p = (ggplot(df_dens, aes('x', 'y')) +
geom_line() +
geom_area(data = df_dens[df_dens['x'] >= 0], fill = my_blue) +
geom_area(data = df_dens[df_dens['x'] <= 0], fill = my_red) +
theme_bw()
)
p + theme(figure_size=(2, 2)) # in inches
```