'b' | blue 蓝色 |
'g' | green 绿色 |
'r' | red 红色 |
'c' | cyan 蓝绿色 |
'm' | magenta 洋红 |
'y' | yellow 黄色 |
'k' | black 黑色 |
'w' | white 白色 |
下面给出两个封装的函数:
# -*- coding: utf-8 -*-
import turtle
import matplotlib.pyplot as plt
from matplotlib import patches, colors
import math
def _color():
fig = plt.figure()
ax = fig.add_subplot(111)
ratio = 1.0 / 3.0
count = math.ceil(math.sqrt(len(colors.cnames)))
x_count = count * ratio
y_count = count / ratio
x = 0
y = 0
w = 1 / x_count
h = 1 / y_count
for c in colors.cnames:
pos = (x / x_count, y / y_count)
ax.add_patch(patches.Rectangle(pos, w, h, color=c))
ax.annotate(c, xy=pos)
if y >= y_count - 1:
x += 1
y = 0
else:
y += 1
plt.show()
def _color_try(is_look_gif=True):
turtle.screensize(800, 600, 'black')
t = turtle.Turtle()
t.speed(0)
t.hideturtle()
i = 0
if is_look_gif == False:
turtle.tracer(0, 0) # 快速显示开关
while i < 1202:
if i < 150:
t.pencolor('cyan')
t.penup()
t.goto(0, 0)
t.forward(200)
t.pendown()
t.forward(10)
t.right(2)
elif i < 300:
t.pencolor('green')
t.penup()
t.goto(0, 0)
t.forward(180)
t.pendown()
t.forward(10)
t.right(2)
elif i < 450:
t.pencolor('yellow')
t.penup()
t.goto(0, 0)
t.forward(160)
t.pendown()
t.forward(10)
t.right(2)
elif i < 600:
t.pencolor('red')
t.penup()
t.goto(0, 0)
t.forward(140)
t.pendown()
t.forward(10)
t.right(2)
elif i < 750:
t.pencolor('blue')
t.penup()
t.goto(0, 0)
t.forward(120)
t.pendown()
t.forward(10)
t.right(2)
elif i < 900:
t.pencolor('purple')
t.penup()
t.goto(0, 0)
t.forward(100)
t.pendown()
t.forward(10)
t.right(2)
elif i < 1050:
t.pencolor('chocolate')
t.penup()
t.goto(0, 0)
t.forward(80)
t.pendown()
t.forward(10)
t.right(2)
elif i < 1200:
t.pencolor('magenta')
t.penup()
t.goto(0, 0)
t.forward(60)
t.pendown()
t.forward(10)
t.right(2)
elif i < 1202:
t.pencolor('blue')
t.pensize(3)
t.penup()
t.goto(17, 10)
t.pendown()
t.circle(20)
else:
pass
i += 1
turtle.done()
if __name__ == '__main__':
_color()
_color_try(is_look_gif=False)
第一个封装的函数用来看颜色名称,第二个是用其中的名称试验一下,用到了turtle小乌龟函数。效果如下所示:
![](https://i-blog.csdnimg.cn/blog_migrate/ba3b6606701023eb14db347015e97384.png)
![](https://i-blog.csdnimg.cn/blog_migrate/6e885c0ebca73670b9b6f6d35fc3a9d7.png)