我正在开发一个基于 matplotlib 散点图的名为samila的 Python 生成艺术生成器库。它有两个函数并将正方形空间映射为任意形状。我们希望生成的形状对于给定的函数和给定的随机种子是相同的,以便可重现。
最近我们正在研究具有复杂值的函数,并通知散点图输出在 matplotlib 的不同版本中不一样。
我想知道为什么会这样,matplotlib 有什么问题。如果这是一个错误,那么 matplotlib 在其不同版本中为特定代码绘制不同的数字可能会很糟糕。
因此,如果您使用以下代码运行matplotlib==3.4.3
:
from samila import *
import math
import matplotlib.pyplot as plt
def f1(x, y):
return math.cos(x**2 * y)**1.926 - math.floor(x - y)**1.861 - math.floor(y**2 * x)**1.688
def f2(x, y):
return x - y**1.617 - math.ceil(y)**1.477 - abs(x**2 * y) ** 1.647 - math.cos(x * y)**1.668
GI = GenerativeImage(f1, f2)
GI.generate(seed=755398)
GI.plot(color=(0.159, 0.085, 0.191), projection=Projection.POLAR, spot_size=2)
GI.save_image('art.png')
plt.show()
您将收到以下警告:
/home/user/.local/lib/python3.8/site-packages/numpy/core/_asarray.py:136: ComplexWarning: Casting complex values to real discards the imaginary part
return array(a, dtype, copy=False, order=order, subok=True)
如果您使用运行代码,matplotlib==3.0.3
您将拥有:
Attribute Qt::AA_EnableHighDpiScaling must be set before QCoreApplication is created.
/home/user/.local/lib/python3.8/site-packages/numpy/core/_asarray.py:136: ComplexWarning: Casting complex values to real discards the imaginary part
return array(a, dtype, copy=False, order=order, subok=True)
[编辑]:我添加了一个matplotlib
直接使用的示例,而不是通过 Samila 使用它。如果您愿意,可以使用此脚本代替以前的脚本。
import random
import math
import matplotlib.pyplot as plt
import itertools
def f1(x,y):
return math.cos(x**2 * y)**1.926 - math.floor(x - y)**1.861 - math.floor(y**2 * x)**1.688
def f2(x,y):
return x - y**1.617 - math.ceil(y)**1.477 - abs(x**2 * y) ** 1.647 - math.cos(x * y)**1.668
def float_range(start, stop, step):
while start < stop:
yield float(start)
start += step
data1 = []
data2 = []
range1 = list(float_range(-1*math.pi, math.pi, 0.01))
range_prod = list(itertools.product(range1, range1))
for item in range_prod:
data1.append(f1(item[0], item[1]))
data2.append(f2(item[0], item[1]))
color = (0.159, 0.085, 0.191)
spot_size = 0.01
projection = "polar"
fig = plt.figure()
fig.set_size_inches(10, 10)
ax = fig.add_subplot(111, projection=projection)
ax.scatter(
data2,
data1,
alpha=0.1,
edgecolors=color,
s=spot_size)
ax.set_axis_off()
ax.patch.set_zorder(-1)
ax.add_artist(ax.patch)
plt.show()
系统详情:
- 操作系统:Linux - Ubuntu 20.04
- Python:Python 3.8.10
- 海湾合作委员会:[海湾合作委员会 9.3.0]
- Matplotlib:[3.0.3,3.4.3]
- 麻木:1.19.1