Matlotlib: How to pass the subplot-number to floating_axes.FloatingSubplot method?

Viewed 123

I have a 4x4 grid having 16 subplots. I am using floating_axes.FloatingSubplot method to rotate all the axes in my figure. How should I pass the subplot-number as it takes axes but as three digit number, see this. Now since I am having more than 9 subplots I dont know how to pass the subplot-number to the FloatingSubplot method.

Code:-

from mpl_toolkits.axisartist.grid_finder import DictFormatter
from matplotlib.transforms import Affine2D
import mpl_toolkits.axisartist.floating_axes as floating_axes
import matplotlib.pyplot as plt

def setup_axes1(fig, ax, angle):
    tr = Affine2D().scale(2, 2).rotate_deg(angle)

    grid_helper = floating_axes.GridHelperCurveLinear(
        tr, extremes=(0, 11, 0, 11)
    )

    ax1 = floating_axes.FloatingSubplot(fig, ax, grid_helper=grid_helper)
    ax1.axis("off")
    
    ax1.axis["bottom"].set_visible(False)
    ax1.axis["top"].set_visible(False)
    ax1.axis["right"].set_visible(False)
    ax1.axis["left"].set_visible(False)

    fig.add_subplot(ax1)
    
    aux_ax = ax1.get_aux_axes(tr)

    return aux_ax

fig, axes = plt.subplots(nrows=4, ncols=3, figsize=(16,30), facecolor="#222222")

for index, ax_ in enumerate(fig.get_axes(), start=1):
    
    fig.delaxes(ax_)
    
    ## HERE IS THE PROBLEM PASSING 430 + INDEX FAILS AT 430 + 10
    ax = setup_axes1(fig, 430 + index, -45)

    ax.set_aspect("equal")

    temp_count = 1

    start, end = 1, 10

    for i in range(start, end+1):
        count = 0
        for j in range(end-i+1, end+1):
            if temp_count <= 55:
                color, radius, hatch_num, lw, fill = "gold", 0.4, 0, 1, "gold"
            else:
                color, radius, hatch_num, lw, fill = "grey", 0.27, 5, 0, None

            count += 1

            circle = plt.Circle((count, j), radius=radius, linewidth=lw, fill=fill, hatch=hatch_num*'-', color=color)
            ax.add_artist(circle)

            temp_count += 1

    for i in range(start+1, end+1):
        count = i
        for j in range(start, end+1-i+1):
            if temp_count <= 55:
                color, radius, hatch_num, lw, fill = "gold", 0.4, 0, 1, "gold"
            else:
                color, radius, hatch_num, lw, fill = "#F2F2F2", 0.3, 3, 0, None

            circle = plt.Circle((count, j), radius=radius, linewidth=lw, fill=fill, hatch=hatch_num*'-', color=color)
            ax.add_artist(circle)

            count += 1
            temp_count += 1

# plt.show()

Output: enter image description here

Can someone help?

1 Answers

Not sure if you find a solution to this or not, but I faced the exact same issue today. I tried to pass the x, y, n values instead of rect, like I would have done in add_subfigure without success.

But I also discovered that the FloatingSubplot method is depreciated, and it was already when the initial post was posted. I think the solution is to use another method, but I couldn't figure out by what it has been replaced.

My solution was to force a 3x3 grid and make several figures... Ugly, but eh.

I do know this post does not really answer to the problem, but mentioning that this method is depreciated is most likely useful for anyone who will end up on this page in the future.

Related