[英]Removing legend from mpl parallel coordinates plot?
I have a parallel coordinates plot with lots of data points so I'm trying to use a continuous colour bar to represent that, which I think I have worked out.我有一个平行坐标 plot 有很多数据点,所以我试图用一个连续的颜色条来表示它,我想我已经解决了。 However, I haven't been able to remove the default key that is put in when creating the plot, which is very long and hinders readability.但是,我无法删除在创建 plot 时放入的默认密钥,该密钥非常长并且妨碍了可读性。 Is there a way to remove this table to make the graph much easier to read?有没有办法删除此表以使图表更易于阅读?
This is the code I'm currently using to generate the parallel coordinates plot:这是我目前用来生成平行坐标 plot 的代码:
parallel_coordinates(data[[' male_le','
female_le','diet','activity','obese_perc','median_income']],'median_income',colormap = 'rainbow',
alpha = 0.5)
fig, ax = plt.subplots(figsize=(6, 1))
fig.subplots_adjust(bottom=0.5)
cmap = mpl.cm.rainbow
bounds = [0.00,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N,)
plt.colorbar(mpl.cm.ScalarMappable(norm = norm, cmap=cmap),cax = ax, orientation = 'horizontal',
label = 'normalised median income', alpha = 0.5)
plt.show()
Current Output:当前 Output:
I want my legend to be represented as a color bar, like this:我希望我的图例表示为一个彩条,如下所示:
Any help would be greatly appreciated.任何帮助将不胜感激。 Thanks.谢谢。
You can use ax.legend_.remove()
to remove the legend.您可以使用ax.legend_.remove()
删除图例。
The cax
parameter of plt.colorbar
indicates the subplot where to put the colorbar. plt.colorbar 的cax
参数指示放置plt.colorbar
的子图。 If you leave it out, matplotlib will create a new subplot, "stealing" space from the current subplot (subplots are often referenced to by ax
in matplotlib).如果你忽略它,matplotlib 将创建一个新的子图,从当前子图中“窃取”空间(子图通常由 matplotlib 中的ax
引用)。 So, here leaving out cax
(adding ax=ax
isn't necessary, as here ax
is the current subplot) will create the desired colorbar.因此,这里省略cax
(添加ax=ax
不是必需的,因为这里ax
是当前子图)将创建所需的颜色条。
The code below uses seaborn's penguin dataset to create a standalone example.下面的代码使用 seaborn 的企鹅数据集来创建一个独立的示例。
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sns
import numpy as np
from pandas.plotting import parallel_coordinates
penguins = sns.load_dataset('penguins')
fig, ax = plt.subplots(figsize=(10, 4))
cmap = plt.get_cmap('rainbow')
bounds = np.arange(penguins['body_mass_g'].min(), penguins['body_mass_g'].max() + 200, 200)
norm = mpl.colors.BoundaryNorm(bounds, 256)
penguins = penguins.dropna(subset=['body_mass_g'])
parallel_coordinates(penguins[['bill_length_mm', 'bill_depth_mm', 'flipper_length_mm', 'body_mass_g']],
'body_mass_g', colormap=cmap, alpha=0.5, ax=ax)
ax.legend_.remove()
plt.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=cmap),
ax=ax, orientation='horizontal', label='body mass', alpha=0.5)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.