在Python中使用 Plotly 绘制平行坐标图
Plotly 是一个Python库,用于设计图形,尤其是交互式图形。它可以绘制各种图形和图表,如直方图、条形图、箱线图、展开图等等。它主要用于数据分析以及财务分析。 Plotly 是一个交互式可视化库。
平行坐标图
平行坐标图是可视化和分析高维数据集的常用方法。 n 维空间中的一个点表示为一条折线,顶点在平行轴上,顶点的位置对应于该点的坐标。
Syntax: parallel_coordinates(data_frame=None, dimensions=None, labels={}, range_color=None)
Parameters:
data_frame: This argument needs to be passed for column names (and not keyword names) to be used.
dimensions: Either names of columns in data_frame, or pandas Series, or array_like objects Values from these columns are used for multidimensional visualization.
labels: By default, column names are used in the figure for axis titles, legend entries and hovers. This parameter allows this to be overridden. The keys of this dict should correspond to column names, and the values should correspond to the desired label to be displayed.
range_color: If provided, overrides auto-scaling on the continuous color scale.
示例 1:
Python3
import plotly.express as px
df = px.data.tips()
fig = px.parallel_coordinates(
df, dimensions=['tip', 'total_bill', 'day','time'],)
fig.show()
Python3
import plotly.graph_objects as go
fig = go.Figure(data=go.Parcoords(
line_color='green',
dimensions=list([
dict(range=[4, 9],
label='A', values=[5, 8]),
dict(range=[2, 7],
label='B', values=[3, 6]),
])
)
)
fig.show()
Python3
import plotly.graph_objects as go
import plotly.express as px
df = px.data.tips()
fig = go.Figure(data=go.Parcoords(
dimensions=list([
dict(range=[0, 8],
constraintrange=[4, 8],
label='Sepal Length', values=df['tip']),
dict(range=[0, 8],
label='Sepal Width', values=df['total_bill']),
])
)
)
fig.show()
输出:
示例 2:使用go.Parcoords()显示平行坐标图
蟒蛇3
import plotly.graph_objects as go
fig = go.Figure(data=go.Parcoords(
line_color='green',
dimensions=list([
dict(range=[4, 9],
label='A', values=[5, 8]),
dict(range=[2, 7],
label='B', values=[3, 6]),
])
)
)
fig.show()
输出:
示例 3:
蟒蛇3
import plotly.graph_objects as go
import plotly.express as px
df = px.data.tips()
fig = go.Figure(data=go.Parcoords(
dimensions=list([
dict(range=[0, 8],
constraintrange=[4, 8],
label='Sepal Length', values=df['tip']),
dict(range=[0, 8],
label='Sepal Width', values=df['total_bill']),
])
)
)
fig.show()
输出: