不同轴距的Pandas平行图

2024-03-29 04:52:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须绘制一些范围不同的数据集的平行图。当我在google上搜索时,我在this website中发现了一个漂亮的javascript示例。在

我为测试创建了一些样本数据集,并希望实现具有yxis ticks不同范围yax的平行图,类似于此图:

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

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

输出如下:

如我们所见,底部曲线高度不可分辨。我想把它修好。 我在google上搜索,试图找到如何更改垂直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?

更新

^{pr2}$

enter image description here

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


Tags: 数据orgimportpandasdfparallelmatplotlibas
1条回答
网友
1楼 · 发布于 2024-03-29 04:52:51

这是一个的解决方案,它将有助于使用损坏的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

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

相关问题 更多 >