Skip to main content
 首页 » 编程设计

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

2024年12月31日6itcoder

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

生成如下图:

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