如何使用色彩映射绘制Pandas DataFrames的图 [英] How to use colormaps to color plots of Pandas DataFrames

查看:923
本文介绍了如何使用色彩映射绘制Pandas DataFrames的图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 pd.DataFrame 像这样:

ColumnName
1
1
2
3
1
2
3
1
2
2



我可以用 df ['ColumnName'] style ='o')

如何为列中的不同值定义不同的颜色为2,橙色为3)。我知道它与

How I can define different colors for the different values in the column (for example red for value 1, green for 2, orange for 3). I know it has to do with colormap, but how I can use it?

一个解决方案是构造一个新的 DataFrame / code>与每个值的列。但是这些值被排序,我想让这个序列只有不同的颜色。

An solution is to construct a new DataFrame with the columns of every value. But these values are sorted and I want have exactly this sequence just colored in the different colors.

推荐答案

您的数据框架,尝试这样:

To plot the first column from your dataframe, try something like this:

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

df = pd.DataFrame(np.random.randint(20, size=20))
cmap = cm.get_cmap('Spectral') # Colour map (there are many others)

fig, ax = plt.subplots(1)
# Now here's the plot. range(len(df)) just makes the x values 1, 2, 3...
# df[0] is then the y values. c sets the colours (same as y values in this
# case). s is the marker size.
ax.scatter(range(len(df)), df[0], c=df[0], s=120, cmap=cmap, edgecolor='None')
plt.show()

其结果是:

这篇关于如何使用色彩映射绘制Pandas DataFrames的图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆