热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Python中的Matplotlib.colors.rgb_to_hsv()

Python中的Matplotlib.colors.rgb_to_hsv()

Python 中的 Matplotlib.colors.rgb_to_hsv()

原文:https://www . geesforgeks . org/matplotlib-colors-RGB _ to _ HSV-in-python/

Matplotlib 是 Python 中一个惊人的可视化库,用于数组的 2D 图。Matplotlib 是一个多平台数据可视化库,构建在 NumPy 数组上,旨在与更广泛的 SciPy 堆栈一起工作。

matplotlib.colors.rgb_to_hsv()

matplotlib . colors . RGB _ to _ HSV()功能属于 matplotlib.colors 模块。matplotlib.colors.rgb_to_hsv()功能用于将 0 到 1 范围内的浮点 rgb 转换为 hsv 值的 numpy 数组。

语法:matplotlib . colors . RGB _ to _ HSV(arr)

参数:


  • arr: 它是一个类似数组的参数,形式为(…,3),其中所有值必须在 0 到 1 的范围内。

返回:


  • hsv: 它以(…,3)的形式返回一个数组,该数组由转换为 0 到 1 范围内的 hsv 值的颜色组成。


例 1:

import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
# helper function to plot a 
# color table
def colortable(colors, title, 
               colors_sort = True,
               emptycols=0):
    # cell dimensions
    width = 212
    height = 22
    swatch_width = 48
    margin = 12
    topmargin = 40
    # Sorting colors based on hue,
    # saturation, value and name.
    if colors_sort is True:
        to_hsv = sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgb(color))),
                         name)
                        for name, color in colors.items())
        names = [name for hsv, name in to_hsv]
    else:
        names = list(colors)
    length_of_names = len(names)
    length_cols = 4 - emptycols
    length_rows = length_of_names // length_cols + int(length_of_names % length_cols > 0)
    width2 = width * 4 + 2 * margin
    height2 = height * length_rows + margin + topmargin
    dpi = 72
    figure, axes = plt.subplots(figsize=(width2 / dpi, height2 / dpi),
                                dpi=dpi)
    figure.subplots_adjust(margin/width2, margin/height2,
                           (width2-margin)/width2, 
                           (height2-topmargin)/height2)
    axes.set_xlim(0, width * 4)
    axes.set_ylim(height * (length_rows-0.5), -height/2.)
    axes.yaxis.set_visible(False)
    axes.xaxis.set_visible(False)
    axes.set_axis_off()
    axes.set_title(title, fOntsize=24, loc="left", pad=10)
    for i, name in enumerate(names):
        rows = i % length_rows
        cols = i // length_rows
        y = rows * height
        swatch_start_x = width * cols
        swatch_end_x = width * cols + swatch_width
        text_pos_x = width * cols + swatch_width + 7
        axes.text(text_pos_x, y, name, fOntsize=14,
                horizOntalalignment='left',
                verticalalignment='center')
        axes.hlines(y, swatch_start_x, swatch_end_x,
                  color=colors[name], lineBase Colors",
                colors_sort=False, emptycols=1)
colortable(mcolors.TABLEAU_COLORS, "Tableau Palette",
                colors_sort=False, emptycols=2)
colortable(mcolors.CSS4_COLORS, "CSS Colors")
plt.show()

输出:

例 2:

使用的图像:

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image = mpimg.imread('food.jpeg')
plt.title("Output image")
hsv = matplotlib.colors.rgb_to_hsv(image)
plt.imshow(hsv)

输出:


推荐阅读
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • Go 中的 init 函数 ... [详细]
  • r2dbc配置多数据源
    R2dbc配置多数据源问题根据官网配置r2dbc连接mysql多数据源所遇到的问题pom配置可以参考官网,不过我这样配置会报错我并没有这样配置将以下内容添加到pom.xml文件d ... [详细]
  • 花瓣|目标值_Compose 动画边学边做夏日彩虹
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了Compose动画边学边做-夏日彩虹相关的知识,希望对你有一定的参考价值。引言Comp ... [详细]
  • 互联网世界 9 种基本的商业模式
    互联网世界9种基本的商业模式一个商业模式是运行一个公司的方法;通过该模式的运作,一个公司能维持自己的生存,就是说,能有收益。商业模式意味着一个公司是如何通过在价值链中定位自己,从而获 ... [详细]
  • 第38天:Python decimal 模块
    by程序员野客在我们开发工作中浮点类型的使用还是比较普遍的,对于一些涉及资金金额的计算更是不能有丝毫误差,Python的decimal模块为浮点型精确计算提供了支持。1简介deci ... [详细]
  • pdf怎么把html变成pdf1 用AdobeAcroat8.1.2,打开网页后,页面右键菜单中会出现一个“转换为AobePDF的选项,点击就可以转换。 安装AdobeAcroba ... [详细]
  • 最近学习了数据挖掘常用的两种算法:FP-Growth和K-Means。现在把我的学习结果分享给大家。以下是本文的目录,大家可以根据需要跳过一些章节:1.FP-Grow ... [详细]
  • 使用Python中的Plotly绘制三维网格图原文:ht ... [详细]
  • 零入门kubernetes网络实战15>基于golang编程实现给ns网络命名空间添加额外的网卡
    《零入门kubernetes网络实战》视频专栏地址https:www.ixigua.com7193641905282875942本篇文章视频地址(稍后上传)本篇文章主要是想通过g ... [详细]
  • Java在PDF文档中添加或删除页面
    当你编辑一个PDF文档时,有时需要删除文档中多余的页面或 ... [详细]
  • 开发笔记:Spring执行ApplicationEvent事件顺序ServletWebServerInitializedEvent
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了Spring执行ApplicationEvent事件顺序ServletWebServerInitializedEvent相关的知识,希望对你有一 ... [详细]
  •  //CAUTION:Followtheconfigurationorderforsettingtheports.   //1)settingva ... [详细]
  • 这篇文章主要介绍“CSS浮动和定位属性介绍”,在日常操作中,相信很多人在CSS浮动和定位属性介绍问题上存在疑惑,小编查阅了各式资料,整理出简单 ... [详细]
  • 1,数据组织:训练验证数据生成:pythoncreate_data.pynuscenes_data_prep--data_pathNU ... [详细]
author-avatar
123AJAgjt
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有