[英]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. 我想知道如何避免重复出现颜色,因为有时使用random.choice()会得到一些相同颜色的轨道。 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)
. 如果希望采样的颜色彼此唯一,则可以使用random.sample(population, k)
。 It samples k "unique" elements from the population. 它从总体中采样了k个“独特”元素。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.