Marcelliniโ€™s Blog
  • Home
  • ๐Ÿงญ Explore
    • ๐Ÿท๏ธ Tags
    • ๐Ÿ“‚ Categories
  • ๐Ÿง  Exact Sciences
    • ๐Ÿงฎ Mathematics
    • ๐Ÿ“Š Statistics
    • ๐Ÿ”ญ Physics
    • ๐Ÿ’ป Programming
  • ๐Ÿ“ Personal Blog
    • ๐Ÿ“ Personal Blog
    • ๐Ÿ‘ค About Me and the Blog
  • ๐Ÿ“˜ Courses
    • ๐Ÿงฎ Math Courses
    • ๐Ÿ“Š Statistics Courses
  • ๐Ÿ—บ๏ธ Site Map
  • PT ๐Ÿ‡ง๐Ÿ‡ท
  • Contact

On this page

  • 1 ๐Ÿ“˜ ๐Ÿ‘จโ€๐Ÿ’ป ๐Ÿ“„ ๐Ÿ Python Code Templates for Use with Quarto
    • 1.1 Introduction
    • 1.2 Simple Python code block
    • 1.3 Plot with matplotlib
    • 1.4 Using numpy and seaborn
    • 1.5 Analysis with pandas
    • 1.6 Descriptive statistics with pandas
    • 1.7 Scatter plot
    • 1.8 Linear regression with scikit-learn
    • 1.9 Plot with regression line
    • 1.10 Formatted table with tabulate
    • 1.11 Conclusion
  • 2 ๐Ÿ”— Useful Links

๐Ÿ“˜ ๐Ÿ‘จโ€๐Ÿ’ป ๐Ÿ“„ ๐Ÿ Python Code Templates for Use with Quarto

programming
Python
Quarto
article
Quarto allows the direct execution of Python code blocks.
Author

Blog do Marcellini

Published

June 29, 2025


โ† Back to the Programming Reading Guide ๐Ÿ‘จโ€๐Ÿ’ป

โ† Back to the Python Section ๐Ÿ

โ† Back to the Programming Section ๐Ÿ‘จโ€๐Ÿ’ป


Python

1 ๐Ÿ“˜ ๐Ÿ‘จโ€๐Ÿ’ป ๐Ÿ“„ ๐Ÿ Python Code Templates for Use with Quarto

1.1 Introduction

Quarto allows the direct execution of Python code blocks in .qmd files, making it possible to create interactive and reproducible reports with analyses, plots, and visualizations in Python.

The snippet below is the YAML header of the .qmd document, which defines the title, author, date, output format, and code execution options:

---
title: "Python Code Templates for Use with Quarto"
author: "Blog do Marcellini"
date: 2025-06-23
format: html
editor: visual
lang: en
execute:
  echo: true
  warning: false
  message: false
---

1.2 Simple Python code block

x = list(range(1, 11))
y = [i**2 for i in x]
y
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

1.3 Plot with matplotlib

import matplotlib.pyplot as plt

plt.plot(x, y, marker='o', color='blue')
plt.title("Plot of xยฒ")
plt.xlabel("x")
plt.ylabel("y = xยฒ")
plt.grid(True)
plt.show()


1.4 Using numpy and seaborn

import numpy as np
import seaborn as sns

data = np.random.normal(size=1000)
sns.histplot(data, kde=True)


1.5 Analysis with pandas

import pandas as pd

df = pd.read_csv("data/iris.csv")
df.head()
sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa

1.6 Descriptive statistics with pandas

df.describe()
sepal_length sepal_width petal_length petal_width
count 150.000000 150.000000 150.000000 150.000000
mean 5.843333 3.057333 3.758000 1.198667
std 0.828066 0.435866 1.765298 0.763161
min 4.300000 2.000000 1.000000 0.100000
25% 5.100000 2.800000 1.600000 0.300000
50% 5.800000 3.000000 4.350000 1.300000
75% 6.400000 3.300000 5.100000 1.800000
max 7.900000 4.400000 6.900000 2.500000

1.7 Scatter plot

sns.scatterplot(data=df, x="sepal_length", y="petal_length", hue="species")


1.8 Linear regression with scikit-learn

from sklearn.linear_model import LinearRegression

X = df[["sepal_length"]]
y = df["petal_length"]

model = LinearRegression()
model.fit(X, y)

print(f"Slope: {model.coef_[0]:.2f}")
print(f"Intercept: {model.intercept_:.2f}")
Slope: 1.86
Intercept: -7.10

1.9 Plot with regression line

plt.scatter(X, y, color='gray')
plt.plot(X, model.predict(X), color='red')
plt.xlabel("Sepal Length")
plt.ylabel("Petal Length")
plt.title("Simple Linear Regression")
plt.show()


1.10 Formatted table with tabulate

from tabulate import tabulate

table = df.head(5)
print(tabulate(table, headers='keys', tablefmt='github'))
|    |   sepal_length |   sepal_width |   petal_length |   petal_width | species   |
|----|----------------|---------------|----------------|---------------|-----------|
|  0 |            5.1 |           3.5 |            1.4 |           0.2 | setosa    |
|  1 |            4.9 |           3   |            1.4 |           0.2 | setosa    |
|  2 |            4.7 |           3.2 |            1.3 |           0.2 | setosa    |
|  3 |            4.6 |           3.1 |            1.5 |           0.2 | setosa    |
|  4 |            5   |           3.6 |            1.4 |           0.2 | setosa    |

1.11 Conclusion

With Quarto and Python, it is possible to generate reports and documents rich in visual and analytical content, integrating text, code, plots, and results in an automated and elegant way.


โ† Back to the Programming Reading Guide ๐Ÿ‘จโ€๐Ÿ’ป

โ† Back to the Python Section ๐Ÿ

โ† Back to the Programming Section ๐Ÿ‘จโ€๐Ÿ’ป


๐Ÿ” Back to Top


Blog do Marcellini โ€” Exploring Mathematics, Statistics, and Physics with Rigor and Beauty.

Note

Created by Blog do Marcellini with โค๏ธ and code.

2 ๐Ÿ”— Useful Links

  • ๐Ÿง‘โ€๐Ÿซ About the Blog
  • ๐Ÿ’ป Project GitHub
  • ๐Ÿ“ฌ Contact via Email

ยฉ 2025 - Marcelliniโ€™s Blog

 

๐Ÿ“ฌ Contact via Email
๐Ÿ’ป GitHub Repository