我使用matplotlib来创建图。我必须用不同的颜色来标识每个图,这应该由Python自动生成。
你能不能给我一个方法,在同一个图里不同的图用不同的颜色?
我使用matplotlib来创建图。我必须用不同的颜色来标识每个图,这应该由Python自动生成。
你能不能给我一个方法,在同一个图里不同的图用不同的颜色?
当前回答
老实说,我最喜欢的方法很简单:现在这对任意数量的地块都不适用,但它可以满足你到1163的需求。这是通过使用所有matplotlib的命名颜色的映射,然后随机选择它们。
from random import choice
import matplotlib.pyplot as plt
from matplotlib.colors import mcolors
# Get full named colour map from matplotlib
colours = mcolors._colors_full_map # This is a dictionary of all named colours
# Turn the dictionary into a list
color_lst = list(colours.values())
# Plot using these random colours
for n, plot in enumerate(plots):
plt.scatter(plot[x], plot[y], color=choice(color_lst), label=n)
其他回答
老实说,我最喜欢的方法很简单:现在这对任意数量的地块都不适用,但它可以满足你到1163的需求。这是通过使用所有matplotlib的命名颜色的映射,然后随机选择它们。
from random import choice
import matplotlib.pyplot as plt
from matplotlib.colors import mcolors
# Get full named colour map from matplotlib
colours = mcolors._colors_full_map # This is a dictionary of all named colours
# Turn the dictionary into a list
color_lst = list(colours.values())
# Plot using these random colours
for n, plot in enumerate(plots):
plt.scatter(plot[x], plot[y], color=choice(color_lst), label=n)
稍后再设置
如果你不知道你要绘制的图的数量,你可以改变颜色,一旦你绘制了它们,使用.lines直接从图中检索数字,我使用这个解决方案:
一些随机数据
import matplotlib.pyplot as plt
import numpy as np
fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
for i in range(1,15):
ax1.plot(np.array([1,5])*i,label=i)
你需要的一段代码:
colormap = plt.cm.gist_ncar #nipy_spectral, Set1,Paired
colors = [colormap(i) for i in np.linspace(0, 1,len(ax1.lines))]
for i,j in enumerate(ax1.lines):
j.set_color(colors[i])
ax1.legend(loc=2)
结果如下:
I would like to offer a minor improvement on the last loop answer given in the previous post (that post is correct and should still be accepted). The implicit assumption made when labeling the last example is that plt.label(LIST) puts label number X in LIST with the line corresponding to the Xth time plot was called. I have run into problems with this approach before. The recommended way to build legends and customize their labels per matplotlibs documentation ( http://matplotlib.org/users/legend_guide.html#adjusting-the-order-of-legend-item) is to have a warm feeling that the labels go along with the exact plots you think they do:
...
# Plot several different functions...
labels = []
plotHandles = []
for i in range(1, num_plots + 1):
x, = plt.plot(some x vector, some y vector) #need the ',' per ** below
plotHandles.append(x)
labels.append(some label)
plt.legend(plotHandles, labels, 'upper left',ncol=1)
**: Matplotlib传说不工作
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from skspatial.objects import Line, Vector
for count in range(0,len(LineList),1):
Line_Color = np.random.rand(3,)
Line(StartPoint,EndPoint)).plot_3d(ax,c="Line"+str(count),label="Line"+str(count))
plt.legend(loc='lower left')
plt.show(block=True)
上面的代码可以帮助你以随机的方式添加不同颜色的3D线条。你的彩色线条也可以通过标签="…"中提到的图例来引用。”参数。
Matplot用不同的颜色给你的场景上色,但如果你想放特定的颜色
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(10)
plt.plot(x, x)
plt.plot(x, 2 * x,color='blue')
plt.plot(x, 3 * x,color='red')
plt.plot(x, 4 * x,color='green')
plt.show()