我想设置多个传单颜色,并有一个图例。我当前的代码如下:例如,从此代码提供的输出中,我希望将2020年的一个传单设置为不同的颜色。有人知道怎么做吗?谢谢!
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
boxes = [
{
'label' : "2020",
'whislo': 1.49, # Bottom whisker position
'q1' : 9.36, # First quartile (25th percentile)
'med' : 14.21, # Median (50th percentile)
'q3' : 18.73, # Third quartile (75th percentile)
'whishi': 54.76, # Top whisker position
'fliers': [10.7, 9.4] # Outliers
},
{
'label' : "2019",
'whislo': 0.63, # Bottom whisker position
'q1' : 6.11, # First quartile (25th percentile)
'med' : 9.66, # Median (50th percentile)
'q3' : 15.33, # Third quartile (75th percentile)
'whishi': 23.89, # Top whisker position
'fliers': [2.8, 9.7] # Outliers
},
{
'label' : "2018",
'whislo': -8.19, # Bottom whisker position
'q1' : -0.15, # First quartile (25th percentile)
'med' : 2.66, # Median (50th percentile)
'q3' : 7.85, # Third quartile (75th percentile)
'whishi': 13.25, # Top whisker position
'fliers': [8.6] # Outliers
},
{
'label' : "2017",
'whislo': 3.51, # Bottom whisker position
'q1' : 7.74, # First quartile (25th percentile)
'med' : 10.91, # Median (50th percentile)
'q3' : 15.04, # Third quartile (75th percentile)
'whishi': 22.47, # Top whisker position
'fliers': [15.3] # Outliers
},
{
'label' : "2016",
'whislo': -3.92, # Bottom whisker position
'q1' : 0.05, # First quartile (25th percentile)
'med' : 3.79, # Median (50th percentile)
'q3' : 7.60, # Third quartile (75th percentile)
'whishi': 14.65, # Top whisker position
'fliers': [0.4] # Outliers
}
]
ax.bxp(boxes, showfliers=True, flierprops={'markerfacecolor':'fuchsia', 'marker':'o'})
plt.ylim([-10,65])
plt.show()发布于 2021-08-12 12:08:20
最简单的方法是再次绘制该点(使用1作为x位置,这是第一个框的默认x位置)。例如,ax.plot(1, 10.7, marker='o', markerfacecolor='lime')。要在图例中标记此点,可以使用ax.plot(...., label=...)。
与许多matplotlib函数一样,ax.bxp返回有关创建的图形元素的信息。在本例中,它是一个字典,其条目为'fliers',指向一个列表。这里的每个条目都是一个点列表,每个框一个列表。例如,您可以使用box_info['fliers'][0].set_color('turquoise')更改属于第一个框的所有传单的颜色。类似地,可以使用.set_label(...)在图例中添加条目。
from matplotlib import pyplot as plt
fig, ax = plt.subplots()
box_info = ax.bxp(boxes, showfliers=True, flierprops={'markerfacecolor': 'fuchsia', 'marker': 'o'})
box_info['fliers'][-1].set_label('Outliers')
ax.plot(1, 10.7, marker='o', markerfacecolor='lime', linestyle='', label='Special outlier')
ax.legend()
ax.set_ylim([-10, 65])
plt.show()

https://stackoverflow.com/questions/68749597
复制相似问题