Guf*_*oru 7 python plot colors dataframe pandas
我pd.DataFrame喜欢这个:
ColumnName
1
1
2
3
1
2
3
1
2
2
Run Code Online (Sandbox Code Playgroud)
我可以用它来绘制它 df['ColumnName'].plot(style='o')
如何为列中的不同值定义不同的颜色(例如,红色表示值1,绿色表示2,橙色表示3).我知道这与它有关colormap,但我如何使用它?
解决方案是DataFrame使用每个值的列构造一个new .但是这些值是排序的,我希望这个序列只是以不同的颜色着色.
Lon*_*Rob 18
要绘制数据框中的第一列,请尝试以下操作:
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()
Run Code Online (Sandbox Code Playgroud)
结果如下: