简体   繁体   English

条形图matplotlib / python上的叠加框图

[英]Overlay box plots on bars matplotlib/python

I am trying to overlay a set of box plots on bars using separate y axis in matplotlib. 我正在尝试在matplotlib中使用单独的y轴在条形图上叠加一组箱形图。 I can't find an example anywhere and have tried everything I can think of with ax.set_zorder() and ax2.set_alpha(). 我在任何地方都找不到示例,并且尝试使用ax.set_zorder()和ax2.set_alpha()进行尝试。 Is there a way to set the background opaque from just the box plots? 有没有一种方法可以仅通过箱形图来设置背景不透明? ax.patch.set_facecolor('None') removes the background completely... ax.patch.set_facecolor('None')完全删除背景...

Here's what I've tried: 这是我尝试过的:

import matplotlib.pyplot as plt
import numpy as np

example_data = [1,2,1],[1,2,2,2],[3,2,1]

data = [i for i in example_data]

data_len = [len(i) for i in example_data]

labels = ['one','two','three']

plt.figure()

fig, ax = plt.subplots()

plt.boxplot(data)

ax.set_xticklabels(labels,rotation='vertical')


ax2 = ax.twinx()

ax2.bar(ax.get_xticks(),data_len,color='yellow', align='center')

ax2.set_zorder(1)

ax.set_zorder(2)

ax2.set_alpha(0.1)

ax.patch.set_facecolor('None')

plt.show()

Thanks in advance for your help. 在此先感谢您的帮助。

How about just changing the order of your plots so that the box plot is displayed after the bar plot? 仅更改图的顺序以使箱形图显示在条形图之后怎么样? For example: 例如:

import matplotlib.pyplot as plt
import numpy as np

example_data = [[1,2,1], [1,2,2,2], [3,2,1]]
data_len = [len(i) for i in example_data]
labels = ['one','two','three']

fig, ax = plt.subplots()
ax.bar(range(1, len(data_len)+1), data_len, color='yellow', align='center')
ax2 = ax.twinx()
ax2.boxplot(example_data)
ax2.set_ylim(ax.get_ylim())
ax.set_xticklabels(labels, rotation='vertical')
plt.show()

This would give you: 这将为您提供:

条形图和箱形图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM