【问题标题】:Matplotlib: plotting function plt.contourf() fails to plot over 9 colors from colormap jetMatplotlib:绘图函数 plt.contourf() 无法从颜色图喷射中绘制超过 9 种颜色
【发布时间】:2019-05-30 11:38:40
【问题描述】:

我正在尝试从连续颜色图“jet”绘制具有不同颜色的垂直线,并注意到函数 plt.contourf() 无法绘制超过 9 种颜色。

这里是一个关于可以通过改变变量 num_colors 的值来控制颜色数量的例子。我在这个例子中使用了特定的值,因为我在另一个使用相同 Z 值的程序中注意到了这种现象。

import matplotlib.pyplot as plt
import numpy as np

num_colors = 10
values = [x / 10.0 for x in range(5, (10*num_colors)+5, 10)]  # [0.5, 1.5, ..., 9.5]
# create example color-map (vertical strides)
list = []
for k in range(10*num_colors):
    tmp_list = []
    for i in range(len(values)):
        for j in range(10):
            tmp_list.append(values[i])
    list.append(tmp_list)

Z = np.array(list)  
x = np.arange(0, num_colors, 0.1)
y = np.arange(0, num_colors, 0.1)
xx, yy = np.meshgrid(x, y)

plt.contourf(xx, yy, Z, cmap='jet', alpha=1.0)
plt.show()

【问题讨论】:

  • 使用plt.contourf(xx, yy, Z, levels = values, ...)

标签: python matplotlib


【解决方案1】:

尝试在contourf中定义层数:

plt.contourf(xx, yy, Z, levels=10, cmap='jet', alpha=1.0)

输出:

【讨论】:

    猜你喜欢
    • 2018-08-12
    • 2022-01-20
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    • 2013-07-19
    • 1970-01-01
    • 2015-10-02
    相关资源
    最近更新 更多