Matplotlib:如何根据图中文本的大小分配渐增的颜色阴影?
Matplotlib: How to assign increasing shade of colors according to the size of text in a plot?
我有一堆词频:
words = ['an', 'apple', 'in', 'a', 'tree']
counts = [23, 12, 45, 20, 9]
如何根据单词的值绘制matplotlib中的单词以及
颜色变化平滑(例如cm.Blues)?
我的尝试是这样的:
import numpy as np
import matplotlib.pyplot as plt
plt.rc('font',**{'size':20, 'family':'fantasy'})
from collections import Counter
from matplotlib import colors as mcolors
words = ['an', 'apple', 'in', 'a', 'tree']
counts = [23, 12, 45, 20, 9]
plt.figure(figsize=(8,8))
c = list(mcolors.CSS4_COLORS.values())
for i in range(len(words)):
x = np.random.uniform(low=0, high=1)
y = np.random.uniform(low=0, high=1)
plt.text(x, y, words[i],
size=counts[i]*2,
rotation=np.random.choice([-90, 0.0, 90]),
color=c[np.random.randint(0,len(c))])
plt.setp(plt.gca(), frame_on=False, xticks=(), yticks=())
plt.show()
颜色随机。
如何分配渐增的颜色深浅使这张图更直观?
CSS4_colors 是使用十六进制代码定义的 HTML 颜色。如果您想更改饱和度或亮度,这不是最佳选择。您可能想要使用 HSV 颜色模型。
因此您必须将颜色转换为 hsv,然后根据您的字数更改 s 或 v。
import numpy as np
import matplotlib.pyplot as plt
plt.rc('font',**{'size':20, 'family':'fantasy'})
from matplotlib import colors as mcolors
words = ['an', 'apple', 'in', 'a', 'tree']
counts = [23, 12, 45, 20, 9]
maxcounts = max(counts)
sat = [c / maxcounts for c in counts]
plt.figure(figsize=(8,8))
# convert colors to HSV
css4_colors = list(mcolors.CSS4_COLORS.values())
ncolors = len(c)
rgb = [mcolors.to_rgb(c) for c in css4_colors]
hsv = [mcolors.rgb_to_hsv(c) for c in rgb]
for i in range(len(words)):
x = np.random.uniform(low=0, high=1)
y = np.random.uniform(low=0, high=1)
# select a color
icolor = np.random.randint(0, ncolors)
color = hsv[icolor]
color[1] = sat[i] # here change saturation, index 1 or brightness, index 2 according to the counts list
plt.text(x, y, words[i],
size=counts[i] * 2,
rotation=np.random.choice([-90, 0.0, 90]),
color=mcolors.hsv_to_rgb(color)) # don't forget to get back to rgb
plt.setp(plt.gca(), frame_on=False, xticks=(), yticks=())
plt.show()
编辑
好的,这是另一个版本,您 select 色调颜色(介于 0 和 1 之间)、亮度和仅更改饱和度。
根据自己的需要,可以根据计数改变亮度,保持饱和度不变
import numpy as np
import matplotlib.pyplot as plt
plt.rc('font',**{'size':20, 'family':'fantasy'})
from matplotlib import colors as mcolors
words = ['an', 'apple', 'in', 'a', 'tree']
counts = [23, 12, 45, 20, 9]
maxcounts = max(counts)
sat = [c / maxcounts for c in counts]
plt.figure(figsize=(8,8))
# select a hue value => define the color
hue = 0.6
# choose a brightness
brightness = 1
for i in range(len(words)):
x = np.random.uniform(low=0, high=1)
y = np.random.uniform(low=0, high=1)
# select a color
color = (hue, sat[i], brightness)
plt.text(x, y, words[i],
size=counts[i] * 2,
rotation=np.random.choice([-90, 0.0, 90]),
color=mcolors.hsv_to_rgb(color)) # don't forget to get back to rgb
plt.setp(plt.gca(), frame_on=False, xticks=(), yticks=())
plt.show()
我有一堆词频:
words = ['an', 'apple', 'in', 'a', 'tree']
counts = [23, 12, 45, 20, 9]
如何根据单词的值绘制matplotlib中的单词以及 颜色变化平滑(例如cm.Blues)?
我的尝试是这样的:
import numpy as np
import matplotlib.pyplot as plt
plt.rc('font',**{'size':20, 'family':'fantasy'})
from collections import Counter
from matplotlib import colors as mcolors
words = ['an', 'apple', 'in', 'a', 'tree']
counts = [23, 12, 45, 20, 9]
plt.figure(figsize=(8,8))
c = list(mcolors.CSS4_COLORS.values())
for i in range(len(words)):
x = np.random.uniform(low=0, high=1)
y = np.random.uniform(low=0, high=1)
plt.text(x, y, words[i],
size=counts[i]*2,
rotation=np.random.choice([-90, 0.0, 90]),
color=c[np.random.randint(0,len(c))])
plt.setp(plt.gca(), frame_on=False, xticks=(), yticks=())
plt.show()
颜色随机。
如何分配渐增的颜色深浅使这张图更直观?
CSS4_colors 是使用十六进制代码定义的 HTML 颜色。如果您想更改饱和度或亮度,这不是最佳选择。您可能想要使用 HSV 颜色模型。
因此您必须将颜色转换为 hsv,然后根据您的字数更改 s 或 v。
import numpy as np
import matplotlib.pyplot as plt
plt.rc('font',**{'size':20, 'family':'fantasy'})
from matplotlib import colors as mcolors
words = ['an', 'apple', 'in', 'a', 'tree']
counts = [23, 12, 45, 20, 9]
maxcounts = max(counts)
sat = [c / maxcounts for c in counts]
plt.figure(figsize=(8,8))
# convert colors to HSV
css4_colors = list(mcolors.CSS4_COLORS.values())
ncolors = len(c)
rgb = [mcolors.to_rgb(c) for c in css4_colors]
hsv = [mcolors.rgb_to_hsv(c) for c in rgb]
for i in range(len(words)):
x = np.random.uniform(low=0, high=1)
y = np.random.uniform(low=0, high=1)
# select a color
icolor = np.random.randint(0, ncolors)
color = hsv[icolor]
color[1] = sat[i] # here change saturation, index 1 or brightness, index 2 according to the counts list
plt.text(x, y, words[i],
size=counts[i] * 2,
rotation=np.random.choice([-90, 0.0, 90]),
color=mcolors.hsv_to_rgb(color)) # don't forget to get back to rgb
plt.setp(plt.gca(), frame_on=False, xticks=(), yticks=())
plt.show()
编辑
好的,这是另一个版本,您 select 色调颜色(介于 0 和 1 之间)、亮度和仅更改饱和度。
根据自己的需要,可以根据计数改变亮度,保持饱和度不变
import numpy as np
import matplotlib.pyplot as plt
plt.rc('font',**{'size':20, 'family':'fantasy'})
from matplotlib import colors as mcolors
words = ['an', 'apple', 'in', 'a', 'tree']
counts = [23, 12, 45, 20, 9]
maxcounts = max(counts)
sat = [c / maxcounts for c in counts]
plt.figure(figsize=(8,8))
# select a hue value => define the color
hue = 0.6
# choose a brightness
brightness = 1
for i in range(len(words)):
x = np.random.uniform(low=0, high=1)
y = np.random.uniform(low=0, high=1)
# select a color
color = (hue, sat[i], brightness)
plt.text(x, y, words[i],
size=counts[i] * 2,
rotation=np.random.choice([-90, 0.0, 90]),
color=mcolors.hsv_to_rgb(color)) # don't forget to get back to rgb
plt.setp(plt.gca(), frame_on=False, xticks=(), yticks=())
plt.show()