# plotly 小提琴圖

## 在 Python 中使用 Plotly 的 `violin()` 函式建立小提琴圖

Plotly 中的小提琴圖表示變數通過其四分位數的分佈。它與箱線圖相同，但它還在圖的每一側繪製旋轉密度。

``````import plotly.express as px

labels = ['A','B','C']
value =  [[10,20,30],[20,30,20],[10,20,30],[10,10,20]]
fig = px.violin(x=labels,y=value, width=500, height=400)
fig.show()
``````

## 在 Python 中使用 Plotly 繪製 Violin 圖的 `box` 引數

``````import plotly.express as px

labels = ['A','B','C']
value =  [[10,20,30],[20,30,20],[10,20,30],[10,10,20]]
fig = px.violin(x=labels,y=value, width=500, height=400, box=True)
fig.show()
``````

## 在 Python 中使用 Plotly 的 `color` 引數更改小提琴圖的顏色

width 和 height 引數用於設定圖形的寬度和高度（以畫素為單位）。我們可以使用 `color` 引數為每​​個單元格指定預設顏色，並將其值設定為標籤。

``````import plotly.express as px

labels = ['A','B','C']
value =  [[10,50,30],[20,30,60],[10,20,30],[10,10,10]]
fig = px.violin(x=labels,y=value, width=700, height=400, color=labels, title='Plotly violin Plot',facet_col=[1,2,'b'])
fig.show()
``````

``````import plotly.express as px

labels = ['A','B','C']
value =  [[10,50,30],[20,30,60],[10,20,30],[10,10,10]]
fig = px.violin(x=labels,y=value, width=700, height=400, color=labels, color_discrete_map={'A':'green',
'B':'cyan',
'C':'yellow',})
fig.show()
``````

``````import plotly.express as px

labels = ['A','B','C']
value =  [[10,50,30],[20,30,60],[10,20,30],[10,10,10]]
fig = px.violin(x=labels,y=value, width=700, height=400, color=labels, color_discrete_sequence=px.colors.qualitative.Dark2)
fig.show()
``````

``````import plotly.express as px

labels = ['A','B','C']
value =  [[10,50,30],[20,30,60],[10,20,30],[10,10,10]]
fig = px.violin(x=labels,y=value, width=700, height=400, color=labels)
fig.update_traces(fillcolor='yellow', opacity=0.8,legendgrouptitle_text='LegendTitle')

fig.show()
``````

Author: Ammar Ali

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.