Matplotlib 改变图形颜色
Matplotlib change graph colors
我想更改下一张图中的颜色。我需要黄色为绿色,绿色为红色,紫色为蓝色。
我有下一个代码:
fig = plt.figure()
ax = fig.add_subplot(111)
scatter = ax.scatter( kmeans['Revenue'], kmeans['Frequency'],c=kmeans['Cluster'], s=15)
ax.set_xlabel('Recency')
ax.set_ylabel('Frequency')
plt.colorbar(scatter)
如何更改颜色?
如果您阅读 scatter 的文档,那么您会看到 colormap
的选项 cmap
。
如果你使用 Google matplotlib scatter colormap
那么你会发现 Choosing Colormaps in Matplotlib
它显示了预定义的颜色图,您似乎需要 brg
。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
random = np.random.sample((100,3))
kmeans = pd.DataFrame(random, columns=['Revenue', 'Frequency', 'Cluster'])
fig = plt.figure()
ax = fig.add_subplot(111)
scatter = ax.scatter( kmeans['Revenue'], kmeans['Frequency'], c=kmeans['Cluster'], s=15, cmap='brg')
ax.set_xlabel('Recency')
ax.set_ylabel('Frequency')
plt.colorbar(scatter)
plt.show()
我想更改下一张图中的颜色。我需要黄色为绿色,绿色为红色,紫色为蓝色。
我有下一个代码:
fig = plt.figure()
ax = fig.add_subplot(111)
scatter = ax.scatter( kmeans['Revenue'], kmeans['Frequency'],c=kmeans['Cluster'], s=15)
ax.set_xlabel('Recency')
ax.set_ylabel('Frequency')
plt.colorbar(scatter)
如何更改颜色?
如果您阅读 scatter 的文档,那么您会看到 colormap
的选项 cmap
。
如果你使用 Google matplotlib scatter colormap
那么你会发现 Choosing Colormaps in Matplotlib
它显示了预定义的颜色图,您似乎需要 brg
。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
random = np.random.sample((100,3))
kmeans = pd.DataFrame(random, columns=['Revenue', 'Frequency', 'Cluster'])
fig = plt.figure()
ax = fig.add_subplot(111)
scatter = ax.scatter( kmeans['Revenue'], kmeans['Frequency'], c=kmeans['Cluster'], s=15, cmap='brg')
ax.set_xlabel('Recency')
ax.set_ylabel('Frequency')
plt.colorbar(scatter)
plt.show()