[Python] Remove frame on plt.figure; frame by initialising the subplots, but imgs in subplots...

Discussão em 'Python' iniciado por Stack, Março 16, 2025.

  1. Stack

    Stack Membro Participativo

    i am trying to build a figure in python using matplotlib.pyplot, containing 3 subplots in a row. The subplots should be returned by a seperate function and then be displayed on the axis in the figure. But however, i am having difficulties with the frames, that are beeing initially created by my Code.

    My problem with too many and wrong frames beeing displayed

    Moreover, the numbers beeing displayed on the x-axis are wrong on two of the images, on the right one its correct.The y-axis are somehow correct, but should reach into the neighbouring colorbar. The seperate function creates them correctly and i can verify using Jupyter, but my Code seems to ignore them. Somehow the colormaps and cbars are correct tho. The titles of y-axis and most of the x-axis are also lost, but the cbar-title still is displayed For some reason. Also, each one of these images should have a title. But most are lost.

    I tried almost everything, frameon=False at different points, ax.axis("Off"), spine.set_visible(False), but they always either only take away the frame from my ax-object returned by return_ax_obj and keep the ones outside or the actually take away the background and the whole figure looks shitty :(

    def show_evol(data, x = "dis", y = "up", c = "avg", cmap = "jet",width = None, hight = None, size = None, itering = True, hardcut_up = False, hardcut_down = False,times = [int(365/2), int(365/6)]):

    if hight == None or width == None:
    width, hight = get_boundries(data, x,y,size=size, itering=itering)


    array_list = [array_of_all(data,x,y,c,hight,width,0,hardcut_down,hardcut_up,size,itering)]

    for i in times:
    array_list += [array_of_diff(data,x,y,c,hight,width,i,hardcut_down,hardcut_up,size,itering)]
    # date_array = array_of_all(data, hight=hight, dis = dis, y_list = y_list, c_list = c_list, date = i)
    # array_list += [array_of_diff(data, y_list = y_list, c_list = c_list, hight = hight, dis = dis, date = i)]

    fig = plt.figure()
    gs = fig.add_gridspec(2, 3, height_ratios=[1, 0.5])
    # gs = matplotlib.gridspec.GridSpec(2, 3, fig)

    axs_top = [fig.add_subplot(gs[0, i]) for i in range(3)]

    ar_ax1 = fig.add_subplot(gs[0,0])
    ar_ax2 = fig.add_subplot(gs[0, 1], sharey = ar_ax1)
    ar_ax3 = fig.add_subplot(gs[0, 2], sharey = ar_ax1)

    ar_list = [ar_ax1, ar_ax2, ar_ax3]

    # for ax in ar_list:
    # ax.set_frame_on(False)




    # for i, array in enumerate(array_list):

    # ar_list.add_image, ar_list = return_ax_obj(array,x,y,c,cmap,width,hight, title = f"this is a test for {i}")#[1]

    for ax, array in zip(ar_list, array_list):

    # ax.set_frame_on(True)
    return_ax_obj(array,x,y,c,cmap,width,hight, title = f"this is a test for {i}", ax = ax, fig = fig)
    # for spine in ax.spines.values():
    # spine.set_visible(True)


    # for ax, array in zip(ar_list, array_list):
    # # Entferne gezielt nur den äußeren Rahmen der Subplots
    # for spine in ['top', 'right', 'left', 'bottom']:
    # ax.spines[spine].set_visible(False)

    # # Optional: Tick-Parameter anpassen (falls nötig)
    # ax.tick_params(axis='both', which='both', length=0)

    # # Bild und Colorbar anzeigen
    # return_ax_obj(array, x, y, c, cmap, width, hight, title=f"this is a test for {i}", ax=ax)




    plt.show()


    I can verify, that i can successfully return an ax object and display it with:

    fig, ax = plt.subplots()
    new_bike_functions.return_ax_obj(my_other_alltime_array, width = my_iter_boundries[0], hight = my_iter_boundries[1], title = "test",fig = fig, ax = ax)
    plt.show()


    The resulting image seems to be correct: Working return, with all the labels, titles and numbers beeing displayed and alle the ratios and sizes beeing correct

    The way the return-function is written, there is a nother ways to verify its working:

    ax =new_bike_functions.return_ax_obj(my_other_alltime_array, width = my_iter_boundries[0], hight = my_iter_boundries[1], title = "test")
    print(ax)
    print(ax[0])
    print(ax[1])
    plt.show()


    This way we dont need a predefined plt.figure() to see the image, but we get an figure with axis, created by the function. The printout looks like this:

    (<matplotlib.image.AxesImage object at 0x00000207EC776D50>, <Axes: title={'center': 'test'}, xlabel='distance in km', ylabel='total uphill in m'>)
    AxesImage(shape=(9, 9))
    Axes(0.125,0.11;0.62x0.77)


    The picture looks the same: second verification

    My understanding is that, when called with ax = ax, the return function should manipulate the ax and the return is irrelevant but with no axis called, the function return the plt.imshow()-object aswell as the axis created by fig, axis = plt.subplots() as an tuple of (AxisImage, Axes).

    I expect the genereal Code aboce (the larger one, not the verification), to someday create a picture like follows: The expected figure (plot below not yet implemented) Note that this is hardcoded and it looks awful in Code (but nice result). But i want to be able to define the images above in my return_ax_obj-function and then have pyplot display it as predefined by the return-function. This way for example, i could have the colorbars very easily have the same layout and everything, but in the hardcode-solution they need to be fixed for axis[0,0] and axis[0,1:] seperatly. Just an example, there are several things i want to generalise and fix the layout for every picture the same way.

    Can you guys please help me with this?

    I also tried adding the images returned by the return-function via ax.add_image, ax-add_artist and ax.add_something_else_i_forgot_rn, but none of those worked for my intended porpuse. I think the solution is to remove the frames initiated by the subplot-initiallisation, but i dont know how. Maybe someone can explain this to everyone and also maybe someone has a better idea to solve the problem.

    Thanks in advance!

    Continue reading...

Compartilhe esta Página