我有一系列要绘制为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“的值来影响行的堆叠顺序。到目前为止,我还没有找到这样做的方法。
发布于 2020-10-06 02:26:26
我使用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()

https://stackoverflow.com/questions/64212979
复制相似问题