The functions used in the previous post generate an array of integers, the number of iterations for the function return value to exceed 2. This post looks in more detail at how this data can be plotted in Excel using the Matplotlib imshow function and pyxll.
Matplotlib.imshow documentation
First the Mandelbrot data is generated using the fastest of the functions described in the previous post, then new Matplotlib fig and ax objects are created:
@xl_func
@xl_arg('width', 'int')
@xl_arg('height', 'int')
@xl_arg('maxiter', 'int')
def mandelbrot_image(xmin,xmax,ymin,ymax,width=3,height=3,maxiter=80,cmap='hot', normval=0.3, show_ticks = True):
dpi = 72
img_width = dpi * width
img_height = dpi * height
x,y,z = mandelbrot_set6(xmin,xmax,ymin,ymax,img_width,img_height,maxiter)
fig, ax = plt.subplots(figsize=(width, height),dpi=72)
Axis “tick” values are created at the required spacing and precision, or alternatively are set not to display:
if show_ticks:
ticks = np.arange(0,img_width,3*dpi)
x_ticks = np.round(xmin + (xmax-xmin)*ticks/img_width,4)
plt.xticks(ticks, x_ticks)
y_ticks = np.round(ymin + (ymax-ymin)*ticks/img_width, 4)
plt.yticks(ticks, y_ticks)
else:
plt.xticks([])
plt.yticks([])
The imshow function requires cmap and norm arguments:
cmap str or Colormap
, default: rcParams["image.cmap"]
(default: 'viridis'
): The Colormap instance or registered colormap name used to map scalar data to colors.
norm Normalize
, optional: The Normalize
instance used to scale scalar data to the [0, 1] range before mapping to colors using cmap. By default, a linear scaling mapping the lowest value to 0 and the highest to 1 is used.
Norm is set using colors.PowerNorm:
Linearly map a given value to the 0-1 range and then apply a power-law normalization over that range.
ax.imshow is used to generate the image, then pyxll.plot to copy the image to Excel:
norm = colors.PowerNorm(normval)
ax.imshow(z.T,cmap=cmap,origin='lower',norm=norm)
pyxll.plot(fig)
The animations below step through all 166 of the “cmap” values with three different “norm” values (double click any image to display full screen):
Norm = 0.3:
Norm = 0.2:
Norm = 0.25: