如何在带有子图的 matplotlib 中实现自动颜色更改?
How to implement automatic color change in matplotlib with subplots?
我正在用 matplotlib 绘制图表。当我用这段代码在同一张图表上绘制它时:
def draw0(x, ys, labels):
plt.suptitle("big title")
i =0
for y in ys:
plt.plot(x, y, label=labels[i])
plt.scatter(x, y) # dots
plt.xticks(range(1, max(x) + 1))
plt.grid(True)
i+=1
plt.figlegend(loc="upper left")
plt.show()
return
x = [1,2,3,4,5]
y1 = [1,3,5,7,9]
y2 = [10,30,50,70,90]
y3 = [0.1,0.3,0.5,0.7,0.9]
draw0(x, [y1, y2, y3], ["chart1", "chart2", "chart3"])
一切正常。
charts in one window
但我需要每个图表都位于单独的子图中。
我正在尝试这样做:
def draw11(x, ys, labels):
plt.figure()
plt.suptitle("big title")
i =0
for y in ys:
if i == 0:
ax = plt.subplot(len(ys),1, i+1)
else:
plt.subplot(len(ys), 1, i + 1, sharex=ax)
plt.plot(x, y, label=labels[i])
plt.scatter(x, y) # dots
plt.xticks(range(1, max(x) + 1))
plt.grid(True)
i+=1
plt.figlegend(loc="upper left")
plt.show()
return
我明白了。
charts in sublots
问题是所有图表都具有相同的颜色。传说是无用的。
我如何为所有子批次添加 自动 颜色管理?我希望那里没有相同的颜色。
比如subplot1.chart1 = color1, subplo1.chart2 = color2, sublot2.chart1 = color3, 不是color1.
只需要一个颜色列表和 select 每个 plt.plot
和 plt.scatter
。
colors = ['orange', 'cyan', 'green']
i =0
for y in ys:
if i == 0:
ax = plt.subplot(len(ys),1, i+1)
else:
plt.subplot(len(ys), 1, i + 1, sharex=ax)
plt.plot(x, y, label=labels[i], c=colors[i])
plt.scatter(x, y, c=colors[i]) # dots
plt.xticks(range(1, max(x) + 1))
plt.grid(True)
i+=1
Matplotlib 有一个内置的 属性 循环器,默认情况下有 10 种颜色可以循环。但是,这些是按轴循环的。如果您想循环遍历子图,则需要使用循环器并从中为每个子图获取新颜色。
import matplotlib.pyplot as plt
colors = plt.rcParams["axes.prop_cycle"]()
def draw11(x, ys, labels):
fig, axes = plt.subplots(nrows=len(ys), sharex=True)
fig.suptitle("big title")
for ax, y, label in zip(axes.flat, ys, labels):
# Get the next color from the cycler
c = next(colors)["color"]
ax.plot(x, y, label=label, color=c)
ax.scatter(x, y, color=c) # dots
ax.set_xticks(range(1, max(x) + 1))
ax.grid(True)
fig.legend(loc="upper left")
plt.show()
x = [1,2,3,4,5]
y1 = [1,3,5,7,9]
y2 = [10,30,50,70,90]
y3 = [0.1,0.3,0.5,0.7,0.9]
draw11(x, [y1, y2, y3], ["chart1", "chart2", "chart3"])
我正在用 matplotlib 绘制图表。当我用这段代码在同一张图表上绘制它时:
def draw0(x, ys, labels):
plt.suptitle("big title")
i =0
for y in ys:
plt.plot(x, y, label=labels[i])
plt.scatter(x, y) # dots
plt.xticks(range(1, max(x) + 1))
plt.grid(True)
i+=1
plt.figlegend(loc="upper left")
plt.show()
return
x = [1,2,3,4,5]
y1 = [1,3,5,7,9]
y2 = [10,30,50,70,90]
y3 = [0.1,0.3,0.5,0.7,0.9]
draw0(x, [y1, y2, y3], ["chart1", "chart2", "chart3"])
一切正常。 charts in one window 但我需要每个图表都位于单独的子图中。
我正在尝试这样做:
def draw11(x, ys, labels):
plt.figure()
plt.suptitle("big title")
i =0
for y in ys:
if i == 0:
ax = plt.subplot(len(ys),1, i+1)
else:
plt.subplot(len(ys), 1, i + 1, sharex=ax)
plt.plot(x, y, label=labels[i])
plt.scatter(x, y) # dots
plt.xticks(range(1, max(x) + 1))
plt.grid(True)
i+=1
plt.figlegend(loc="upper left")
plt.show()
return
我明白了。
charts in sublots
问题是所有图表都具有相同的颜色。传说是无用的。 我如何为所有子批次添加 自动 颜色管理?我希望那里没有相同的颜色。 比如subplot1.chart1 = color1, subplo1.chart2 = color2, sublot2.chart1 = color3, 不是color1.
只需要一个颜色列表和 select 每个 plt.plot
和 plt.scatter
。
colors = ['orange', 'cyan', 'green']
i =0
for y in ys:
if i == 0:
ax = plt.subplot(len(ys),1, i+1)
else:
plt.subplot(len(ys), 1, i + 1, sharex=ax)
plt.plot(x, y, label=labels[i], c=colors[i])
plt.scatter(x, y, c=colors[i]) # dots
plt.xticks(range(1, max(x) + 1))
plt.grid(True)
i+=1
Matplotlib 有一个内置的 属性 循环器,默认情况下有 10 种颜色可以循环。但是,这些是按轴循环的。如果您想循环遍历子图,则需要使用循环器并从中为每个子图获取新颜色。
import matplotlib.pyplot as plt
colors = plt.rcParams["axes.prop_cycle"]()
def draw11(x, ys, labels):
fig, axes = plt.subplots(nrows=len(ys), sharex=True)
fig.suptitle("big title")
for ax, y, label in zip(axes.flat, ys, labels):
# Get the next color from the cycler
c = next(colors)["color"]
ax.plot(x, y, label=label, color=c)
ax.scatter(x, y, color=c) # dots
ax.set_xticks(range(1, max(x) + 1))
ax.grid(True)
fig.legend(loc="upper left")
plt.show()
x = [1,2,3,4,5]
y1 = [1,3,5,7,9]
y2 = [10,30,50,70,90]
y3 = [0.1,0.3,0.5,0.7,0.9]
draw11(x, [y1, y2, y3], ["chart1", "chart2", "chart3"])