Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Removing duplicates from animation's lened of a 3d plot in python

I am exporting an animation in python but the legend is repeating. I have only one plot and want to have one single legend item in every frame of the animation. This is my script:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
x = np.linspace(0., 10., 100)
y = np.linspace(0., 10., 100)
z = np.random.rand(100)
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot (111, projection="3d")

def init():
    # Plot the surface.
    ax.scatter3D(x, y, z, label='random', s=10)
    ax.set_zlabel('Z [m]')
    ax.set_ylabel('Y [m]')
    ax.set_xlabel('X [m]')
    plt.legend()
    ax.grid(None)
    return fig,

def animate(i):
    ax.view_init(elev=20, azim=i)
    return fig,

# Animate
ani = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=360, interval=200, blit=True)

# Export
ani.save('random data.gif', writer='pillow', fps=30, dpi=50)

And this is the animation in which legend is repeated three times:

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I very much appreciate any help.

>Solution :

As was suggested here, replace plt.legend() with the following 3 lines:

def init():
    # Plot the surface.
    ax.scatter3D(x, y, z, label='not random', s=10)
    ax.set_zlabel('Z [m]')
    ax.set_ylabel('Y [m]')
    ax.set_xlabel('X [m]')

    # REPLACE plt.legend() STARTS HERE
    handles, labels = plt.gca().get_legend_handles_labels()
    by_label = dict(zip(labels, handles))
    plt.legend(by_label.values(), by_label.keys())
    # REPLACE plt.legend() ENDS HERE    

    ax.grid(None)
    return fig, 

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading