ily*_*lya 1 python colors matplotlib subplot
我正在使用 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"])
Run Code Online (Sandbox Code Playgroud)
一切正常。 一个窗口中的图表 但我需要每个图表都位于单独的子图上。
我正在尝试这样做:
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
Run Code Online (Sandbox Code Playgroud)
我明白了。
问题是所有图表都具有相同的颜色。而传说是没有用的。如何为所有子批次添加自动颜色管理?我希望那里有不同的颜色。就像 subplot1.chart1 = color1、subplo1.chart2 = color2、sublot2.chart1 = color3,而不是 color1。
Imp*_*est 16
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"])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8756 次 |
| 最近记录: |