简体   繁体   中英

Non-repeating colours and specific linewidth in Python

this is a little part of a code which plots a serie of orbits. I would like to know how to not get repeated colors, because sometimes with random.choice() I get some orbits of the same color. I would like to know if is it possible to select a specific linewitdh for every body contained in "bodies". Thank you in advance.

#Output of the code
def plot_output(bodies, outfile = None):
    fig = plot.figure()
    colours = ['r', 'b','g','y','m','c', 'black','aqua', 'salmon', 'orangered', 'lime', 'mediumseagreen', 'yellow', 'gold', 'darkcyan', 'lime', 'magenta', 'grey', 'mediumslateblue', 'dimgray', 'deeppink', 'firebrick', 'pink', 'deepskyblue', 'olive', 'greenyellow', 'thistle', 'springgreen']
    ax = fig.add_subplot(1,1,1, projection='3d')
    max_range = 0
    for current_body in bodies:
        max_dim = max(max(current_body["x"]),max(current_body["y"]),max(current_body["z"]))
        if max_dim > max_range:
            max_range = max_dim
        ax.plot(current_body["x"], current_body["y"], current_body["z"], c = random.choice(colours), label = current_body["name"])       
    ax.set_xlim([-max_range,max_range])    
    ax.set_ylim([-max_range,max_range])
    ax.set_zlim([-max_range,max_range])
    ax.legend()       

    if outfile:
        plot.savefig(outfile)
    else:
        plot.show()

If you want the sampled colors be unique to each other, you can use random.sample(population, k) . It samples k "unique" elements from the population.

Following is the script showing how you can use it:

bodies = ['a', 'b', 'c']
colours = ['r', 'b','g','y','m','c', 'black','aqua', 'salmon', 'orangered', 'lime', 'mediumseagreen', 'yellow', 'gold', 'darkcyan', 'lime', 'magenta', 'grey', 'mediumslateblue', 'dimgray', 'deeppink', 'firebrick', 'pink', 'deepskyblue', 'olive', 'greenyellow', 'thistle', 'springgreen']
sampled_colours = random.sample(colours, len(bodies))

for current_body, colour in zip(bodies, sampled_colours):
    print(current_body, colour)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM