我正在尝试使用Seaborn创建一个boxplot和一个stripplot,就像在this中一样。但是,在箱线图的顶部可能难以读取stripplot的数据点。我的目标是拥有一个'开放'的箱形图,就像大熊猫DataFrame.plot(kind ='box')所做的那样。见this example。但我仍然希望Seaborn内置分组功能。
我的尝试是使用PatchArtist而不是Line2D艺术家。来自here,
kwargs : key, value mappings
Other keyword arguments are passed through to plt.boxplot at draw time.
但是传递patch_artist = True
会导致错误:TypeError: boxplot() got multiple values for keyword argument 'patch_artist'
。
最小的工作示例:
import seaborn as sns
data = sns.load_dataset('tips')
sns.boxplot(x='day', y='total_bill', data=data, **{'notch':True})
以上示例显示kwargs正确传递给plt.boxplot()。以下示例生成TypeError
。
import seaborn as sns
data = sns.load_dataset('tips')
sns.boxplot(x='day', y='total_bill', data=data, **{'patch_artist':True})
patch_artist
是生成打开的箱形图的最佳方式吗?如果是这样,我怎样才能将其与seaborn一起使用?
答案 0 :(得分:2)
As it turns out, seaborn returns the subplot axis that it just generated. We can directly set properties on the artists it creates.
A minimal example:
var vm = require('vm');
var babel = require("babel-core");
// VM context object
var context = {
require: require,
callback: function(error) {
if (error) {
console.log(error.stack);
} else {
console.log(this.response);
}
}
};
// ES 7 code
var code = "var request = require('request-promise'); var response = await request({ url: 'https://graph.facebook.com/?id=http://news.ycombinator.com', json: true })";
// Wrap the code
code = "'use strict'; async function run() { try { " + code.replace(/var /g, "this.") + "; this.callback(null); } catch(error) { this.callback(error); } }; run.apply(this)";
// Transpile code ES7 -> ES5
var regeneratedCode = babel.transform(code, { "ast": false, "presets": ["stage-0"] }).code;
// Create VM context
var vmContext = new vm.createContext(context);
// Create virtual script
var script = new vm.Script(regeneratedCode);
// Run script
script.runInContext(vmContext, {displayErrors: true, timeout: 30000});
This lets us manipulate each patch individually, while still using the import seaborn as sns
import matplotlib.pyplot as plt
data = sns.load_dataset('tips')
ax = sns.boxplot(x='day', y='total_bill', data=data)
plt.setp(ax.artists, alpha=.5, linewidth=2, fill=False, edgecolor="k")
sns.stripplot(x='day', y='total_bill', data=data, jitter=True, edgecolor='gray')
variable in hue
to group the data for us. Finally, the sns.boxplot()
overlay sits on top of the box.