Python 3.x 如何将python调色板转换为给定大小的颜色数组?
它需要用函数填充相互上方的10个图形之间的空间Python 3.x 如何将python调色板转换为给定大小的颜色数组?,python-3.x,matplotlib,Python 3.x,Matplotlib,它需要用函数填充相互上方的10个图形之间的空间 plt.fill_between(x, y1, y2, color= 'here_is_color', alpha=0.5) 颜色应该改变,例如,从上到下从红色到蓝色。为此,我认为您可以将给定的调色板转换为颜色数组,并在循环中使用它,直到填充图形之间的空间 我分享我找到的解决方案:) 接下来,根据需要更改循环中的num计数器,函数如下所示 plt.fill_between(x, y1, y2, color=col(num)) 可以使用为matp
plt.fill_between(x, y1, y2, color= 'here_is_color', alpha=0.5)
颜色应该改变,例如,从上到下从红色到蓝色。为此,我认为您可以将给定的调色板转换为颜色数组,并在循环中使用它,直到填充图形之间的空间
我分享我找到的解决方案:) 接下来,根据需要更改循环中的num计数器,函数如下所示
plt.fill_between(x, y1, y2, color=col(num))
可以使用为matplit库定义的调色板:
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import numpy as np
获取颜色并按hsv排序(或按您想要的顺序提供名称-请参阅
为堆叠的y
创建一些演示数据:
def get_y_data():
y1,y2,y3,y4,y5,y6 = np.random.rand(6,20)
y2 += y1 + 0.01
y3 += y2 + 0.01
y4 += y3 + 0.01
y5 += y4 + 0.01
y6 += y5 + 0.01
return y1,y2,y3,y4,y5,y6
把它插在一起:
# get the color-names from above or supply your own names:
cols_to_use = get_hsv_colors()[20::5] # skip the greys, only take every 5th name
# zipp the data with the corresponding color
matched_y_data = list( zip((y1,y2,y3,y4,y5,y6), cols_to_use))
# plot data with lines
for (y,c) in matched_y_data:
plt.plot(x, y, color=c)
plt.show()
# plot the same using fill_between
fig, ax1 = plt.subplots(1, 1, sharex=True, figsize=(6, 6))
# base line y -> 0:
ax1.fill_between(x, matched_y_data[0][0], 0, color=matched_y_data[0][1])
# intermediate lines y_n -> y_n+1:
for (idx, (y_data, col)) in enumerate(matched_y_data[:-1]):
ax1.fill_between(x, y_data, matched_y_data[idx+1][0], color=col)
# last line
mm = max( matched_y_data[-1][0] ) + 0.1
ax1.fill_between(x, matched_y_data[-1][0], mm, color=matched_y_data[-1][1])
ax1.set_xlabel('x')
fig.tight_layout()
plt.show()
得到
及
您可以通过仔细选择颜色名称来获得配色方案:
# zipp the data with the corresponding color
matched_y_data = list(
zip((y1,y2,y3,y4,y5,y6),
"royalblue cornflowerblue lightsteelblue mistyrose lightsalmon tomato".split()))
@PatrickArtner抱歉,我是新来的,我在任何地方都找不到这个问题的答案。更准确地说,它需要用前面指定的函数填充10个图形之间的空间。颜色应该改变,例如,从上到下从绿色到黄色。要做到这一点,我认为您可以创建创建一个颜色数组并在循环中使用它,它填充了图形之间的空间。您可能希望在帖子中提供您获得的图形和所需内容的最小示例:o)@PatrickArtner好的,完成:-)非常感谢!
# get the color-names from above or supply your own names:
cols_to_use = get_hsv_colors()[20::5] # skip the greys, only take every 5th name
# zipp the data with the corresponding color
matched_y_data = list( zip((y1,y2,y3,y4,y5,y6), cols_to_use))
# plot data with lines
for (y,c) in matched_y_data:
plt.plot(x, y, color=c)
plt.show()
# plot the same using fill_between
fig, ax1 = plt.subplots(1, 1, sharex=True, figsize=(6, 6))
# base line y -> 0:
ax1.fill_between(x, matched_y_data[0][0], 0, color=matched_y_data[0][1])
# intermediate lines y_n -> y_n+1:
for (idx, (y_data, col)) in enumerate(matched_y_data[:-1]):
ax1.fill_between(x, y_data, matched_y_data[idx+1][0], color=col)
# last line
mm = max( matched_y_data[-1][0] ) + 0.1
ax1.fill_between(x, matched_y_data[-1][0], mm, color=matched_y_data[-1][1])
ax1.set_xlabel('x')
fig.tight_layout()
plt.show()
# zipp the data with the corresponding color
matched_y_data = list(
zip((y1,y2,y3,y4,y5,y6),
"royalblue cornflowerblue lightsteelblue mistyrose lightsalmon tomato".split()))