【问题标题】:pandas parallel plot with different axis ranges具有不同轴范围的熊猫平行图
【发布时间】:2019-03-28 23:57:47
【问题描述】:

我必须绘制一些具有不同范围的数据集的平行图。当我用谷歌搜索时,我在this website 中找到了一个漂亮的 javascript 示例。

我已经为测试创建了一些示例数据集,并希望实现具有 yxis-ticks不同范围的 yaxes 类似于此图像的平行图:

到目前为止,我已经这样做了:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pandas.plotting import parallel_coordinates
np.random.seed(100)

%matplotlib inline

df = pd.DataFrame({'calcium': np.random.randint(0,7,5),
                  'calories': np.random.randint(200,900,5),
                 'fiber': np.random.randint(10,75,5),
                'potassium': np.random.randint(0,20,5)
                  })
df = df.T
df['name'] = df.index

df.reset_index(drop=True)

parallel_coordinates(df,'name')

输出是这样的:

正如我们所见,底部曲线非常难以辨认。我想解决这个问题。 我用谷歌搜索并试图找到如何更改垂直 y 轴刻度线和更改范围(标准化)。

我们将不胜感激。 这是一个美丽的情节,向地球上成功用python可视化这个美丽情节的人致敬!

相关链接:
http://bl.ocks.org/syntagmatic/raw/3150059/
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.plotting.parallel_coordinates.html
https://pandas.pydata.org/pandas-docs/stable/visualization.html
How to plot parallel coordinates on pandas DataFrame with some columns containing strings?

更新

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pandas.plotting import parallel_coordinates
np.random.seed(100)

plt.style.use('ggplot')
%matplotlib inline

df = pd.DataFrame({'calcium': np.random.randint(0,7,5),
                   'calories': np.random.randint(200,900,5),
                   'fiber': np.random.randint(10,75,5),
                   'potassium': np.random.randint(0,20,5),
                   'name': ['apple','banana','orange','mango','watermelon']

                  })
ax = parallel_coordinates(df,'name')
ax.grid(True)
ax.set_yscale('log')

仍然无法在中间轴上放置 ytick 标记。

【问题讨论】:

  • 您是否考虑过使用对数 y 轴?当值的范围太宽而无法在线性轴上绘制时,它很有用。
  • 是的,这会加宽第一个 y 轴刻度标签,但要为所有其他轴提供更多刻度。我的目标是从 blocks.org 中获取情节
  • 但是您将所有元素都绘制在一个图中,它们使用多个子图。您是否在询问如何绘制具有不同 y 轴范围的多个图?
  • 也许我的措辞不同,请看问题中的第一张图片,无论他们采用什么方法来获得那张漂亮的图片,我都在努力获得像他们一样的形象。 ax.set_yscale('log') 也是个好主意。感谢您的建议。
  • 我不想成为一个抱怨者,但是你作为例子展示的情节被重载了一个不是很有用的恕我直言。仅仅因为它看起来很酷,并不意味着它是一个内容丰富的情节。

标签: python pandas matplotlib visualization


【解决方案1】:

这是一个解决方案,将有助于使用损坏的 y 轴提高可读性。我从here 窃取了大部分代码。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(100)

%matplotlib inline

df = pd.DataFrame({'calcium': np.random.randint(0,7,5),
              'calories': np.random.randint(200,900,5),
             'fiber': np.random.randint(10,75,5),
            'potassium': np.random.randint(0,20,5)
              })

f, (ax, ax2) = plt.subplots(2, 1, sharex=True)

#plot the same data on both axes
ax.plot(df)
ax2.plot(df)

# zoom-in / limit the view to different portions of the data
ax.set_ylim(250, 800)  # outliers only
ax2.set_ylim(0, 75)  # most of the data

# hide the spines between ax and ax2
ax.spines['bottom'].set_visible(False)
ax2.spines['top'].set_visible(False)
ax.xaxis.tick_top()
ax.tick_params(labeltop='off')  # don't put tick labels at the top
ax2.xaxis.tick_bottom()

d = .015  # how big to make the diagonal lines in axes coordinates
kwargs = dict(transform=ax.transAxes, color='k', clip_on=False)
ax.plot((-d, +d), (-d, +d), **kwargs)        # top-left diagonal
ax.plot((1 - d, 1 + d), (-d, +d), **kwargs)  # top-right diagonal

kwargs.update(transform=ax2.transAxes)  # switch to the bottom axes
ax2.plot((-d, +d), (1 - d, 1 + d), **kwargs)  # bottom-left diagonal
ax2.plot((1 - d, 1 + d), (1 - d, 1 + d), **kwargs)  # bottom-right diagonal


f.subplots_adjust(left=0.1, right=1.6, 
              bottom=0.1, top = 0.9, 
              hspace=0.3) # space between the two sections
f.legend(df.columns)

plt.show()

这会产生如下所示的情节:

我仍然认为钙线很难解释,但如果图表足够简单,可以分解成块,你可以放大图像或再次破坏 y 轴。

【讨论】:

  • f.legend(labels=df.columns) TypeError: legend() missing 1 required positional argument: 'handles'
  • 我在 matplotlib 2.2.2 上,你可以通过在 df.columns 中用 for col 替换 ax.plot(df) 来破解: ax.plot(df[col], label=col)并将 f.legend(df.columns) 替换为 f.legend()
猜你喜欢
  • 1970-01-01
  • 2022-01-18
  • 2021-05-07
  • 2020-04-04
  • 2021-06-15
  • 2019-11-09
  • 2019-01-19
  • 2019-04-15
  • 2022-08-09
相关资源
最近更新 更多