python - 具有不同轴范围的 Pandas 平行图

标签 python pandas matplotlib visualization

我必须绘制一些具有不同范围的数据集的平行图。当我用谷歌搜索时,我在 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')

enter image description here

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

最佳答案

这是一个解决方案,有助于使用断开的 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()

生成如下图: enter image description here

我仍然认为钙线很难解释,但如果图形足够简单可以分解成 block ,您可以放大图像或再次打断 y 轴。

关于python - 具有不同轴范围的 Pandas 平行图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52971188/

相关文章:

python - 如何在 python 中导入 matplotlib

python - 使用 Pandas 创建/重命名类别

python - 如何在Python中设置类属性

python - python pandas 需要帮助来减少代码行数和周期时间

python - 在函数中定义类是一种可接受的模式吗?

python - 控制对数刻度的刻度间距

python - 如何在 ubuntu 14.04 服务器中为 Python 3.5 使用 pip3 安装包

python - 检查它是否是一个快乐的数字

python - pandas python 中的 VLOOKUP Excel 模拟

python - Pandas 中的 Excel 公式计算