Python Matplotlib:如何根据绘图中文本的大小指定增加的颜色阴影?
我有一大堆词:Python Matplotlib:如何根据绘图中文本的大小指定增加的颜色阴影?,python,matplotlib,plot,python-ggplot,Python,Matplotlib,Plot,Python Ggplot,我有一大堆词: words = ['an', 'apple', 'in', 'a', 'tree'] counts = [23, 12, 45, 20, 9] 如何在matplotlib中根据单词的值以及 颜色变化是否平稳(例如厘米蓝) 我的尝试是: import numpy as np import matplotlib.pyplot as plt plt.rc('font',**{'size':20, 'family':'fantasy'}) from collections impor
words = ['an', 'apple', 'in', 'a', 'tree']
counts = [23, 12, 45, 20, 9]
如何在matplotlib中根据单词的值以及
颜色变化是否平稳(例如厘米蓝)
我的尝试是:
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\u颜色是使用十六进制代码定义的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()
编辑
好的,这是另一个版本,您可以选择色调颜色(介于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()
CSS4_颜色是使用十六进制代码定义的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()
编辑
好的,这是另一个版本,您可以选择色调颜色(介于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()
如果要定义颜色,为什么要使用随机整数设置文本颜色?要更改每个单词的颜色和阴影,是吗?如果要定义颜色,为什么要使用随机整数设置文本颜色?您想更改每个单词的颜色和阴影,是吗?谢谢,但是,它仍然提供不同的颜色,而不是不同的蓝色或类似颜色。在这个问题中,它不需要使用css颜色,我们可以使用我们想要的任何颜色,只是阴影渐变应该不同。我用一个新版本编辑答案,在这个版本中,我只更改饱和度。因此,颜色总是一样的。谢谢,但是,它仍然提供不同的颜色,而不是不同深浅的蓝色或类似的颜色。在这个问题中,它不需要使用css颜色,我们可以使用我们想要的任何颜色,只是阴影渐变应该不同。我用一个新版本编辑答案,在这个版本中,我只更改饱和度。因此,颜色总是相同的。