【发布时间】: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