- I would like to produce the following graph
- Note that one bar is partially blue and partially teal. This is what I’m trying to reproduce
JavaScript
x
45
45
1
import pandas as pd
2
import seaborn as sns
3
import matplotlib.pyplot as plt
4
import numpy as np
5
from matplotlib.patches import Rectangle
6
7
# plt parameters
8
plt.rcParams['figure.figsize'] = (10.0, 10.0)
9
plt.style.use('seaborn-dark-palette')
10
plt.rcParams['axes.grid'] = True
11
plt.rcParams["patch.force_edgecolor"] = True
12
13
# data
14
np.random.seed(365)
15
replicates = np.random.normal(0.0011542834124829882, 1.6243483937004772, 10000)
16
17
mean_diff = 1.1582360922659518
18
19
# plot replicates
20
# p = sns.distplot(replicates, bins=30, )
21
22
# distplot is deprecated, so use histplot
23
p = sns.histplot(replicates, bins=30, stat='density')
24
25
# add the vertical line
26
plt.vlines(mean_diff, 0, 0.25, color='r', label='mean')
27
28
# add the annotation
29
plt.annotate('p-value', xy=(3.5, 0.05), weight='bold', color='teal',
30
xytext=(4, 0.15), fontsize=15, arrowprops=dict(arrowstyle="->", color='teal'))
31
32
# color bars greater than mean_diff except the partial bar
33
for rectangle in p.patches:
34
if rectangle.get_x() >= mean_diff:
35
rectangle.set_facecolor('teal')
36
37
# I tried adding a Rectangle of the following dimensions, but it didn't color the rectangle
38
Rectangle(xy=(mean_diff, 0), width=1.28523-mean_diff, height=0.206371, angle=0).set_facecolor('teal')
39
40
# add cosmetics
41
plt.legend()
42
plt.ylabel('PDF')
43
plt.xlabel('PA - OH mean percent replicate vote difference')
44
plt.show()
45
Rectangle(xy=(0.876747, 0), width=0.408487, height=0.206371, angle=0)
is the Rectangle that needs to be partially colored.Rectangle(xy=(1.28523, 0), width=0.408487, height=0.150066, angle=0)
is the patch immediately after, which is coloredTeal
Plot generated by my code
- Note the partial bar, beginning at the mean, is not
teal
Advertisement
Answer
JavaScript
1
38
38
1
import pandas as pd
2
import seaborn as sns
3
import matplotlib.pyplot as plt
4
import numpy as np
5
from matplotlib.patches import Rectangle
6
7
# plt parameters
8
plt.rcParams['figure.figsize'] = (10.0, 10.0)
9
plt.style.use('seaborn-dark-palette')
10
plt.rcParams['axes.grid'] = True
11
plt.rcParams["patch.force_edgecolor"] = True
12
13
# data
14
np.random.seed(365)
15
replicates = np.random.normal(0.0011542834124829882, 1.6243483937004772, 10000)
16
17
mean_diff = 1.1582360922659518
18
19
# plot replicates
20
p = sns.distplot(replicates, bins=30)
21
22
# add the vertical line
23
plt.vlines(mean_diff, 0, 0.25, color='r', label='mean', colors="r")
24
25
# add the annotation
26
plt.annotate('p-value', xy=(3.5, 0.05), weight='bold', color='teal',
27
xytext=(4, 0.15), fontsize=15, arrowprops=dict(arrowstyle="->", color='teal'))
28
29
# color bars greater than mean_diff except the partial bar
30
for rectangle in p.patches:
31
if rectangle.get_x() >= mean_diff:
32
rectangle.set_facecolor('teal')
33
34
# I tried adding a Rectangle of the following dimensions, but it didn't color the rectangle
35
width = 1.28523-mean_diff
36
plt.bar(x=mean_diff+0.5*width,height=0.206371,width=width,color="#99cccc",edgecolor="#7a7d89")
37
plt.show()
38
This adds the bar in a matching manner. A bit hacky as we said before.