【问题标题】:Order of plotting in Pandas.plotting.parallel_coordinatesPandas.plotting.parallel_coordinates 中的绘图顺序
【发布时间】:2020-10-05 16:54:08
【问题描述】:

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

代码如下:

... 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”的值来影响堆叠线条的顺序。到目前为止,我还没有找到方法。

【问题讨论】:

  • 我的猜测是它们可能是按照它们在数据框中出现的顺序绘制的。您是否尝试按相关列对数据框进行排序?
  • 嗨,保罗,是的,我这样做了 - 试图通过“allFrames.sort_values(column_for_line_color_derivation, inplace=True, ascending=True)”这一行来表明这一点
  • 那你为什么要绘制largeDataFrame 而不是allFrames
  • 抱歉 - 谢谢 - 这是问题描述中的错误
  • 您使用ascending=True 进行排序,这意味着将最小值移动到数据框的第一行。试试ascending=False

标签: python pandas matplotlib parallel-coordinates


【解决方案1】:

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

你可以临时添加parallel_coordinates(...., lw=5),这样就很清楚了。使用细线,顺序不太明显,因为黄线的对比度较低。

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

这是一个可重现的小例子:

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()

一个想法是根据类改变线宽:

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()

【讨论】:

  • 嗨 Johan,感谢您的帖子,我发现了错误:我必须使用“colormap="whatever map" 而不是“color=theColormap.colors”,然后输出符合预期。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-27
  • 1970-01-01
相关资源
最近更新 更多