Python 在 pandas 或 matplotlib 的一张图中绘制多个箱线图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27061137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Plot multiple boxplot in one graph in pandas or matplotlib?
提问by Shin
I have a two boxplotes
我有两个箱线图
a1=a[['kCH4_sync','week_days']]
a1.boxplot(by = 'week_days', meanline=True, showmeans=True, showcaps=True, showbox=True,
showfliers=False)
a2=a[['CH4_sync','week_days']]
a2.boxplot(by = 'week_days', meanline=True, showmeans=True, showcaps=True, showbox=True,
showfliers=False)
But I want to place them in one graph to compare them. Have you any advice to solve this problem? Thanks!
但我想将它们放置在一个图形进行比较。你有什么建议来解决这个问题吗?谢谢!
采纳答案by unutbu
Use return_type='axes'to get a1.boxplotto return a matplotlib Axesobject.
Then pass that axes to the second call to boxplotusing ax=ax. This will cause both boxplots to be drawn on the same axes.
使用return_type='axes'得到a1.boxplot返回一个matplotlibAxes对象。然后将该轴传递给boxplotusing的第二次调用ax=ax。这将导致在同一轴上绘制两个箱线图。
a1=a[['kCH4_sync','week_days']]
ax = a1.boxplot(by='week_days', meanline=True, showmeans=True, showcaps=True,
showbox=True, showfliers=False, return_type='axes')
a2 = a[['CH4_sync','week_days']]
a2.boxplot(by='week_days', meanline=True, showmeans=True, showcaps=True,
showbox=True, showfliers=False, ax=ax)
回答by rhody
To plot multiple boxplots on one matplotlib graph you can pass a list of data arrays to boxplot, as in:
要在一个 matplotlib 图上绘制多个箱线图,您可以将数据数组列表传递给箱线图,如下所示:
import numpy as np
import matplotlib.pyplot as plt
x1 = 10*np.random.random(100)
x2 = 10*np.random.exponential(0.5, 100)
x3 = 10*np.random.normal(0, 0.4, 100)
plt.boxplot ([x1, x2, x3])
The only thing I am not sure of is if you want each boxplot to have a different color etc. Generally it won't plot in different colour
我唯一不确定的是,如果您希望每个箱线图具有不同的颜色等。通常它不会以不同的颜色绘制

