使用python创建颜色生成器

一、简介

无论是科研作图还是前端页面设计,颜色的选择往往是理工男的一大难题。笔者使用“matplotlib”库来随机生成颜色并输出颜色的html码以供选择,大大便捷了广大科研人员及前端开发者。

二、代码内容

import matplotlib.pyplot as plt
import random

# Function to generate a random color in hex format
def random_color():
    return "#{:06x}".format(random.randint(0, 0xFFFFFF))

# Generate a list of random colors
num_colors = 5  # You can change this to the number of colors you need
colors = [random_color() for _ in range(num_colors)]

# Output the generated colors in HTML format
for i, color in enumerate(colors):
    print(f"Color {i+1}: {color}")

# Sample data for pie chart
data = [10, 20, 30, 25, 15]

# Plotting a pie chart with the random colors
plt.pie(data, colors=colors, wedgeprops={"edgecolor":"black"})
plt.show()

  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值