Градиентная заливка и переход цвета в matplotlib - PullRequest
0 голосов
/ 26 мая 2018

Image example

В примере на рисунке у меня есть две неокрашенные области.Я хотел бы построить плавный переход цвета при переходе между двумя регионами.Например, я хотел бы начать с желтого в самой левой части незакрашенной области, затем медленно переходить к оранжевому, с самой сильной оранжевой интенсивностью в правой части рисунка.

Есть ли какие-либоКак это можно сделать?Я не уверен, что это можно сделать с помощью fill_between.

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
matplotlib.rcParams['font.sans-serif'] = "Arial"
matplotlib.rcParams['font.family'] = "sans-serif"

#points for the three lines
x_1=[0.00,1.00,2.00,3.00,4.00,5.00,6.00,6.50]
y_1=[3.00,2.80,2.40,2.20,1.80,1.00,0.20,0.00]

x_2 = [0.00,1.00,2.00,2.80]
y_2 = [7.00,8.00,9.00,10.00]

x_3=[2.80,2.80,3.00,4.00]
y_3=[10.00,6.00,4.00,1.80]

#create comparison arrays for fill_between
y_1_compare=[]
for item in x_1:
    y_1_compare.append(0)

y_2_compare=[]
for item in x_2:
    y_2_compare.append(10)

f=plt.figure(figsize=(5,5))
ax=plt.gca()
plt.plot(x_1,y_1,'o',linestyle='-', color='k',linewidth=0.7)
plt.plot(x_2,y_2,'s',linestyle='-',color='k',linewidth=0.7)
plt.plot(x_3,y_3,'^',linestyle='--',color='k',linewidth=0.7)
ax.set_xlim(0,8)
ax.set_ylim(0,10)
ax.fill_between(x_1, y_1, y_1_compare, where=y_1 >= y_1_compare, facecolor='tab:cyan', interpolate=True,alpha=0.6)
ax.fill_between(x_2, y_2, y_2_compare, where=y_2 <= y_2_compare, facecolor='green', interpolate=True,alpha=0.4)
ax.tick_params(labelsize=15)
plt.show()

1 Ответ

0 голосов
/ 26 мая 2018

Обычным способом было бы использовать imshow для этой задачи.Конечно, вы можете создать патч размером с вашу область и залить его градиентным цветом.Или вы просто строите сначала градиент, а затем все свои элементы:

import matplotlib
from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np
matplotlib.rcParams['font.sans-serif'] = "Arial"
matplotlib.rcParams['font.family'] = "sans-serif"

#points for the three lines
x_1=[0.00,1.00,2.00,3.00,4.00,5.00,6.00,6.50]
y_1=[3.00,2.80,2.40,2.20,1.80,1.00,0.20,0.00]

x_2 = [0.00,1.00,2.00,2.80]
y_2 = [7.00,8.00,9.00,10.00]

x_3=[2.80,2.80,3.00,4.00]
y_3=[10.00,6.00,4.00,1.80]

#create comparison arrays for fill_between
y_1_compare=[]
for item in x_1:
    y_1_compare.append(0)

y_2_compare=[]
for item in x_2:
    y_2_compare.append(10)

f=plt.figure(figsize=(5,5))
ax=plt.gca()
#define size of the graph
xl = 0
xh = 8
yl = 0
yh = 10
#array for gradient creation
arr = [[0, 1], [0, 1]]

#plot a gradient picture first in the desired size, which will also autoscale the graph
#set interpolation to "nearest" to see the underlying grid structure
im1 = plt.imshow(arr,  cmap = cm.Oranges, extent = [xl, xh, yl, yh], interpolation = "bicubic", alpha = .6, aspect = "auto")

plt.plot(x_1,y_1,'o',linestyle='-', color='k',linewidth=0.7)
plt.plot(x_2,y_2,'s',linestyle='-',color='k',linewidth=0.7)
plt.plot(x_3,y_3,'^',linestyle='--',color='k',linewidth=0.7)

#fill the areas with white, so we won't see the underlying picture, when we plot a colour with a reduced alpha
ax.fill_between(x_1, y_1, y_1_compare, where=y_1 >= y_1_compare, facecolor='white', interpolate=True,alpha=1)
ax.fill_between(x_2, y_2, y_2_compare, where=y_2 <= y_2_compare, facecolor='white', interpolate=True,alpha=1)
#now plot again color with reduced alpha
ax.fill_between(x_1, y_1, y_1_compare, where=y_1 >= y_1_compare, facecolor='tab:cyan', interpolate=True,alpha=.4)
ax.fill_between(x_2, y_2, y_2_compare, where=y_2 <= y_2_compare, facecolor='green', interpolate=True,alpha=.6)
ax.tick_params(labelsize=15)
plt.show()

Вывод:

enter image description here

Есть многоиз предопределенных цветовых карт , которые будут занимать вас некоторое время.И список [[0, 1], [0, 1]] может быть, конечно, любым видом массива, а не только грубой сеткой 2 x 2.Попробуйте, например:

sp = np.linspace(0, 1, 10)
spy = np.tile(sp, 3)
arr = sp[:, None] * spy[None, :]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...