参考文章
https://www.tutorialspoint.com/plotly/plotly_box_violin_and_contour_plot.htm
实例
https://www.cnblogs.com/feffery/p/9293745.html
有颜色
https://www.jianshu.com/p/4f4daf47cc85
参数详解
https://www.kaggle.com/kanncaa1/plotly-tutorial-for-beginners
kaggle for beginners
最基本箱线图
from plotly.offline import plot
import plotly.graph_objs as go
y = [1140,1460,489,594,502,508,370,200]
trace1 = go.Box(y=y)
layout = go.Layout(plot_bgcolor='#ffffff',width=500,height=500)
data = [trace1]
fig = go.Figure(data=data,layout=layout)
fig.show()
![](https://img.haomeiwen.com/i6857799/ec5d100d388b509c.png)
更改一些设置
import plotly.graph_objs as go
import numpy as np
np.random.seed(10)
c1 = np.random.normal(100,10,200)
c2 = np.random.normal(80,30,200)
trace1 = go.Box(y=c1,fillcolor="#ff7500",marker_color="#ff7500",name="ABC")
trace2 = go.Box(y=c2,fillcolor="#16a951",marker={'color':"#16a951"},name="DEF")
data = [trace1,trace2]
layout = go.Layout(plot_bgcolor='#ffffff',width=500,height=500)
fig = go.Figure(data=data,layout=layout)
fig.show()
![](https://img.haomeiwen.com/i6857799/aaeba022e7540868.png)
遇到一个问题
如何做分组的箱线图
找到分组柱形图的例子
import plotly.graph_objs as go
x = ["A","B","C"]
y1 = [10,12,14]
y2 = [11,9,20]
trace1 = go.Bar(x=x,y=y1,name='citations',text="kobe")
trace2 = go.Bar(x=x,y=y2,name='teaching',text='Bryant')
data = [trace1,trace2]
layout = go.Layout(barmode="group")
fig = go.Figure(data=data,layout=layout)
fig.show()
![](https://img.haomeiwen.com/i6857799/7b633229a2903c32.png)
模仿柱形图做箱线图
import plotly.graph_objs as go
import numpy as np
np.random.seed(10)
x1 = ["A"]*200
c1 = np.random.normal(100,10,200)
c2 = np.random.normal(80,30,200)
x2 = ["B"]*200
c3 = np.random.normal(100,10,200)
c4 = np.random.normal(80,30,200)
trace1 = go.Box(x=x1,y=c1,fillcolor="#ff7500",marker_color="#ff7500",name="ABC")
trace2 = go.Box(x=x1,y=c2,fillcolor="#16a951",marker={'color':"#16a951"},name="DEF")
trace3 = go.Box(x=x2,y=c3,fillcolor="#ff7500",marker_color="#ff7500",name="ABC")
trace4 = go.Box(x=x2,y=c4,fillcolor="#16a951",marker={'color':"#16a951"},name="DEF")
data = [trace1,trace2,trace3,trace4]
layout = go.Layout(plot_bgcolor='#ffffff',width=500,height=500,boxmode='group')
fig = go.Figure(data=data,layout=layout)
fig.show()
![](https://img.haomeiwen.com/i6857799/6f5fbb17037abf03.png)
这个应该不对:
第一个问题是x轴的标签位置是外的,图例应该只有两个。
找到了一个alignmentgroup参数,暂时不知道怎么用!
今天就到这里了
更新 20200519 下午
分组的箱线图是这样
import plotly.graph_objs as go
import numpy as np
np.random.seed(10)
x1 = ["A"]*50
x2 = ["B"]*50
x3 = ["C"]*50
x4 = ["D"]*50
x = x1 + x2 + x3 + x4
print(x)
c1 = np.random.normal(100,10,200)
c2 = np.random.normal(80,30,200)
trace1 = go.Box(x=x,y=c1,fillcolor="#ff7500",marker_color="#ff7500",name="ABC")
trace2 = go.Box(x=x,y=c2,fillcolor="#16a951",marker={'color':"#16a951"},name="DEF")
data = [trace1,trace2]
layout = go.Layout(plot_bgcolor='#ffffff',width=500,height=500,boxmode='group')
fig = go.Figure(data=data,layout=layout)
fig.show()
![](https://img.haomeiwen.com/i6857799/c9bd6e724847352f.png)
小提琴图
import plotly.graph_objs as go
import numpy as np
np.random.seed(10)
x1 = ["A"]*50
x2 = ["B"]*50
x3 = ["C"]*50
x4 = ["D"]*50
x = x1 + x2 + x3 + x4
print(x)
c1 = np.random.normal(100,10,200)
c2 = np.random.normal(80,30,200)
trace1 = go.Violin(x=x,y=c1,fillcolor="#ff7500",marker_color="#ff7500",name="ABC")
trace2 = go.Violin(x=x,y=c2,fillcolor="#16a951",marker={'color':"#16a951"},name="DEF")
data = [trace1,trace2]
layout = go.Layout(plot_bgcolor='#ffffff',width=800,height=500,violinmode='group')
fig = go.Figure(data=data,layout=layout)
fig.show()
![](https://img.haomeiwen.com/i6857799/d2821bde55e5ab15.png)
欢迎大家关注我的公众号
小明的数据分析笔记本
![](https://img.haomeiwen.com/i6857799/083e444502ce9c33.jpg)
网友评论