简体   繁体   English

将颜色图功能与 Pandas.DataFrame.Plot 一起使用

[英]Using Colormap feature with Pandas.DataFrame.Plot

from matplotlib import cm
a = pd.DataFrame(zip(ranFor.feature_importances_, trainSet.columns))
a = a.sort_values(by = [0], ascending= False)
tinydata = a.iloc[:25]
tinydata = tinydata[::-1]
tinydata.set_index([1], inplace=True)
cmap = cm.get_cmap('jet')
colors = cm.jet(np.linspace(0,1,len(tinydata)))
tinydata.plot(kind = 'barh', figsize = (15,10), title = 'Most Important 20 Features of the Initial Model',
                    grid = True, legend = False, color = colors)
plt.xlabel('Feature Importance')
plt.show()

Hello everyone, this is my code for plotting a bar plot.大家好,这是我绘制条形图 plot 的代码。 The problem is I couldn't figure out how to plot the colors with colormap with an increasing transparency like the graph I am attaching to my question.问题是我无法弄清楚如何 plot colors 与颜色图增加透明度,就像我附加到我的问题的图表一样。 Thank you.谢谢你。

我提到的图表

EDIT编辑

colors = cm.Reds(np.linspace(0,len(tinydata),1))
tinydata.plot(kind = 'barh', figsize = (15,10), title = 'Most Important 20 Features of the Initial Model',
                    grid = True, legend = False, color = colors)

I made a change like this and I guess it worked but the colors are really pale.我做了这样的改变,我想它有效,但 colors 真的很苍白。 How can I change this.我该如何改变这一点。

I am afraid that pandas does not provide this functionality.恐怕 pandas 不提供此功能。 Although they say in the documentation that color can take an array, this refers to different columns, as we can also see in this example:尽管他们在文档中说color可以采用数组,但这指的是不同的列,我们也可以在这个例子中看到:

from matplotlib import cm
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np

tinydata = pd.DataFrame({"ind": list("ABCDEF"), 
                         "X": [10, 8, 7, 6, 4, 1], 
                         "Y": [5,  3, 4, 2, 3, 1],
                         "Z": [8,  5, 9, 6, 7, 3] })
tinydata = tinydata[::-1].set_index("ind")
n = len(tinydata)
colors = cm.Reds(np.linspace(0.2, 0.8, 3))
tinydata.plot(kind = 'barh', figsize = (15,10), title = 'Most Important 20 Features of the Initial Model',
                    grid = True, legend = True, color = colors)
plt.xlabel('Feature Importance')
plt.show()

Output: Output: ![在此处输入图像描述

In the context of pandas providing commonly used plotting functions, this makes sense.在 pandas 提供常用绘图功能的情况下,这是有道理的。 So, for your application, it is back to the multifunctionality of matplotlib on which pandas relies anyhow:因此,对于您的应用程序,它又回到了 pandas 无论如何都依赖的matplotlib的多功能性:

from matplotlib import cm
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np

tinydata = pd.DataFrame({"ind": list("ABCDEF"), 
                         "X": [10, 8, 7, 6, 4, 1]})
tinydata = tinydata[::-1].set_index("ind")
n = len(tinydata)
colors = cm.Reds(np.linspace(0, 1, n))

fig, ax = plt.subplots(figsize = (15,10))
ax.barh(tinydata.index, tinydata.X, color = colors)
ax.grid(True)
ax.set_xlabel('Feature Importance')
ax.set_title('Most Important 20 Features of the Initial Model',)
plt.show()

Output: Output: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM