首页 > 解决方案 > 如何让 cmap 在三个 for 循环中以可迭代的方式工作?

问题描述

我正在为每个第三个变量的每个变量对构建一组图表,例如:

pressures = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]
thicknesses = [0.5, 1.0, 1.5, 2.0]
velocities = [600.0, 700.0]

for velocity in velocities:
    for thickness in thicknesses:

        fig, ax = plt.subplots(1, 1)
        cmap = plt.get_cmap('rainbow')

        for pressure in pressures:
            colors = iter(cmap(pressure/len(pressures)))

            try:
                ax.plot(x, y, label='{} mbar'.format(pressure), color=colors) #where (x, y) are variables decided earlier in the try loop
            except TypeError:
                continue

        ax.plot()
        ax.legend()
        plt.show()

这用我想要的数据构建了一个图表,但我无法让颜色图工作。我试图让它在厚度/速度对的每次迭代中为每个压力显示相同的颜色。

标签: pythonpython-3.xpython-2.7matplotlib

解决方案


推荐阅读