📜  在Python使用 Plotly 的小提琴图

📅  最后修改于: 2022-05-13 01:54:34.617000             🧑  作者: Mango

在Python使用 Plotly 的小提琴图

Violin Plot是一种可视化不同变量数值数据分布的方法。它类似于箱线图,但每边都有一个旋转图,提供了关于 y 轴上密度估计的更多信息。密度被镜像并翻转,并填充生成的形状,创建类似于小提琴的图像。小提琴图的优点是它可以显示分布中的细微差别,而这些细微差别在箱线图中是看不到的。另一方面,箱线图更清楚地显示了数据中的异常值。

小提琴图比箱线图包含更多信息,它们不太受欢迎。由于它们不受欢迎,对于许多不熟悉小提琴情节表现的读者来说,它们的含义可能更难理解。

Plotly 是一个开源的Python模块,是一个非常强大的数据可视化工具。它支持各种绘图以轻松表示和研究数据。本文讨论了如何在 Plotly 的两个类(即 express 和 graph_objects)的帮助下使用 Plotly 获得小提琴图。

可以根据自己的方便选择类,但方法保持不变。

方法:



  • 导入模块
  • 导入数据
  • 使用所需参数调用 violinplot
  • 显示图

函数通过Plotly.express支持

句法:

函数通过Plotly.graph_objects支持

句法:

使用中的数据:畅销书

基本小提琴图

方法

  • 导入模块
  • 创建或加载数据框
  • 使用 violin() 绘图
  • 显示图

示例 1:使用 Plotly.express

Python3
import plotly.express as pt
import pandas as pd
  
data = pd.read_csv("C:\\Users\\Vanshi\\Desktop\\gfg\\bestsellers.csv")
df = pd.DataFrame(data)
data = df.head()
  
fig = pt.violin(data, y="Year")
fig.show()


Python3
import plotly.graph_objects as go
import pandas as pd
  
data = pd.read_csv("C:\\Users\\Vanshi\\Desktop\\gfg\\bestsellers.csv")
df = pd.DataFrame(data)
  
plot = go.Figure(data=go.Violin(y=df['Price']))
plot.show()


Python3
import plotly.express as pt
import pandas as pd
  
data = pd.read_csv("C:\\Users\\Vanshi\\Desktop\\gfg\\bestsellers.csv")
df = pd.DataFrame(data)
data = df.head()
  
# display box and scatter plot along with violin plot
fig = pt.violin(data, y="Year", box=True, points='all')
  
fig.show()


Python3
import plotly.graph_objects as go
import pandas as pd
  
data = pd.read_csv("C:\\Users\\Vanshi\\Desktop\\gfg\\bestsellers.csv")
df = pd.DataFrame(data)
  
plot = go.Figure(data=go.Violin(
    y=df['Price'], points='all', pointpos=2, box_visible=True))
  
plot.show()


Python3
import plotly.express as px
import pandas as pd
  
data = pd.read_csv("C:\\Users\\Vanshi\\Desktop\\gfg\\bestsellers.csv")
df = pd.DataFrame(data)
  
plot = px.violin(x=df['Year'], y=df['Price'])
plot.show()


Python3
import plotly.graph_objects as go
import pandas as pd
  
data = pd.read_csv("C:\\Users\\Vanshi\\Desktop\\gfg\\bestsellers.csv")
df = pd.DataFrame(data)
  
plot = go.Figure(data=go.Violin(x=df['Year'], y=df['Price']))
plot.show()


Python3
import plotly.express as px
import pandas as pd
  
data = pd.read_csv("C:\\Users\\Vanshi\\Desktop\\gfg\\bestsellers.csv")
df = pd.DataFrame(data)
  
plot = px.violin(df, x=df['Year'], y=df['Price'], color=df['Genre'])
plot.show()


Python3
import plotly.graph_objects as go
import pandas as pd
  
data = pd.read_csv("C:\\Users\\Vanshi\\Desktop\\gfg\\bestsellers.csv")
df = pd.DataFrame(data)
  
plot = go.Figure()
  
plot.add_trace(go.Violin(x=df['Year'][df['Genre'] == 'Fiction'],
                         y=df['Price'], line_color='red', name='Fiction'))
  
plot.add_trace(go.Violin(x=df['Year'][df['Genre'] == 'Non Fiction'],
                         y=df['Price'], line_color='blue', name='Non-Fiction'))
  
plot.update_layout(violinmode='group')
plot.show()


输出:



示例 2:使用 Graph_objects

蟒蛇3

import plotly.graph_objects as go
import pandas as pd
  
data = pd.read_csv("C:\\Users\\Vanshi\\Desktop\\gfg\\bestsellers.csv")
df = pd.DataFrame(data)
  
plot = go.Figure(data=go.Violin(y=df['Price']))
plot.show()

输出:

带框和散点图的小提琴图

上面的例子描绘了一个简单的小提琴图,但它可以与同一帧内的其他可视化描绘一起可视化。给定的示例显示了如何绘制小提琴图以及箱线图和散点图。

示例 1:使用 Plotly.express

蟒蛇3

import plotly.express as pt
import pandas as pd
  
data = pd.read_csv("C:\\Users\\Vanshi\\Desktop\\gfg\\bestsellers.csv")
df = pd.DataFrame(data)
data = df.head()
  
# display box and scatter plot along with violin plot
fig = pt.violin(data, y="Year", box=True, points='all')
  
fig.show()

输出:

示例 2:使用 graph_objects



蟒蛇3

import plotly.graph_objects as go
import pandas as pd
  
data = pd.read_csv("C:\\Users\\Vanshi\\Desktop\\gfg\\bestsellers.csv")
df = pd.DataFrame(data)
  
plot = go.Figure(data=go.Violin(
    y=df['Price'], points='all', pointpos=2, box_visible=True))
  
plot.show()

输出:

多小提琴情节

可以使用 plotly 在一帧中可视化多个小提琴图以同时比较它们。

示例 1:使用 Plotly express

蟒蛇3

import plotly.express as px
import pandas as pd
  
data = pd.read_csv("C:\\Users\\Vanshi\\Desktop\\gfg\\bestsellers.csv")
df = pd.DataFrame(data)
  
plot = px.violin(x=df['Year'], y=df['Price'])
plot.show()

输出:

示例 2:使用 graph_objects

蟒蛇3



import plotly.graph_objects as go
import pandas as pd
  
data = pd.read_csv("C:\\Users\\Vanshi\\Desktop\\gfg\\bestsellers.csv")
df = pd.DataFrame(data)
  
plot = go.Figure(data=go.Violin(x=df['Year'], y=df['Price']))
plot.show()

输出:

分组小提琴图

分组的小提琴情节允许比较多个小提琴情节,但有一些共同点,即它允许比较两个当代小提琴情节

示例 1:使用 Plotly express

蟒蛇3

import plotly.express as px
import pandas as pd
  
data = pd.read_csv("C:\\Users\\Vanshi\\Desktop\\gfg\\bestsellers.csv")
df = pd.DataFrame(data)
  
plot = px.violin(df, x=df['Year'], y=df['Price'], color=df['Genre'])
plot.show()

输出:

示例 2:使用 graph_objects

蟒蛇3

import plotly.graph_objects as go
import pandas as pd
  
data = pd.read_csv("C:\\Users\\Vanshi\\Desktop\\gfg\\bestsellers.csv")
df = pd.DataFrame(data)
  
plot = go.Figure()
  
plot.add_trace(go.Violin(x=df['Year'][df['Genre'] == 'Fiction'],
                         y=df['Price'], line_color='red', name='Fiction'))
  
plot.add_trace(go.Violin(x=df['Year'][df['Genre'] == 'Non Fiction'],
                         y=df['Price'], line_color='blue', name='Non-Fiction'))
  
plot.update_layout(violinmode='group')
plot.show()

输出: