如何使用 Plotly 创建三元叠加?
Plotly 是一个Python库,用于设计图形,尤其是交互式图形。它可以绘制各种图形和图表,如直方图、条形图、箱线图、展开图等等。它主要用于数据分析以及财务分析。 plotly 是一个交互式可视化库
创建三元散点图
三元散点图是基于所需类别列的每个位置的符号的三元图,并且任何行中的三个数据点 (X + Y + Z) 的总和必须等于 100%。
Syntax: scatter_ternary( a=None, b=None, c=None, color=None,labels={},width=None, height=None)
Parameters:
a: Values from this column or array_like are used to position marks along the a axis in ternary coordinates.
b: Values from this column or array_like are used to position marks along the b axis in ternary coordinates.
c: Values from this column or array_like are used to position marks along the c axis in ternary coordinates.
color: Either a name of a column in data_frame, or a pandas Series or array_like object. Values from this column or array_like are used to assign color to marks.
width: The figure width in pixels.
height: The figure height in pixels.
例子:
Python3
import plotly.express as px
df = px.data.iris()
fig = px.scatter_ternary(df, a="sepal_length",
b="sepal_width", c="petal_length",
color="species", size_max=20)
fig.show()
Python3
import plotly.express as px
import plotly.graph_objects as go
df = px.data.iris()
fig = go.Figure(go.Scatterternary({
'mode': 'lines',
'a': df['sepal_length'],
'b': df['sepal_width'],
'c': df['petal_length'],
'line': {'color': '#444', 'shape': 'spline'},
'marker': {
'color': 'green',
'size': 14,
'line': {'width': 2}
}
}))
fig.show()
输出:
创建三元等值线图
三元等高线图是一种图形表示,它通过在二维格式上绘制恒定 z 切片(称为等高线)来显示 3 维表面。
例子:
蟒蛇3
import plotly.express as px
import plotly.graph_objects as go
df = px.data.iris()
fig = go.Figure(go.Scatterternary({
'mode': 'lines',
'a': df['sepal_length'],
'b': df['sepal_width'],
'c': df['petal_length'],
'line': {'color': '#444', 'shape': 'spline'},
'marker': {
'color': 'green',
'size': 14,
'line': {'width': 2}
}
}))
fig.show()