如何在 Matplotlib 繪製迴圈中為標記和線條設定相同顏色?
要為 matplotlib 中的標記和線設定相同顏色,我們可以採取以下步驟:
使用 numpy 初始化m、n和x資料點。
使用figure()方法建立新圖形或啟用現有圖形。
使用clf()方法清除圖形。
使用subplot()方法向當前圖形新增子圖。
從可迭代標記型別中獲取標記。
遍歷 1 到 n 的範圍。
在迴圈中使用帶有相同標記和顏色的plot()方法繪製線條和標記。
使用show()方法顯示圖形。
示例
import numpy as np import itertools from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True m = 5 n = 5 x = np.zeros(shape=(m, n)) plt.figure() plt.clf() plt.subplot(111) marker = itertools.cycle(('o', 'v', '^', '<', '>', 's', '8', 'p')) for i in range(1, n): x = np.dot(i, [1, 1.1, 1.2, 1.3]) y = x ** 2 plt.plot(x, y, linestyle='', markeredgecolor='none', marker=next(marker), alpha=1) plt.plot(x, y, linestyle='-') plt.show()
輸出
廣告