Add source example files
This commit is contained in:
BIN
source/plotting/charts/bar-chart.png
Normal file
BIN
source/plotting/charts/bar-chart.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 10 KiB |
21
source/plotting/charts/bar_chart.py
Normal file
21
source/plotting/charts/bar_chart.py
Normal file
@ -0,0 +1,21 @@
|
||||
from typing import Iterable
|
||||
|
||||
from matplotlib import pyplot as plot
|
||||
from matplotlib.axes import Axes
|
||||
from matplotlib.figure import Figure
|
||||
import matplotlib
|
||||
|
||||
if __name__ == '__main__':
|
||||
matplotlib.use("QtCairo")
|
||||
labels: Iterable[str] = ("Janvier", "Février", "Mars", "Avril")
|
||||
values: Iterable[float] = (50, 11.4, 91, 66.1)
|
||||
figure: Figure
|
||||
axis: Axes
|
||||
figure, axis = plot.subplots(nrows=1, ncols=1)
|
||||
axis.bar(x=labels, height=values, color="#00ff00")
|
||||
circle = plot.Circle((2, 20), 2, color="#FF0000", fill=False)
|
||||
axis.set_ylabel("Précipitations (mm)")
|
||||
axis.set_xlabel("Mois")
|
||||
axis.set_title("Précipitations pour 2022")
|
||||
axis.add_artist(circle)
|
||||
plot.show()
|
19
source/plotting/charts/bar_chart_save.py
Normal file
19
source/plotting/charts/bar_chart_save.py
Normal file
@ -0,0 +1,19 @@
|
||||
from typing import Iterable
|
||||
|
||||
from matplotlib import pyplot as plot
|
||||
from matplotlib.axes import Axes
|
||||
from matplotlib.figure import Figure
|
||||
import matplotlib
|
||||
|
||||
if __name__ == '__main__':
|
||||
matplotlib.use("QtCairo")
|
||||
labels: Iterable[str] = ("Janvier", "Février", "Mars", "Avril")
|
||||
values: Iterable[float] = (50, 11.4, 91, 66.1)
|
||||
figure: Figure
|
||||
axis: Axes
|
||||
figure, axis = plot.subplots(nrows=1, ncols=1)
|
||||
axis.bar(x=labels, height=values, color="#00ff00")
|
||||
axis.set_ylabel("Précipitations (mm)")
|
||||
axis.set_xlabel("Mois")
|
||||
axis.set_title("Précipitations pour 2022")
|
||||
figure.savefig("bar-chart.png", transparent=True)
|
24
source/plotting/charts/pie_chart.py
Normal file
24
source/plotting/charts/pie_chart.py
Normal file
@ -0,0 +1,24 @@
|
||||
"""
|
||||
Display sales of various cities in a sunburst chart.
|
||||
|
||||
Given the cities can have recurring parents, we can have
|
||||
a sunburst chart with 2 rings, the centermost ring for the
|
||||
country, and the outmost ring to have sales per city.
|
||||
"""
|
||||
import matplotlib
|
||||
import pandas as pd
|
||||
from matplotlib import pyplot
|
||||
|
||||
if __name__ == '__main__':
|
||||
matplotlib.use("QtCairo")
|
||||
df = pd.DataFrame(data={
|
||||
"country": ["France", "France", "Spain", "Spain"],
|
||||
"city": ["Montpellier", "Bordeaux", "Madrid", "Valencia"],
|
||||
"sales": [150_000, 127_000, 97_200, 137_250]
|
||||
})
|
||||
df.set_index(["country", "city"], inplace=True)
|
||||
total: int = df["sales"].sum()
|
||||
print(df)
|
||||
axes = df.plot.pie(subplots=True, autopct=lambda x: f"{x * total / 100:.0f} {x:.2f}%")
|
||||
pyplot.show()
|
||||
|
8
source/plotting/charts/plotly_bar.py
Normal file
8
source/plotting/charts/plotly_bar.py
Normal file
@ -0,0 +1,8 @@
|
||||
import pandas as pd
|
||||
from plotly.express import bar
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
df = pd.DataFrame(data={"label": ["Citron", "Pomme", "Mangue"], "price": [1.99, 3.97, 6.8]})
|
||||
plot = bar(df, x="label", y="price")
|
||||
plot.show()
|
11
source/plotting/charts/plotly_iris_scatter.py
Normal file
11
source/plotting/charts/plotly_iris_scatter.py
Normal file
@ -0,0 +1,11 @@
|
||||
import pandas as pd
|
||||
from plotly import express as px
|
||||
|
||||
data = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data", header=None,
|
||||
names=["sepal-length", "sepal-width", "petal-length", "petal-width", "class"])
|
||||
|
||||
plot = px.scatter(data, x="sepal-length", y="sepal-width", size="petal-width", color="class", template="seaborn",
|
||||
title="Iris flowers dataset",
|
||||
labels={"sepal-length": "Sepal length", "sepal-width": "Sepal width", "petal-width": "Petal width", "class": "Class"})
|
||||
plot.layout.update({"font": {"family": "Cabin", "size": 13}})
|
||||
plot.show()
|
21
source/plotting/charts/plotly_sunburst.py
Normal file
21
source/plotting/charts/plotly_sunburst.py
Normal file
@ -0,0 +1,21 @@
|
||||
"""
|
||||
Display sales of various cities in a sunburst chart.
|
||||
|
||||
Given the cities can have recurring parents, we can have
|
||||
a sunburst chart with 2 rings, the centermost ring for the
|
||||
country, and the outmost ring to have sales per city.
|
||||
"""
|
||||
import pandas as pd
|
||||
import plotly
|
||||
from plotly.express import sunburst
|
||||
|
||||
if __name__ == '__main__':
|
||||
df = pd.DataFrame(data={
|
||||
"country": ["France", "France", "Spain", "Spain", "England", "England", "England"],
|
||||
"city": ["Montpellier", "Bordeaux", "Madrid", "Valencia", "London", "Manchester", "Bristol"],
|
||||
"sales": [150_000, 127_000, 97_200, 137_250, 200_000, 180_000, 150_000]
|
||||
})
|
||||
plot = sunburst(df, path=["country", "city"], values="sales", title="Sales by country and city", template="ggplot2",
|
||||
color_discrete_sequence=plotly.colors.qualitative.Dark2)
|
||||
plot.layout.update({"font": {"family": "Cabin", "size": 13}})
|
||||
plot.show()
|
Reference in New Issue
Block a user