如何指定部分直方图块的颜色?
How can I specify the color for a partial histogram patch?
- 我想制作下图
- 请注意,一根柱子部分是蓝色的,部分是蓝绿色的。这就是我要重现的内容
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Rectangle
# plt parameters
plt.rcParams['figure.figsize'] = (10.0, 10.0)
plt.style.use('seaborn-dark-palette')
plt.rcParams['axes.grid'] = True
plt.rcParams["patch.force_edgecolor"] = True
# data
np.random.seed(365)
replicates = np.random.normal(0.0011542834124829882, 1.6243483937004772, 10000)
mean_diff = 1.1582360922659518
# plot replicates
# p = sns.distplot(replicates, bins=30, )
# distplot is deprecated, so use histplot
p = sns.histplot(replicates, bins=30, stat='density')
# add the vertical line
plt.vlines(mean_diff, 0, 0.25, color='r', label='mean')
# add the annotation
plt.annotate('p-value', xy=(3.5, 0.05), weight='bold', color='teal',
xytext=(4, 0.15), fontsize=15, arrowprops=dict(arrowstyle="->", color='teal'))
# color bars greater than mean_diff except the partial bar
for rectangle in p.patches:
if rectangle.get_x() >= mean_diff:
rectangle.set_facecolor('teal')
# I tried adding a Rectangle of the following dimensions, but it didn't color the rectangle
Rectangle(xy=(mean_diff, 0), width=1.28523-mean_diff, height=0.206371, angle=0).set_facecolor('teal')
# add cosmetics
plt.legend()
plt.ylabel('PDF')
plt.xlabel('PA - OH mean percent replicate vote difference')
plt.show()
Rectangle(xy=(0.876747, 0), width=0.408487, height=0.206371, angle=0)
是需要局部着色的Rectangle。
Rectangle(xy=(1.28523, 0), width=0.408487, height=0.150066, angle=0)
是紧随其后的补丁,颜色为 Teal
我的代码生成的图
- 请注意,从平均值开始的部分柱状图不是
teal
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Rectangle
# plt parameters
plt.rcParams['figure.figsize'] = (10.0, 10.0)
plt.style.use('seaborn-dark-palette')
plt.rcParams['axes.grid'] = True
plt.rcParams["patch.force_edgecolor"] = True
# data
np.random.seed(365)
replicates = np.random.normal(0.0011542834124829882, 1.6243483937004772, 10000)
mean_diff = 1.1582360922659518
# plot replicates
p = sns.distplot(replicates, bins=30)
# add the vertical line
plt.vlines(mean_diff, 0, 0.25, color='r', label='mean', colors="r")
# add the annotation
plt.annotate('p-value', xy=(3.5, 0.05), weight='bold', color='teal',
xytext=(4, 0.15), fontsize=15, arrowprops=dict(arrowstyle="->", color='teal'))
# color bars greater than mean_diff except the partial bar
for rectangle in p.patches:
if rectangle.get_x() >= mean_diff:
rectangle.set_facecolor('teal')
# I tried adding a Rectangle of the following dimensions, but it didn't color the rectangle
width = 1.28523-mean_diff
plt.bar(x=mean_diff+0.5*width,height=0.206371,width=width,color="#99cccc",edgecolor="#7a7d89")
plt.show()
这将以匹配的方式添加栏。正如我们之前所说,有点老套。
- 我想制作下图
- 请注意,一根柱子部分是蓝色的,部分是蓝绿色的。这就是我要重现的内容
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Rectangle
# plt parameters
plt.rcParams['figure.figsize'] = (10.0, 10.0)
plt.style.use('seaborn-dark-palette')
plt.rcParams['axes.grid'] = True
plt.rcParams["patch.force_edgecolor"] = True
# data
np.random.seed(365)
replicates = np.random.normal(0.0011542834124829882, 1.6243483937004772, 10000)
mean_diff = 1.1582360922659518
# plot replicates
# p = sns.distplot(replicates, bins=30, )
# distplot is deprecated, so use histplot
p = sns.histplot(replicates, bins=30, stat='density')
# add the vertical line
plt.vlines(mean_diff, 0, 0.25, color='r', label='mean')
# add the annotation
plt.annotate('p-value', xy=(3.5, 0.05), weight='bold', color='teal',
xytext=(4, 0.15), fontsize=15, arrowprops=dict(arrowstyle="->", color='teal'))
# color bars greater than mean_diff except the partial bar
for rectangle in p.patches:
if rectangle.get_x() >= mean_diff:
rectangle.set_facecolor('teal')
# I tried adding a Rectangle of the following dimensions, but it didn't color the rectangle
Rectangle(xy=(mean_diff, 0), width=1.28523-mean_diff, height=0.206371, angle=0).set_facecolor('teal')
# add cosmetics
plt.legend()
plt.ylabel('PDF')
plt.xlabel('PA - OH mean percent replicate vote difference')
plt.show()
Rectangle(xy=(0.876747, 0), width=0.408487, height=0.206371, angle=0)
是需要局部着色的Rectangle。Rectangle(xy=(1.28523, 0), width=0.408487, height=0.150066, angle=0)
是紧随其后的补丁,颜色为Teal
我的代码生成的图
- 请注意,从平均值开始的部分柱状图不是
teal
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Rectangle
# plt parameters
plt.rcParams['figure.figsize'] = (10.0, 10.0)
plt.style.use('seaborn-dark-palette')
plt.rcParams['axes.grid'] = True
plt.rcParams["patch.force_edgecolor"] = True
# data
np.random.seed(365)
replicates = np.random.normal(0.0011542834124829882, 1.6243483937004772, 10000)
mean_diff = 1.1582360922659518
# plot replicates
p = sns.distplot(replicates, bins=30)
# add the vertical line
plt.vlines(mean_diff, 0, 0.25, color='r', label='mean', colors="r")
# add the annotation
plt.annotate('p-value', xy=(3.5, 0.05), weight='bold', color='teal',
xytext=(4, 0.15), fontsize=15, arrowprops=dict(arrowstyle="->", color='teal'))
# color bars greater than mean_diff except the partial bar
for rectangle in p.patches:
if rectangle.get_x() >= mean_diff:
rectangle.set_facecolor('teal')
# I tried adding a Rectangle of the following dimensions, but it didn't color the rectangle
width = 1.28523-mean_diff
plt.bar(x=mean_diff+0.5*width,height=0.206371,width=width,color="#99cccc",edgecolor="#7a7d89")
plt.show()
这将以匹配的方式添加栏。正如我们之前所说,有点老套。