python - 如何用 pandas 和 matplotlib 绘制两个分类(名义)系列之间的比较

标签 python matplotlib pandas classification dataframe

我正在尝试比较一系列元素的两种不同分类。假设我想获得这样的图形: Comparison between two classification of N element (on y axis)

通过以下 iPython session 获得:

In [1]: df
Out[1]: 
<class 'pandas.core.frame.DataFrame'>
Index: 446 entries, element0 to element445
Data columns (total 2 columns):
Classification1       446  non-null values
Classification2    446  non-null values
dtypes: int64(2)

In [2]: pylab.pcolor(df, cmap='Oranges')
Out[2]: <matplotlib.collections.PolyCollection at 0x55dc650>

In [3]: pl.show()

1)如何查看两个分类中相同的值是否使用相同的颜色? IE。我如何测试在两个分类中具有相同颜色的 element1 是否也具有相同的值(例如,在 Classification1 和 Classification2 中都被分类为 1)? 我尝试使用 pl.legend() 但它告诉我:

In [54]: pl.legend()
/usr/lib/pymodules/python2.7/matplotlib/axes.py:4486: UserWarning: No labeled objects found. Use label='...' kwarg on individual plots.
  warnings.warn("No labeled objects found. "

2) 是否有更好的方法以图形方式比较这两个分类?

谢谢

最佳答案

我觉得这个比你之前的回答更符合你的要求

levels = [0.0, 1.0, 2.0]
a = np.random.choice(levels, size=10)
a_array = np.vstack((a, a)).T

b = np.random.choice(levels, size=10)
b_array = np.vstack((b, b)).T

ax = plt.subplot(121)
CS = ax.contourf(a_array, 2, level=levels, colors=('r', 'g', 'b'))

ax = plt.subplot(122)
CS = ax.contourf(b_array, 2, level=levels, colors=('r', 'g', 'b'))

Cbar = plt.colorbar(CS)

Cbar.set_ticks(levels)
Cbar.set_ticklabels(["series 1", "series 2", "series 3"])

enter image description here

据我所知,pcolor 无法以这种方式设置标签。尽管如果可以的话,q 会是一个更好的方法

关于python - 如何用 pandas 和 matplotlib 绘制两个分类(名义)系列之间的比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18848991/

相关文章:

python - 100 步 100 名随机游走者的平均值

python - Pandas — 在 MultiIndex 上使用部分切片设置值

python - 在 Django Rest Framework 验证方法中添加字段错误?

类外的Python classmethod

python - 类型错误 : 'DataFrame' object is not callable

python - 值错误: year is out of range pyplot

python - 数据帧 : Group separate subcolumns with identical column names together

python - 使用部分映射更新 pandas DataFrame 的列

python - 使用 selenium 抓取页面链接总是返回有限数量的链接

python - 建立分层评论树?