Skip to content

Instantly share code, notes, and snippets.

@cast42
Last active August 17, 2023 12:40
Show Gist options
  • Save cast42/4ed85e8320e2c7aec80f9b6a7b81e465 to your computer and use it in GitHub Desktop.
Save cast42/4ed85e8320e2c7aec80f9b6a7b81e465 to your computer and use it in GitHub Desktop.
Simone Conradi Chaos attractor in matplotlib
# Code from https://twitter.com/S_Conradi/status/1595324702008000512/photo/1
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# %config InlineBackend.figure_format = "retina"
def list2f(ifs):
"""From a list of x and y arrays coefficients to f1 and f2"""
it1 = ifs[0]
it2 = ifs[1]
def f1(x, y):
return (
it1[0]
+ it1[1] * x
+ it1[2] * x**2
+ it1[3] * x * y
+ it1[4] * y
+ it1[5] * y**2
)
def f2(x, y):
return (
it2[0]
+ it2[1] * x
+ it2[2] * x**2
+ it2[3] * x * y
+ it2[4] * y
+ it2[5] * y**2
)
return f1, f2
# list of x and y arrays coefficients defintng the IFS
ifs = [
np.array([-0.28752426, 0.65608465, 0.71259527, 1.34370624, 1.01724109, 0.19113889]),
np.array(
[-1.06839961, 0.29822047, 0.35672293, -0.68326573, 0.68020521, 1.18480771]
),
]
fx, fy = list2f(ifs) # getting functions
n_points = 1_000_000
points = np.zeros((n_points, 2), dtype=np.float32) # points of the ifs
x, y = 0.05, 0.05
points[0, 0], points[0, 1] = x, y
for i in range(1, n_points):
x, y = fx(x, y), fy(x, y) # iteration of the ifs
points[i, 0], points[i, 1] = x, y
# plotting
fig, ax = plt.subplots(figsize=(15, 15))
fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None)
fig.set_facecolor("lightgray")
ax.set_axis_off()
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.spines["left"].set_visible(False)
ax.spines["bottom"].set_visible(False)
ax.set_aspect("equal", "box")
ax.scatter(points[:, 0], points[:, 1], s=0.001, alpha=0.3, c="black")
plt.show()
plt.close()
@fomightez
Copy link

fomightez commented Nov 23, 2022

If you want to change the background color: try fig.set_facecolor("gray"), based on https://stackoverflow.com/a/44807740/8508004. That ax.set_facecolor("white") didn't seem to do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment