首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pandas.plotting.parallel_coordinates中的绘图顺序

Pandas.plotting.parallel_coordinates中的绘图顺序
EN

Stack Overflow用户
提问于 2020-10-06 00:54:09
回答 1查看 383关注 0票数 0

我有一系列要绘制为pandas.plotting.parallel_coordinates的测量值,其中每条线的颜色由一个pandas.column的值给出。

代码如下所示:

代码语言:javascript
复制
... data retrieval and praparation from a couple of Excel files
---> output = 'largeDataFrame'

theColormap: ListedColormap = cm.get_cmap('some cmap name')

# This is a try to stack the lines in the right order.. (doesn't work)
largeDataFrames.sort_values(column_for_line_color_derivation, inplace=True, ascending=True)

# here comes the actual plotting of data
sns.set_style('ticks')
sns.set_context('paper')
plt.figure(figsize=(10, 6))
thePlot: plt.Axes = parallel_coordinates(largeDataFrame, class_column=column_for_line_color_derivation, cols=[columns to plot], color=theColormap.colors)
plt.title('My Title')
thePlot.get_legend().remove()
plt.xticks(rotation=90)
plt.tight_layout()
plt.show()

这很好地工作,并产生以下结果:

现在,我希望在绿色和深色线条前面绘制黄色线条(“column_for_line_color_derivation”的高值),这样它们就会变得更加突出。换句话说,我希望通过"column_for_line_color_derivation“的值来影响行的堆叠顺序。到目前为止,我还没有找到这样做的方法。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-06 02:26:26

我使用pandas版本1.1.2和1.0.3运行了一些测试,在这两种情况下,线条都是从着色列的低值到高值绘制的,与数据帧顺序无关。

您可以临时添加parallel_coordinates(...., lw=5),这使它变得非常清晰。对于细线,顺序不太明显,因为黄线的对比度较小。

参数sort_labels=似乎具有与其名称相反的效果:当为False (默认值)时,按排序顺序绘制线条,当为True时,它们保持数据帧顺序。

下面是一个可重复使用的小示例:

代码语言:javascript
复制
import numpy as np
import pandas as pd
from pandas.plotting import parallel_coordinates
import matplotlib.pyplot as plt

df = pd.DataFrame({ch: np.random.randn(100) for ch in 'abcde'})
df['coloring'] = np.random.randn(len(df))

fig, axes = plt.subplots(ncols=2, figsize=(14, 6))
for ax, lw in zip(axes, [1, 5]):
    parallel_coordinates(df, class_column='coloring', cols=df.columns[:-1], colormap='viridis', ax=ax, lw=lw)
    ax.set_title(f'linewidth={lw}')
    ax.get_legend().remove()
plt.show()

一种想法是根据类的不同改变线宽:

代码语言:javascript
复制
fig, ax = plt.subplots(figsize=(8, 6))

parallel_coordinates(df, class_column='coloring', cols=df.columns[:-1], colormap='viridis', ax=ax)
num_lines = len(ax.lines)
for ind, line in enumerate(ax.lines):
    xs = line.get_xdata()
    if xs[0] != xs[-1]:  # skip the vertical lines representing axes
        line.set_linewidth(1 + 3 * ind / num_lines)
ax.set_title(f'linewidth depending on class_column')
ax.get_legend().remove()
plt.show()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64212979

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档