Home > Software design >  How to keep label appearing only once but use a iterating number within it
How to keep label appearing only once but use a iterating number within it

Time:01-26

I am trying to create an animation that will show a line moving up a graph based on a current radius. Right now, my code is creating a new label for each graph and I cannot figure out how to use the iterating variable i inside of the label to have the label read "current radius = i", where i is the index of iteration, not the letter i, if that makes sense. I have posted my code and its output below, any help here is greatly appreciated!

list = np.linspace(0,1,12)  # vector of increasing radii
death_list = np.array( (.1, .2, .3, .4, .8) )   # list of radii that cause deaths

fig, (ax1, ax2) = plt.subplots(1, 2)
camera = Camera(fig)

for i in list :
    ax1.scatter(x,y, color = "green", marker = "o", s = 15)

    for xx,yy in zip(x,y) :
        cir = mpatches.Circle((xx, yy), i, color='r',fill=True, zorder = 0)
        ax1.add_patch(cir)

    ax1.set_aspect('equal', adjustable='datalim')
    ax1.set_title('Random Points on a Circle')
    ax1.set(xlabel='x-axis', ylabel='y-axis')
    ax1.set_xlim([-1.25, 1.25])
    ax1.set_ylim([-1.25, 1.25])

    a = np.arange(0,1.1,.1)
    b = a
    ax2.plot(a,b, color = 'b')
    ax2.axhline(y = i, label = f'current radius = {i}', color = 'r', linestyle = '-')

    if i in death_list :
        ax2.plot(0, i, marker="o", markersize = 5, markeredgecolor = "green", markerfacecolor = "green")
    
    ax2.set_aspect('equal', adjustable='datalim')
    ax2.set_title('Persistence Diagram')
    ax2.set(xlabel='Birth', ylabel='Death')
    ax2.legend(loc = 'lower right')

enter image description here

CodePudding user response:

If I understand your problem correctly, you want each line to be labeled in the legend through the following replacement: ax2.axhline(y = i, label = 'current radius = ', color = 'r', linestyle = '-')ax2.axhline(y = i, label = 'current radius = {0}'.format(i), color = 'r', linestyle = '-')

Let me know if this is not what you are looking for. I dealt with a similar issue recently.

EDIT: @matszwecja I didn't know you could do that directly. You learn something new everyday.

SECONDARY EDIT: @Piethon, to answer your questions within your comment below Also, is there a way to truncate the decimal?

Yes, with my method, it is very easy. Just replace {0} with {0:.Nf} if you want N decimal places, etc. For example {0:.2f} will output two decimal places.

As for your other question Ok, awesome that helped! I have updated the question with the new code and the new output, it still outputting the label multiple times, should I place the ax2.legend(loc = 'lower right') line outside of the for loop? , I'm not really sure what you mean. You do have a lot of unnecessary repetition though in your code. For example:

ax1.set_aspect('equal', adjustable='datalim')
ax1.set_title('Random Points on a Circle')
ax1.set(xlabel='x-axis', ylabel='y-axis')
ax1.set_xlim([-1.25, 1.25])
ax1.set_ylim([-1.25, 1.25])
...

ax2.set_aspect('equal', adjustable='datalim')
ax2.set_title('Persistence Diagram')
ax2.set(xlabel='Birth', ylabel='Death')
ax2.legend(loc = 'lower right')

Can all be put outside of your loop.

CodePudding user response:

I would use f-string formatting:

label = f'current radius = {i}'
  • Related