Plotly的Parcoords类可以用于绘制并行坐标图,用于可视化多个数值变量之间的关系。下面是一个简单的示例,演示如何使用Parcoords类绘制一个并行坐标图:
import plotly.express as px
df = px.data.iris()
fig = px.parallel_coordinates(df, color="species_id",
dimensions=['sepal_length', 'sepal_width', 'petal_length', 'petal_width'],
labels={'sepal_length': 'Sepal Length', 'sepal_width': 'Sepal Width',
'petal_length': 'Petal Length', 'petal_width': 'Petal Width'},
color_continuous_scale=px.colors.diverging.Tealrose,
color_continuous_midpoint=2)
fig.show()
在上面的示例中,我们首先使用plotly.express模块中的data.iris()方法加载了一个示例数据集。然后使用px.parallel_coordinates()方法创建了一个Parcoords图,并指定了数据集中的四个数值变量作为维度,并将species_id作为颜色变量。最后调用show()方法显示图表。
通过调整dimensions参数可以指定要显示的数值变量,通过调整labels参数可以指定每个数值变量的标签,通过调整color参数可以指定颜色变量,通过调整color_continuous_scale和color_continuous_midpoint参数可以设置颜色的渐变和中点。