[英]Non-repeating colours and specific linewidth in Python
這只是繪制一系列軌跡的代碼的一小部分。 我想知道如何避免重復出現顏色,因為有時使用random.choice()會得到一些相同顏色的軌道。 我想知道是否有可能為“機構”中包含的每個機構選擇特定的機體。 先感謝您。
#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()
如果希望采樣的顏色彼此唯一,則可以使用random.sample(population, k)
。 它從總體中采樣了k個“獨特”元素。
以下是顯示如何使用它的腳本:
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.