Python并排matplotlib带颜色的箱线图

Python并排matplotlib带颜色的箱线图,python,matplotlib,boxplot,Python,Matplotlib,Boxplot,我遵循了关于如何创建带有颜色的箱线图的示例。 我一直在尝试不同的方法,在两个不同的位置分离这些箱线图,而不是两者重叠,但都没有用。如果我为它们指定不同的位置,它们将保持在bp2位置。如何将这两个方框图并排放置 import matplotlib.pyplot as plt def color_boxplot(data, color): bp = boxplot(data, patch_artist=True, showmeans=True) for item in ['boxes

我遵循了关于如何创建带有颜色的箱线图的示例。 我一直在尝试不同的方法,在两个不同的位置分离这些箱线图,而不是两者重叠,但都没有用。如果我为它们指定不同的位置,它们将保持在bp2位置。如何将这两个方框图并排放置

import matplotlib.pyplot as plt

def color_boxplot(data, color):
   bp = boxplot(data, patch_artist=True,  showmeans=True)
   for item in ['boxes', 'whiskers', 'fliers', 'medians', 'caps']:
        plt.setp(bp[item], color=color)


data1 = [1, 2, 3, 4, 5]
data2 = [4, 5, 6, 7, 8]
fig, ax = plt.subplots()
bp1 = color_boxplot(data1, 'green')
bp2 = color_boxplot(data2, 'red')
plt.show()
编辑:添加示例数据


使用seaborn制造的预制件怎么样

import seaborn as sns
sns.boxplot(data=[data1, data2])
如果要手动选择颜色,可以使用xkcd:

尽管我认为在这种情况下,这可能是最好的选择,但如果您真的想保持原始帖子的外观,您必须修改代码,以便只使用一次对boxplot的调用。这样,matplotlib负责将它们正确定位在轴上。然后,您可以迭代boxplot返回的字典以调整输出

data1 = [1, 2, 3, 4, 5]
data2 = [4, 5, 6, 7, 8]
data3 = [0, 1, 2]
data = [data1,data2, data3]
colors = ['red','green','blue']
fig, ax = plt.subplots()
box_dict = ax.boxplot(data, patch_artist=True,  showmeans=True)
for item in ['boxes', 'fliers', 'medians', 'means']:
    for sub_item,color in zip(box_dict[item], colors):
        plt.setp(sub_item, color=color)
# whiskers and caps have to be treated separately since there are two of each for each plot
for item in ['whiskers', 'caps']:
    for sub_items,color in zip(zip(box_dict[item][::2],box_dict[item][1::2]),colors):
        plt.setp(sub_items, color=color)

为了保持代码基本完整,只需在函数中添加另一个位置参数即可

import matplotlib.pyplot as plt

def color_boxplot(data, color, pos=[0], ax=None):
    ax = ax or plt.gca()
    bp = ax.boxplot(data, patch_artist=True,  showmeans=True, positions=pos)
    for item in ['boxes', 'whiskers', 'fliers', 'medians', 'caps']:
        plt.setp(bp[item], color=color)


data1 = [1, 2, 3, 4, 5]
data2 = [4, 5, 6, 7, 8]
fig, ax = plt.subplots()
bp1 = color_boxplot(data1, 'green', [1])
bp2 = color_boxplot(data2, 'red', [2])
ax.autoscale()
ax.set(xticks=[1,2], xticklabels=[1,2])
plt.show()

您能提供一些示例数据,以便我们运行代码吗?使用seaborn给这些箱线图上色会更容易吗?这就是我一直在苦苦挣扎的唯一原因。@Gabriele默认情况下,两个方框图的颜色不同。你需要手动选择颜色吗?快速谷歌搜索不会告诉我如何使用seaborn显示每个方框图的平均值。这与matplotlib中的showmeans=True不同。我不是一个聪明人。谢谢,萨沙@Gabriele通常,您可以预期对matplotlib有效的参数对seaborn中的类似绘图函数有效。在seaborn Documentation中,它们被标记为
**kwargs
,回答很好。有没有关于添加传奇的想法?
import matplotlib.pyplot as plt

def color_boxplot(data, color, pos=[0], ax=None):
    ax = ax or plt.gca()
    bp = ax.boxplot(data, patch_artist=True,  showmeans=True, positions=pos)
    for item in ['boxes', 'whiskers', 'fliers', 'medians', 'caps']:
        plt.setp(bp[item], color=color)


data1 = [1, 2, 3, 4, 5]
data2 = [4, 5, 6, 7, 8]
fig, ax = plt.subplots()
bp1 = color_boxplot(data1, 'green', [1])
bp2 = color_boxplot(data2, 'red', [2])
ax.autoscale()
ax.set(xticks=[1,2], xticklabels=[1,2])
plt.show()