How can I specify the color for a partial histogram patch?

半城伤御伤魂 提交于 2021-01-27 23:37:18

问题


  • 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

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=clrs)

# 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


回答1:


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.



来源:https://stackoverflow.com/questions/62737627/how-can-i-specify-the-color-for-a-partial-histogram-patch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!