Python 如何创建具有最佳打印分辨率的图形?

Python 如何创建具有最佳打印分辨率的图形?,python,matplotlib,figure,Python,Matplotlib,Figure,我很喜欢,想把它印在画布上 我可以用python创建绘图,但需要一些帮助来准备图形属性,以便它具有合适的分辨率进行打印。我的代码现在产生了一些锯齿线 这是我的密码: 将numpy作为np导入 将matplotlib.pyplot作为plt导入 # overall image properties width, height, dpi = 2560, 1440, 96 picture_background = 'white' aspect_ratio = width / height plt.

我很喜欢,想把它印在画布上

我可以用python创建绘图,但需要一些帮助来准备图形属性,以便它具有合适的分辨率进行打印。我的代码现在产生了一些锯齿线

这是我的密码:

将numpy作为np导入 将matplotlib.pyplot作为plt导入

# overall image properties
width, height, dpi = 2560, 1440, 96
picture_background = 'white'
aspect_ratio = width / height


plt.close('all')
R = np.linspace(3.5,4,5001)

fig = plt.figure(figsize=(width / dpi, height / dpi), frameon=False)
ylim = -0.1,1.1
ax = plt.Axes(fig, [0, 0, 1, 1], xlim = (3.4,4))
ax.set_axis_off()
fig.add_axes(ax)

for r in R:

    x = np.zeros(5001)

    x[0] = 0.1

    for i in range(1,len(x)):

        x[i] = r*x[i-1]*(1-x[i-1])


    ax.plot(r*np.ones(2500),x[-2500:],marker = '.', markersize= 0.01,color = 'grey', linestyle = 'none')

plt.show()  
plt.savefig('figure.eps', dpi=dpi, bbox_inches=0, pad_inches=0, facecolor=picture_background)
下面是代码生成的内容:

如您所见,图中最左边的一些线条相当参差不齐


如何创建此图形,使分辨率适合在各种帧尺寸上打印?

我认为锯齿的来源是基本像素大小+您正在使用非常小的“点”标记绘制此图形。线条经过的像素已经完全饱和,所以你会看到“锯齿状”

绘制此数据的更好方法是提前进行装箱,然后让mpl绘制热图:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
plt.ion()

width, height, dpi = 2560 / 2, 1440 / 2, 96  # cut to so SO will let me upload result

aspect_ratio = width / height

fig, ax = plt.subplots(figsize=(width / dpi, height / dpi), frameon=False,
                       tight_layout=True)
ylim = -0.1, 1.1
ax.axis('off')


# make heatmap at double resolution
accumulator = np.zeros((height, width), dtype=np.uint64)

burn_in_count = 25000
N = 25000

R = np.linspace(3.5, 4, width)
x = 0.1 * np.ones_like(R)
row_indx = np.arange(len(R), dtype='uint')
# do all of the r values in parallel
for j in range(burn_in_count):
    x = R * x * (1 - x)

for j in range(N):
    x = R * x * (1 - x)
    col_indx = (height * x).astype('int')
    accumulator[col_indx, row_indx] += 1

im = ax.imshow(accumulator, cmap='gray_r',
               norm=mcolors.LogNorm(), interpolation='none')

请注意,这是对数缩放的,如果您只想查看命中的像素数

使用

但这些仍然存在锯齿状的问题,有时(可能更糟的是)窄线会被混淆

这是一类问题,或旨在通过在绘图时以智能方式重新组合数据来解决()。这两个都是原型/概念验证,但都是可用的


在zoom上重新计算Mandelbrot集也可能会引起您的兴趣

我认为锯齿的来源是潜在的像素大小+您正在使用非常小的“点”标记绘制它。线条经过的像素已经完全饱和,所以你会看到“锯齿状”

绘制此数据的更好方法是提前进行装箱,然后让mpl绘制热图:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
plt.ion()

width, height, dpi = 2560 / 2, 1440 / 2, 96  # cut to so SO will let me upload result

aspect_ratio = width / height

fig, ax = plt.subplots(figsize=(width / dpi, height / dpi), frameon=False,
                       tight_layout=True)
ylim = -0.1, 1.1
ax.axis('off')


# make heatmap at double resolution
accumulator = np.zeros((height, width), dtype=np.uint64)

burn_in_count = 25000
N = 25000

R = np.linspace(3.5, 4, width)
x = 0.1 * np.ones_like(R)
row_indx = np.arange(len(R), dtype='uint')
# do all of the r values in parallel
for j in range(burn_in_count):
    x = R * x * (1 - x)

for j in range(N):
    x = R * x * (1 - x)
    col_indx = (height * x).astype('int')
    accumulator[col_indx, row_indx] += 1

im = ax.imshow(accumulator, cmap='gray_r',
               norm=mcolors.LogNorm(), interpolation='none')

请注意,这是对数缩放的,如果您只想查看命中的像素数

使用

但这些仍然存在锯齿状的问题,有时(可能更糟的是)窄线会被混淆

这是一类问题,或旨在通过在绘图时以智能方式重新组合数据来解决()。这两个都是原型/概念验证,但都是可用的

在zoom上重新计算Mandelbrot集也可能会引起您的兴趣