如何在Python中使用 Plotly 创建堆积面积图?
Plotly是一个Python库,用于设计图形,尤其是交互式图形。它可以绘制各种图形和图表,如直方图、条形图、箱线图、散布图等等。它主要用于数据分析和财务分析。 plotly 是一个交互式可视化库。
堆积面积图
堆积面积图是基本面积图的放大,在同一图形上显示若干组值的放大。每组的值显示在彼此的顶部。这是用于将类别分布显示为不需要累积总数的整个区域的一部分的最佳图表。可以通过在graph_objects类的scatter()方法中传递stackgroup参数来创建
示例 1:
Python3
import plotly.graph_objects as px
import numpy
# creating random data through randomint
# function of numpy.random
np.random.seed(42)
random_x= np.random.randint(1,101,100)
random_y= np.random.randint(1,101,100)
x = ['A', 'B', 'C', 'D']
plot = px.Figure()
plot.add_trace(go.Scatter(
name = 'Data 1',
x = x,
y = [100, 200, 500, 673],
stackgroup='one'
))
plot.add_trace(go.Scatter(
name = 'Data 2',
x = x,
y = [56, 123, 982, 213],
stackgroup='one'
)
)
plot.show()
Python3
import plotly.graph_objects as go
import numpy
plot = go.Figure(data=[go.Scatter(
x = np.random.randn(1000),
y = np.random.randn(1000),
stackgroup='one'),
go.Scatter(
x = np.random.randn(10),
y = np.random.randn(50),
stackgroup='one')
])
plot.show()
Python3
import plotly.graph_objects as go
import plotly.express as px
import numpy
df = px.data.iris()
plot = go.Figure(data=[go.Scatter(
x = df['sepal_width'],
y = df['sepal_length'],
stackgroup='one'),
go.Scatter(
x = df['petal_width'],
y = df['petal_length'],
stackgroup='one')
])
plot.show()
输出:
示例 2:
Python3
import plotly.graph_objects as go
import numpy
plot = go.Figure(data=[go.Scatter(
x = np.random.randn(1000),
y = np.random.randn(1000),
stackgroup='one'),
go.Scatter(
x = np.random.randn(10),
y = np.random.randn(50),
stackgroup='one')
])
plot.show()
输出:
示例 3:
Python3
import plotly.graph_objects as go
import plotly.express as px
import numpy
df = px.data.iris()
plot = go.Figure(data=[go.Scatter(
x = df['sepal_width'],
y = df['sepal_length'],
stackgroup='one'),
go.Scatter(
x = df['petal_width'],
y = df['petal_length'],
stackgroup='one')
])
plot.show()
输出: