python数据可视化的时候用到的颜色标记

'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小乌龟函数。效果如下所示:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值