栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

了解matplotlib转换

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

了解matplotlib转换

我从matplotlib用户邮件列表的Ryan获得了解决方案。非常优雅,因此我在这里分享他的例子:

import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.patches import Rectanglefrom matplotlib.collections import PatchCollectionn = 100# Get your xy data points, which are the centers of the rectangles.xy = np.random.rand(n,2)# Set a fixed heightheight = 0.02# The variable widths of the rectangleswidths = np.random.rand(n)*0.1# Get a color map and make some colorscmap = plt.cm.hsvcolors = np.random.rand(n)*10.# Make a normalized array of colorscolors_norm = colors/colors.max()# Here's where you have to make a ScalarMappable with the colormapmappable = plt.cm.ScalarMappable(cmap=cmap)# Give it your non-normalized color datamappable.set_array(colors)rects = []for p, w in zip(xy, widths):    xpos = p[0] - w/2 # The x position will be half the width from the center    ypos = p[1] - height/2 # same for the y position, but with height    rect = Rectangle( (xpos, ypos), w, height ) # Create a rectangle    rects.append(rect) # Add the rectangle patch to our list# Create a collection from the rectanglescol = PatchCollection(rects)# set the alpha for all rectanglescol.set_alpha(0.3)# Set the colors using the colormapcol.set_facecolor( cmap(colors_norm) )# No linescol.set_linewidth( 0 )#col.set_edgecolor( 'none' )# Make a figure and add the collection to the axis.fig = plt.figure()ax = fig.add_subplot(111)ax.add_collection(col)# Add your ScalarMappable to a figure colorbarfig.colorbar(mappable)plt.show()

谢谢Ryan,以及所有贡献自己想法的人!



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/419894.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号