Python 在范围内绘制矩形和颜色
Python draw rectangle and color in range
import brewer2mpl
import numpy as np
a = np.random.rand(3) # a[0] represents value of rect1, a[1] of rect[2]....
def get_colors():
"""
Get colorbrewer colors, which are nicer
"""
bmap = brewer2mpl.get_map('Accent','qualitative',6)
return bmap.mpl_colors
rect1 = matplotlib.patches.Rectangle((2,2), 1, 1, color='yellow'))
ax.add_patch(rect1)
rect2 = matplotlib.patches.Rectangle((3,3), 1, 1, color='green'))
ax.add_patch(rect2)
rect3 = matplotlib.patches.Rectangle((5,5), 1, 1, color='red'))
ax.add_patch(rect3)
我希望矩形的颜色根据向量的值而变化 'a'。不是纯 yellow/green/red 颜色,而是 select 范围内的颜色,最好是 brewer2mpl 颜色
据我所知,mpl_color 是一个颜色列表。颜色是三元组,范围在 (0,1) 代表 rgb 量。你可以把一个颜色设置成这样的三元组。
import pylab as py
py.plot([0,1],[2,3], color = (1,0,0.))
py.savefig('tmp.png')
因此,您所要做的就是获取向量 a
中的条目(我将在下面将其称为 avalue
,使用 0<avalue<1
)并将其映射到适当的整数.例如
colorlist = get_colors()
colorlength = len(colorlist)
py.plot([0,1],[2,3], color = colorlist[int(avalue*colorlength)])
您不一定需要创建一个 np 数组来保存您要使用的颜色的随机索引。
为了给每个矩形分配一种颜色,您可以使用 itertools 轻松循环颜色:
import brewer2mpl
import numpy as np
import itertools
color_map = brewer2mpl.get_map('Accent', 'qualitative', 6)
colors = itertools.cycle(color_map.mpl_colors)
rect1 = matplotlib.patches.Rectangle((2,2), 1, 1, color=next(colors))
这将按照原始顺序分配颜色。如果你想随机化颜色,你可以很容易地做到:
import random
import itertools
color_map = brewer2mpl.get_map('Accent', 'qualitative', 6)
colors = [c for c in color_map.mpl_colors]
random.shuffle(colors)
rnd_colors = itertools.cycle(colors)
rect1 = matplotlib.patches.Rectangle((2,2), 1, 1, color=next(rnd_colors))
如果你确定你的矩形有足够的颜色,你真的不需要循环,可以随机弹出一些:
color_map = brewer2mpl.get_map('Accent', 'qualitative', 6)
colors = [c for c in color_map.mpl_colors]
random.shuffle(colors)
rect1 = matplotlib.patches.Rectangle((2,2), 1, 1, color=colors.pop())
不确定是 colorbrewer 还是使用 a
更重要,而且它们似乎不适合同时使用。几种方法:
import brewer2mpl
import numpy as np
import matplotlib
bigness = 8
a = np.random.rand(bigness) # a[0] represents value of rect1, a[1] of rect[2]....
def get_colors(numpatches):
"""
Get colorbrewer colors, which are nicer
"""
bmap = brewer2mpl.get_map('Accent','qualitative', numpatches)
return bmap.mpl_colors
fig, axs = matplotlib.pyplot.subplots(ncols=3, nrows=1)
colors = get_colors(bigness)
for i in (0,2,3,5,bigness-1):
rect = matplotlib.patches.Rectangle((i,i), 1, 1, color=colors[i])
axs[0].add_patch(rect)
axs[0].set_title('Color evenly spaced\nfrom colorbrewer')
patches = []
for i in (0,2,3,5,bigness-1):
rect = matplotlib.patches.Rectangle((i,i), 1, 1)
patches.append(rect)
collection = matplotlib.collections.PatchCollection(patches,
cmap=matplotlib.cm.hot)
collection.set_array(a)
collection.set_edgecolor('none')
axs[1].add_collection(collection) # and why not?
axs[1].set_title('Color from\nrandom array a')
patches = []
for i in a:
loc = i*bigness
rect = matplotlib.patches.Rectangle((loc,loc), 1, 1)
patches.append(rect)
collection = matplotlib.collections.PatchCollection(patches,
cmap=matplotlib.cm.hot)
collection.set_array(a)
collection.set_edgecolor('none')
axs[2].add_collection(collection) # and why not?
axs[2].set_title('Color matches location\nboth from a')
for ax in axs:
ax.set_xlim((0,bigness))
ax.set_ylim((0,bigness))
from os.path import realpath, basename
s = basename(realpath(__file__))
fig = matplotlib.pyplot.gcf()
fig.savefig(s.split('.')[0])
matplotlib.pyplot.show()
import brewer2mpl
import numpy as np
a = np.random.rand(3) # a[0] represents value of rect1, a[1] of rect[2]....
def get_colors():
"""
Get colorbrewer colors, which are nicer
"""
bmap = brewer2mpl.get_map('Accent','qualitative',6)
return bmap.mpl_colors
rect1 = matplotlib.patches.Rectangle((2,2), 1, 1, color='yellow'))
ax.add_patch(rect1)
rect2 = matplotlib.patches.Rectangle((3,3), 1, 1, color='green'))
ax.add_patch(rect2)
rect3 = matplotlib.patches.Rectangle((5,5), 1, 1, color='red'))
ax.add_patch(rect3)
我希望矩形的颜色根据向量的值而变化 'a'。不是纯 yellow/green/red 颜色,而是 select 范围内的颜色,最好是 brewer2mpl 颜色
据我所知,mpl_color 是一个颜色列表。颜色是三元组,范围在 (0,1) 代表 rgb 量。你可以把一个颜色设置成这样的三元组。
import pylab as py
py.plot([0,1],[2,3], color = (1,0,0.))
py.savefig('tmp.png')
因此,您所要做的就是获取向量 a
中的条目(我将在下面将其称为 avalue
,使用 0<avalue<1
)并将其映射到适当的整数.例如
colorlist = get_colors()
colorlength = len(colorlist)
py.plot([0,1],[2,3], color = colorlist[int(avalue*colorlength)])
您不一定需要创建一个 np 数组来保存您要使用的颜色的随机索引。
为了给每个矩形分配一种颜色,您可以使用 itertools 轻松循环颜色:
import brewer2mpl
import numpy as np
import itertools
color_map = brewer2mpl.get_map('Accent', 'qualitative', 6)
colors = itertools.cycle(color_map.mpl_colors)
rect1 = matplotlib.patches.Rectangle((2,2), 1, 1, color=next(colors))
这将按照原始顺序分配颜色。如果你想随机化颜色,你可以很容易地做到:
import random
import itertools
color_map = brewer2mpl.get_map('Accent', 'qualitative', 6)
colors = [c for c in color_map.mpl_colors]
random.shuffle(colors)
rnd_colors = itertools.cycle(colors)
rect1 = matplotlib.patches.Rectangle((2,2), 1, 1, color=next(rnd_colors))
如果你确定你的矩形有足够的颜色,你真的不需要循环,可以随机弹出一些:
color_map = brewer2mpl.get_map('Accent', 'qualitative', 6)
colors = [c for c in color_map.mpl_colors]
random.shuffle(colors)
rect1 = matplotlib.patches.Rectangle((2,2), 1, 1, color=colors.pop())
不确定是 colorbrewer 还是使用 a
更重要,而且它们似乎不适合同时使用。几种方法:
import brewer2mpl
import numpy as np
import matplotlib
bigness = 8
a = np.random.rand(bigness) # a[0] represents value of rect1, a[1] of rect[2]....
def get_colors(numpatches):
"""
Get colorbrewer colors, which are nicer
"""
bmap = brewer2mpl.get_map('Accent','qualitative', numpatches)
return bmap.mpl_colors
fig, axs = matplotlib.pyplot.subplots(ncols=3, nrows=1)
colors = get_colors(bigness)
for i in (0,2,3,5,bigness-1):
rect = matplotlib.patches.Rectangle((i,i), 1, 1, color=colors[i])
axs[0].add_patch(rect)
axs[0].set_title('Color evenly spaced\nfrom colorbrewer')
patches = []
for i in (0,2,3,5,bigness-1):
rect = matplotlib.patches.Rectangle((i,i), 1, 1)
patches.append(rect)
collection = matplotlib.collections.PatchCollection(patches,
cmap=matplotlib.cm.hot)
collection.set_array(a)
collection.set_edgecolor('none')
axs[1].add_collection(collection) # and why not?
axs[1].set_title('Color from\nrandom array a')
patches = []
for i in a:
loc = i*bigness
rect = matplotlib.patches.Rectangle((loc,loc), 1, 1)
patches.append(rect)
collection = matplotlib.collections.PatchCollection(patches,
cmap=matplotlib.cm.hot)
collection.set_array(a)
collection.set_edgecolor('none')
axs[2].add_collection(collection) # and why not?
axs[2].set_title('Color matches location\nboth from a')
for ax in axs:
ax.set_xlim((0,bigness))
ax.set_ylim((0,bigness))
from os.path import realpath, basename
s = basename(realpath(__file__))
fig = matplotlib.pyplot.gcf()
fig.savefig(s.split('.')[0])
matplotlib.pyplot.show()