Skip to content
Advertisement

How to specify the color for a partial histogram patch

  • 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

enter image description here

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) 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 colored Teal

Plot generated by my code

  • Note the partial bar, beginning at the mean, is not teal

enter image description here

Advertisement

Answer

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()

This adds the bar in a matching manner. A bit hacky as we said before. enter image description here

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement