Faire un triangle de Pascal coloré en Python est un bon moyen de s’occuper alors que les vacances sont là. Je vais vous présenter plusieurs designs, mon préféré allant au dernier.
Alors oui, je sais, j’avais déjà proposé une méthode pour construire le triangle de Pascal dans ce post. Mais j’avais envie d’explorer de nouvelles contrées informatiques. Je ne voulais pas passer par LATEX.
Un triangle de Pascal coloré en Python: les prémices
Faire un triangle de Pascal en Python, c’est pas trop compliqué. Mais rendre le résultat attrayant, joli, et donc en mode graphique, c’est mieux… Il y a plusieurs techniques:
pygame (pas génial à mon goût),
tkinter (beurk! pour si peu ?)
via LATEX et TiKZ (mouais… mais bon! je veux éviter justement!)
matplotlib… Ouais!
On va partir sur matplotlib!
Le cahier des charges est simple:
un triangle de Pascal centré ou pas
des couleurs de fond pour chaque « cases »
des couleurs en fonction de la position du nombre par rapport au centre des lignes
une sauvegarde en haute définition de l’image.
Je précise que je ne vais pas définir ce qu’est le triangle de Pascal: z’avez cas regarder sur wikipedia.
Un triangle de Pascal coloré en Python: les différentes propositions
Je vais vous présenter six scripts.
Un triangle de Pascal coloré en Python: script 1
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
defpascal_triangle(n):
"""Generate the first n rows of Pascal's triangle."""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
def pascal_triangle(n):
"""Generate the first n rows of Pascal's triangle."""
triangle = np.zeros((n, n), dtype=int)
for i in range(n):
triangle[i, 0] = 1
for j in range(1, i + 1):
triangle[i, j] = triangle[i - 1, j - 1] + triangle[i - 1, j]
return triangle
def plot_pascal_triangle(n):
"""Plot the first n rows of Pascal's triangle with color."""
triangle = pascal_triangle(n)
fig, ax = plt.subplots(figsize=(10, 10))
ax.axis('off')
# Define a color map
colors = plt.cm.viridis(np.linspace(0, 1, n))
for i in range(n):
for j in range(i + 1):
x = i
y = -i / 2 + j
color = colors[i]
intensity = 1 - abs(j - i / 2) / (i / 2 + 1)
ax.text(y, -x, triangle[i, j], ha='center', va='center',
bbox=dict(facecolor=color * intensity, edgecolor='none', boxstyle='round,pad=0.3'))
plt.xlim(-n/2, n/2)
plt.ylim(-n, 1)
plt.gca().set_aspect('equal', adjustable='box')
plt.savefig('triangle_de_pascal.png', dpi=300, bbox_inches='tight')
plt.show()
plot_pascal_triangle(10)
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
def pascal_triangle(n):
"""Generate the first n rows of Pascal's triangle."""
triangle = np.zeros((n, n), dtype=int)
for i in range(n):
triangle[i, 0] = 1
for j in range(1, i + 1):
triangle[i, j] = triangle[i - 1, j - 1] + triangle[i - 1, j]
return triangle
def plot_pascal_triangle(n):
"""Plot the first n rows of Pascal's triangle with color."""
triangle = pascal_triangle(n)
fig, ax = plt.subplots(figsize=(10, 10))
ax.axis('off')
# Define a color map
colors = plt.cm.viridis(np.linspace(0, 1, n))
for i in range(n):
for j in range(i + 1):
x = i
y = -i / 2 + j
color = colors[i]
intensity = 1 - abs(j - i / 2) / (i / 2 + 1)
ax.text(y, -x, triangle[i, j], ha='center', va='center',
bbox=dict(facecolor=color * intensity, edgecolor='none', boxstyle='round,pad=0.3'))
plt.xlim(-n/2, n/2)
plt.ylim(-n, 1)
plt.gca().set_aspect('equal', adjustable='box')
plt.savefig('triangle_de_pascal.png', dpi=300, bbox_inches='tight')
plt.show()
plot_pascal_triangle(10)
Bon, clairement, c’est joli mais pas ouf… On peut améliorer!
Script 2: cases carrées, triangle aligné à gauche
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import numpy as np
import matplotlib.pyplot as plt
defpascal_triangle(n):
"""Generate the first n rows of Pascal's triangle."""
import numpy as np
import matplotlib.pyplot as plt
def pascal_triangle(n):
"""Generate the first n rows of Pascal's triangle."""
triangle = np.zeros((n, n), dtype=int)
for i in range(n):
triangle[i, 0] = 1
for j in range(1, i + 1):
triangle[i, j] = triangle[i - 1, j - 1] + triangle[i - 1, j]
return triangle
def plot_pascal_triangle(n):
"""Plot the first n rows of Pascal's triangle with color."""
triangle = pascal_triangle(n)
fig, ax = plt.subplots(figsize=(10, 10))
ax.axis('off')
# Define a color map
colors = plt.cm.viridis(np.linspace(0, 1, n))
max_width = max([len(str(triangle[i, j])) for i in range(n) for j in range(i + 1)])
for i in range(n):
for j in range(i + 1):
x = j
y = -i
color = colors[i]
intensity = 1 - abs(j - i / 2) / (i / 2 + 1)
rect = plt.Rectangle((x - 0.5, y - 0.5), 1, 1, facecolor=color * intensity, edgecolor='none')
ax.add_patch(rect)
ax.text(x, y, triangle[i, j], ha='center', va='center', fontsize=10)
plt.xlim(-0.5, n - 0.5)
plt.ylim(-n, 0.5)
plt.gca().set_aspect('equal', adjustable='box')
plt.savefig('triangle_de_pascal.png', dpi=300, bbox_inches='tight')
plt.show()
plot_pascal_triangle(10)
import numpy as np
import matplotlib.pyplot as plt
def pascal_triangle(n):
"""Generate the first n rows of Pascal's triangle."""
triangle = np.zeros((n, n), dtype=int)
for i in range(n):
triangle[i, 0] = 1
for j in range(1, i + 1):
triangle[i, j] = triangle[i - 1, j - 1] + triangle[i - 1, j]
return triangle
def plot_pascal_triangle(n):
"""Plot the first n rows of Pascal's triangle with color."""
triangle = pascal_triangle(n)
fig, ax = plt.subplots(figsize=(10, 10))
ax.axis('off')
# Define a color map
colors = plt.cm.viridis(np.linspace(0, 1, n))
max_width = max([len(str(triangle[i, j])) for i in range(n) for j in range(i + 1)])
for i in range(n):
for j in range(i + 1):
x = j
y = -i
color = colors[i]
intensity = 1 - abs(j - i / 2) / (i / 2 + 1)
rect = plt.Rectangle((x - 0.5, y - 0.5), 1, 1, facecolor=color * intensity, edgecolor='none')
ax.add_patch(rect)
ax.text(x, y, triangle[i, j], ha='center', va='center', fontsize=10)
plt.xlim(-0.5, n - 0.5)
plt.ylim(-n, 0.5)
plt.gca().set_aspect('equal', adjustable='box')
plt.savefig('triangle_de_pascal.png', dpi=300, bbox_inches='tight')
plt.show()
plot_pascal_triangle(10)
Cela commence à avoir de la gueule… Mais j’aime bien quand c’est centré!
Script 3: cases carrées, triangle centré
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import numpy as np
import matplotlib.pyplot as plt
defpascal_triangle(n):
"""Generate the first n rows of Pascal's triangle."""
import numpy as np
import matplotlib.pyplot as plt
def pascal_triangle(n):
"""Generate the first n rows of Pascal's triangle."""
triangle = np.zeros((n, n), dtype=int)
for i in range(n):
triangle[i, 0] = 1
for j in range(1, i + 1):
triangle[i, j] = triangle[i - 1, j - 1] + triangle[i - 1, j]
return triangle
def plot_pascal_triangle(n):
"""Plot the first n rows of Pascal's triangle with color."""
triangle = pascal_triangle(n)
fig, ax = plt.subplots(figsize=(10, 10))
ax.axis('off')
# Define a color map
colors = plt.cm.viridis(np.linspace(0, 1, n))
for i in range(n):
for j in range(i + 1):
x = j - i / 2
y = -i
color = colors[i]
intensity = 1 - abs(j - i / 2) / (i / 2 + 1)
rect = plt.Rectangle((x - 0.5, y - 0.5), 1, 1, facecolor=color * intensity, edgecolor='none')
ax.add_patch(rect)
ax.text(x, y, triangle[i, j], ha='center', va='center', fontsize=10)
plt.xlim(-n/2 - 0.5, n/2 + 0.5)
plt.ylim(-n, 0.5)
plt.gca().set_aspect('equal', adjustable='box')
plt.savefig('triangle_de_pascal.png', dpi=300, bbox_inches='tight')
plt.show()
plot_pascal_triangle(10)
import numpy as np
import matplotlib.pyplot as plt
def pascal_triangle(n):
"""Generate the first n rows of Pascal's triangle."""
triangle = np.zeros((n, n), dtype=int)
for i in range(n):
triangle[i, 0] = 1
for j in range(1, i + 1):
triangle[i, j] = triangle[i - 1, j - 1] + triangle[i - 1, j]
return triangle
def plot_pascal_triangle(n):
"""Plot the first n rows of Pascal's triangle with color."""
triangle = pascal_triangle(n)
fig, ax = plt.subplots(figsize=(10, 10))
ax.axis('off')
# Define a color map
colors = plt.cm.viridis(np.linspace(0, 1, n))
for i in range(n):
for j in range(i + 1):
x = j - i / 2
y = -i
color = colors[i]
intensity = 1 - abs(j - i / 2) / (i / 2 + 1)
rect = plt.Rectangle((x - 0.5, y - 0.5), 1, 1, facecolor=color * intensity, edgecolor='none')
ax.add_patch(rect)
ax.text(x, y, triangle[i, j], ha='center', va='center', fontsize=10)
plt.xlim(-n/2 - 0.5, n/2 + 0.5)
plt.ylim(-n, 0.5)
plt.gca().set_aspect('equal', adjustable='box')
plt.savefig('triangle_de_pascal.png', dpi=300, bbox_inches='tight')
plt.show()
plot_pascal_triangle(10)
Voilà! C’est centré, mais peut-être trop « carré » ! Non ?
Script 4: cases avec coins arrondis, cases qui se chevauchent
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import FancyBboxPatch
defpascal_triangle(n):
"""Generate the first n rows of Pascal's triangle."""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import FancyBboxPatch
def pascal_triangle(n):
"""Generate the first n rows of Pascal's triangle."""
triangle = np.zeros((n, n), dtype=int)
for i in range(n):
triangle[i, 0] = 1
for j in range(1, i + 1):
triangle[i, j] = triangle[i - 1, j - 1] + triangle[i - 1, j]
return triangle
def plot_pascal_triangle(n):
"""Plot the first n rows of Pascal's triangle with color."""
triangle = pascal_triangle(n)
fig, ax = plt.subplots(figsize=(10, 10))
ax.axis('off')
# Define a color map
colors = plt.cm.viridis(np.linspace(0, 1, n))
for i in range(n):
for j in range(i + 1):
x = j - i / 2
y = -i
color = colors[i]
intensity = 1 - abs(j - i / 2) / (i / 2 + 1)
rect = FancyBboxPatch((x - 0.5, y - 0.5), 1, 1,
boxstyle="round,pad=0.1,rounding_size=0.2",
facecolor=color * intensity, edgecolor='none')
ax.add_patch(rect)
ax.text(x, y, triangle[i, j], ha='center', va='center', fontsize=10, color='white')
plt.xlim(-n/2 - 0.5, n/2 + 0.5)
plt.ylim(-n, 0.5)
plt.gca().set_aspect('equal', adjustable='box')
plt.savefig('triangle_de_pascal.png', dpi=300, bbox_inches='tight')
plt.show()
plot_pascal_triangle(10)
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import FancyBboxPatch
def pascal_triangle(n):
"""Generate the first n rows of Pascal's triangle."""
triangle = np.zeros((n, n), dtype=int)
for i in range(n):
triangle[i, 0] = 1
for j in range(1, i + 1):
triangle[i, j] = triangle[i - 1, j - 1] + triangle[i - 1, j]
return triangle
def plot_pascal_triangle(n):
"""Plot the first n rows of Pascal's triangle with color."""
triangle = pascal_triangle(n)
fig, ax = plt.subplots(figsize=(10, 10))
ax.axis('off')
# Define a color map
colors = plt.cm.viridis(np.linspace(0, 1, n))
for i in range(n):
for j in range(i + 1):
x = j - i / 2
y = -i
color = colors[i]
intensity = 1 - abs(j - i / 2) / (i / 2 + 1)
rect = FancyBboxPatch((x - 0.5, y - 0.5), 1, 1,
boxstyle="round,pad=0.1,rounding_size=0.2",
facecolor=color * intensity, edgecolor='none')
ax.add_patch(rect)
ax.text(x, y, triangle[i, j], ha='center', va='center', fontsize=10, color='white')
plt.xlim(-n/2 - 0.5, n/2 + 0.5)
plt.ylim(-n, 0.5)
plt.gca().set_aspect('equal', adjustable='box')
plt.savefig('triangle_de_pascal.png', dpi=300, bbox_inches='tight')
plt.show()
plot_pascal_triangle(10)
Script 5: cases à coins arrondis, nombres qui s’adaptent
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import FancyBboxPatch
defpascal_triangle(n):
"""Generate the first n rows of Pascal's triangle."""
Se préparer à l’épreuve du bac 2025 en mathématiques peut passer par tenter de faire des annales (il y a des annales corrigées sur le site de l’APMEP). Cela peut aussi passer par tenter de Lire la suite…
Je vous présente ici une méthode de chiffrement personnel d’images avec Python. Pour se faire, nous allons prendre une image quelconque, par exemple celle-ci:
Le jeu de la moyenne est inspiré d’un épisode de la saison 2 de la série « Alice in Borderland », celui du roi de carreau. Ce jeu se déroule dans le bâtiment de la Cour suprême Lire la suite…
0
0
Votre panier
Votre panier est vide
wpDiscuz
0
0
Nous aimerions avoir votre avis, veuillez laisser un commentaire.x