Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用colormaps为数据帧绘制颜色图_Python_Colors_Plot_Pandas_Dataframe - Fatal编程技术网

Python 如何使用colormaps为数据帧绘制颜色图

Python 如何使用colormaps为数据帧绘制颜色图,python,colors,plot,pandas,dataframe,Python,Colors,Plot,Pandas,Dataframe,我有一个像这样的pd.DataFrame: ColumnName 1 1 2 3 1 2 3 1 2 2 我可以用df['ColumnName']打印它。打印(style='o') 如何为列中的不同值定义不同的颜色(例如,红色表示值1,绿色表示值2,橙色表示值3)。我知道这和你有关系,但我怎么用呢 解决方案是用每个值的列构造一个新的数据帧。但是这些值是经过排序的,我想让这个序列正好以不同的颜色着色。要从数据帧中绘制第一列,请尝试以下操作: from matplotlib import cm i

我有一个像这样的
pd.DataFrame

ColumnName
1
1
2
3
1
2
3
1
2
2
我可以用
df['ColumnName']打印它。打印(style='o')

如何为列中的不同值定义不同的颜色(例如,红色表示值1,绿色表示值2,橙色表示值3)。我知道这和你有关系,但我怎么用呢


解决方案是用每个值的列构造一个新的
数据帧。但是这些值是经过排序的,我想让这个序列正好以不同的颜色着色。

要从数据帧中绘制第一列,请尝试以下操作:

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()
其结果是: