一、matplotlib顏色表
matplotlib是一個非常常用的繪圖庫,提供了多種可視化操作函數。其中,顏色表的選擇是繪製圖形時非常重要的一個因素。下面介紹一些matplotlib提供的顏色表。
1、按顏色分組的顏色表
import matplotlib.pyplot as plt
colors = ['blue', 'green', 'red', 'cyan', 'magenta', 'yellow', 'black']
for i, color in enumerate(colors):
plt.plot(range(5), [i]*5, label=color)
plt.legend()
plt.show()
使用顏色名稱,可繪製顏色樣本。
2、預定義的顏色表
import matplotlib.pyplot as plt
names = ['Accent', 'Dark2', 'Paired', 'Pastel1', 'Set1', 'Set2', 'Set3', 'tab10', 'tab20', 'tab20b', 'tab20c']
for i, name in enumerate(names):
plt.subplot(3, 4, i+1)
plt.imshow(plt.cm.get_cmap(name)(range(10)).T[::-1])
plt.axis('off')
plt.title(name)
plt.show()
使用預定義的顏色表,可繪製出不同顏色映射方案的色彩/灰度值序列的示例
3、自定義顏色表
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randint(0, 10, size=10)
y = np.random.randint(0, 10, size=10)
points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
fig, ax = plt.subplots(figsize=(5, 5))
norm = plt.Normalize(0, 1)
lc = LineCollection(segments, cmap='viridis', norm=norm)
lc.set_array(np.array([0.5]*9))
line = ax.add_collection(lc)
fig.colorbar(line, ax=ax)
plt.show()
使用自定義顏色序列,可繪製出自定義的顏色映射方案的色彩/灰度值序列。
二、matplotlib線條顏色
在matplotlib中,可以使用多種方式設置線條的顏色:
1、RGB顏色值
import matplotlib.pyplot as plt
plt.plot(range(5), 'r') # 'r'表示紅色線條
plt.show()
2、RGBA顏色值
import matplotlib.pyplot as plt
plt.plot(range(5), color='blue', alpha=0.5) # blue的RGBA值為(0,0,1,1), alpha值取[0,1]
plt.show()
3、HTML顏色代碼
import matplotlib.pyplot as plt
plt.plot(range(5), '#00FF00') # 使用html顏色代碼,表示綠色
plt.show()
三、matplotlib填充顏色
使用fill函數可以填充顏色
1、填充單色
import matplotlib.pyplot as plt
plt.fill_between(range(5), 0, range(5), color='skyblue', alpha=0.5)
plt.show()
2、填充多色
import matplotlib.pyplot as plt
x = range(5)
y = range(5)
fig, ax = plt.subplots()
ax.fill_between(x[:2], y[:2], color='blue', alpha=0.5)
ax.fill_between(x[1:3], y[1:3], color='green', alpha=0.5)
ax.fill_between(x[2:], y[2:], color='red', alpha=0.5)
plt.show()
四、matplotlib設置線條顏色
在matlplotlib中,可以使用set_color()函數來設置線條顏色。
1、設置單色線條顏色
import matplotlib.pyplot as plt
line, = plt.plot(range(5))
line.set_color('r')
plt.show()
2、設置多色線條顏色
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
line1, = ax.plot(range(5), label='Line 1')
line2, = ax.plot(range(1, 6), label='Line 2')
line3, = ax.plot(range(2, 7), label='Line 3')
plt.legend()
line1.set_color('red')
line2.set_color('green')
line3.set_color('blue')
plt.show()
五、matplotlib坐標軸顏色
可以使用spines來設置坐標軸顏色
1、自定義坐標軸顏色
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(range(5))
ax.spines['bottom'].set_color('chartreuse')
ax.spines['top'].set_color('chartreuse')
ax.spines['left'].set_color('chartreuse')
ax.spines['right'].set_color('chartreuse')
plt.show()
2、隱藏坐標軸
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(range(5))
ax.spines['bottom'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.show()
六、matplotlib好看的顏色
下面是一些matplotlib中比較好看的顏色可以作為參考:
1、藍色系列
import matplotlib.pyplot as plt
plt.plot(range(5), color='dodgerblue')
plt.show()
2、紅色系列
import matplotlib.pyplot as plt
plt.plot(range(5), color='tomato')
plt.show()
3、黃色系列
import matplotlib.pyplot as plt
plt.plot(range(5), color='gold')
plt.show()
七、matplotlib顏色設置
可以使用rcParams設置顏色的默認值。
1、設置線條顏色
import matplotlib.pyplot as plt
plt.rcParams['lines.color'] = 'r'
plt.plot(range(5))
plt.show()
2、設置坐標軸顏色
import matplotlib.pyplot as plt
plt.rcParams['axes.edgecolor'] = 'r'
plt.plot(range(5))
plt.show()
3、設置網格線顏色
import matplotlib.pyplot as plt
plt.rcParams['grid.color'] = 'r'
plt.plot(range(5))
plt.show()
八、python matplotlib顏色選取
下面介紹幾種常用的顏色選取方式:
1、手動指定顏色
import matplotlib.pyplot as plt
colors = ['blue', 'green', 'red', 'cyan', 'magenta', 'yellow', 'black']
for i, color in enumerate(colors):
plt.plot(range(5), [i]*5, label=color)
plt.legend()
plt.show()
2、取自定義顏色序列
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randint(0, 10, size=10)
y = np.random.randint(0, 10, size=10)
points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
fig, ax = plt.subplots(figsize=(5, 5))
norm = plt.Normalize(0, 1)
lc = LineCollection(segments, cmap='viridis', norm=norm)
lc.set_array(np.array([0.5]*9))
line = ax.add_collection(lc)
fig.colorbar(line, ax=ax)
plt.show()
3、使用colormap
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 1)
y = np.linspace(0, 1)
X, Y = np.meshgrid(x, y)
Z = X**2 - Y**2
fig, ax = plt.subplots()
im = ax.imshow(Z, cmap='viridis', extent=[0, 1, 0, 1])
fig.colorbar(im, ax=ax)
plt.show()
4、使用預定義顏色表
import matplotlib.pyplot as plt
names = ['Accent', 'Dark2', 'Paired', 'Pastel1', 'Set1', 'Set2', 'Set3', 'tab10', 'tab20', 'tab20b', 'tab20c']
for i, name in enumerate(names):
plt.subplot(3, 4, i+1)
plt.imshow(plt.cm.get_cmap(name)(range(10)).T[::-1])
plt.axis('off')
plt.title(name)
plt.show()
5、使用hex值
import matplotlib.pyplot as plt
plt.plot(range(5), '#FF00FF')
plt.show()
總結
這篇文章詳細闡述了matplotlib中的顏色對照表相關知識,包含了matplotlib顏色表、matplotlib線條顏色、matplotlib填充顏色、matplotlib設置線條顏色、matplotlib坐標軸顏色、matplotlib 好看的顏色、matplotlib顏色設置和python matplotlib顏色選取等方面。以上是一些常用的顏色設置方式,希望對大家有所幫助。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/253110.html