Matplotlib Python 中的線型


在任何線圖中,線的視覺效果都會受到線型(也稱為 linestyle)的影響。Python 的 Matplotlib 模組提供了各種線型,以增強繪圖的視覺吸引力。本文全面介紹了在 Matplotlib Python 中使用線型的方法,並提供了清晰簡潔的示例。

瞭解 Matplotlib 中的線型

Matplotlib 中的線型定義了可以繪製的線的圖案。實線、虛線、點劃線和點線是一些最常用的線型。透過使用不同的線型,您可以使您的繪圖更清晰、更具資訊性。

開始使用 Matplotlib 線型

首先,檢查您的 Python 環境中是否已載入 Matplotlib 模組。如果沒有,可以使用 pip 進行安裝 -

pip install matplotlib

匯入所需的庫 -

import matplotlib.pyplot as plt

示例 1:基本線型

讓我們從一些簡單的線型示例開始

import numpy as np

# Define a simple range of values for x and reshape so it can be used in multiple plots
x = np.linspace(0, 10, 1000).reshape(-1,1)

# Set up the figure and axis
fig, ax = plt.subplots(1,1, figsize=(10,6))

# solid line style
ax.plot(x, np.sin(x), linestyle='-', label='solid')

# dashed line style
ax.plot(x, np.cos(x), linestyle='--', label='dashed')

# dashdot line style
ax.plot(x, -np.sin(x), linestyle='-.', label='dashdot')

# dotted line style
ax.plot(x, -np.cos(x), linestyle=':', label='dotted')

ax.legend()

plt.show()

本例中使用 plot() 方法繪製了四種不同的線型('-', '--', '-.' 和 ':')。

示例 2:線型簡寫程式碼

此外,Matplotlib 為大多數常用的線型提供了單字母簡寫程式碼 -

Additionally, Matplotlib offers one-letter shortcodes for the majority of common line styles:
fig, ax = plt.subplots(1,1, figsize=(10,6))

# solid line style
ax.plot(x, np.sin(x), linestyle='-', label='solid')

# dashed line style
ax.plot(x, np.cos(x), linestyle='d', label='dashed')

# dashdot line style
ax.plot(x, -np.sin(x), linestyle='-.', label='dashdot')

# dotted line style
ax.plot(x, -np.cos(x), linestyle=':', label='dotted')

ax.legend()

plt.show()

本例使用了單字母簡寫程式碼,但仍然使用了與之前相同的線型。

示例 3:帶有自定義間距的線型

要指定虛線圖案,您可以使用元組定義自己的線型

fig, ax = plt.subplots(1,1, figsize=(10,6))

# Define custom line styles
line_styles = [(0, (1, 10)), (0, (1, 1)), (0, (5, 10)), (0, (5, 5))]

for i, style in enumerate(line_styles, start=1):
   ax.plot(x, i * np.sin(x), linestyle=style, label=f'line style {i}')

ax.legend()

plt.show()

每個元組定義一個線型。元組中的第一個值是偏移量。第二個值是一個元組,指定虛線和間距的長度。

示例 4:將線型與顏色結合使用

您還可以將不同的顏色和線型結合使用,使您的繪圖更有趣、更具指導意義。

fig, ax = plt.subplots(1,1, figsize=(10,6))

ax.plot(x, np.sin(x), linestyle='-', color='blue', label='solid blue')
ax.plot(x, np.cos(x), linestyle='--', color='red', label='dashed red')
ax.plot(x, -np.sin(x), linestyle='-.', color='green', label='dashdot green')
ax.plot(x, -np.cos(x), linestyle=':', color='purple', label='dotted purple')

ax.legend()

plt.show()

在本例中,每種線型都與不同的顏色混合。

示例 5:線型和標記的組合

可以將標記和線型組合起來建立更復雜的視覺化效果

fig, ax = plt.subplots(1,1, figsize=(10,6))

# Define a smaller range for x
x = np.linspace(0, 10, 10)

ax.plot(x, np.sin(x), linestyle='-', marker='o', label='solid with marker')
ax.plot(x, np.cos(x), linestyle='--', marker='x', label='dashed with marker')

ax.legend()

plt.show()

此圖中每個資料點都有標記,以及各種線型。

結論

Matplotlib 的線型是一個關鍵特性,對於區分不同的資料集以及提高整體繪圖的可讀性至關重要。您可以選擇各種選項,使用簡單的線型、將它們與顏色和標記結合起來,或者建立自己的自定義設計。

始終牢記,準確的資料視覺化不僅僅是在圖表上繪製資料。它還包括講述一個易於理解的故事。透過成為 Matplotlib 線型專家,您將離建立有趣且有意義的資料故事更近一步。

更新於: 2023年7月18日

951 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.