柱形分布图系列
柱形分布图系列
使用柱形图的方式展示数据的分布规律;
可以借助误差线或散点图;
带误差线的柱形图就是使用每个类别的均值作为柱形的高度;
再根据每个类别的标准差绘制误差线;
缺点:无法显示数据的分布情况;
可以在带误差线的柱形图的基础上,添加抖动散点图;
优点:可以方便观察数据分布规律;
绘制柱形分布图系列
带误差线柱形图使用statsummary(fundata='meansdl',geom='bar')实现柱形图;使用statsummary(fundata='meansdl',geom='errorbar')实现误差线绘制;再使用geom_jitter()函数添加抖动散点图;
绘制带误差线的柱形图
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from plotnine import *
df=pd.read_csv('d:\python\out\DistributionD.csv')
df['class']=df['class'].astype("category",["n", "s", "k", "mm"])
#带误差线的柱形图
Barjitter_plot=(ggplot(df,aes(x='class',y="value",fill="class"))
+stat_summary(fun_data="mean_sdl",fun_args = {'mult':1},
geom="bar", color = "black",size = 0.75,width=0.7,show_legend=False)
+stat_summary(fun_data="mean_sdl", fun_args = {'mult':1},
geom="errorbar", color = "black",size = 0.75,width=.2,show_legend=False)
+scale_fill_hue(s = 0.90, l = 0.65, h=0.0417,color_space='husl')
+ylim(0,7)
+theme_matplotlib()
+theme(aspect_ratio =1.05,
dpi=100,
figure_size=(4,4)))
print(Barjitter_plot)
Barjitter_plot.save("d:\python\out\Barjitter_plot.pdf")
绘制带误差线柱形与抖动散点图;
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from plotnine import *
df=pd.read_csv('d:\python\out\DistributionD.csv')
df['class']=df['class'].astype("category",["n", "s", "k", "mm"])
#带误差线柱形与抖动图
Barjitter_plot2=(ggplot(df,aes(x='class',y="value",fill="class"))
+stat_summary(fun_data="mean_sdl", fun_args = {'mult':1},
geom="bar", fill="w",color = "black",size =0.75,width=0.7,show_legend=False)
+stat_summary(fun_data="mean_sdl",fun_args = {'mult':1},
geom="errorbar", color = "black",size = 0.75,width=.2,show_legend=False)
+geom_jitter(width=0.3,size=2,stroke=0.1,shape='o',show_legend=False)
+scale_fill_hue(s = 0.90, l = 0.65, h=0.0417,color_space='husl')
+ylim(0,7)
+theme_matplotlib()
+theme(aspect_ratio =1.05,
dpi=100,
figure_size=(4,4)))
print(Barjitter_plot2)
Barjitter_plot.save("d:\python\out\Barjitter_plot2.pdf")