Matplotlib lijndiagram

Jinku Hu 12 april 2022 20 december 2019
  1. Lineaire lijn
  2. Gebogen lijn
  3. Lijntype
Matplotlib lijndiagram

We beginnen met het plotten van het basistabeltype - lijndiagram. plot zou gemakkelijk lijnen kunnen uitzetten zoals Lineaire lijn of gebogen lijn, en hebben ook een andere configuratie zoals kleuren, breedte, marker-grootte, enz.

Lineaire lijn

# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 9, 10)  
y = 2 * x
plt.plot(x, y, "b-")
plt.show()

Het plot de lijn van y=2*x , waarbij x in het bereik tussen 0 en 9 ligt.

Matplotlib-lijndiagram - lineaire lijn

plt.plot(x, y, "b-")

Het plot de gegevens van x en y met een lijnstijl van b - blauwe kleur en - - ononderbroken lijn.

Gebogen lijn

# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 4 * np.pi, 1000)  
y = np.sin(x)

plt.plot(x, y, "r--")

plt.show()

Het genereert een sinusvormige golfvorm en heeft de lijnstijl als kleur rood - r en stippellijn - --.

Matplotlib-lijndiagram - gebogen lijn

Lijntype

Je kan het lijntype zoals breedte, kleur en lijnstijl wijzigen met verschillende invoerargumenten in plt.plot() functie.

matplotlib.pyplot.plot(*args, **kwargs)

parameters

Naam descritpion
x, y De horizontale / verticale coördinaten van de gegevenspunten
fmt Een opmaakreeks, bijvoorbeeld ‘b-’ voor blauwe ononderbroken lijn.

**kwargs

Eigendom Beschrijving
color of c elke matplotlib-kleur
figure een Figure instantie
label voorwerp
linestyle of ls [‘solide’
linewidth of lw vlotterwaarde in punten
marker A valid marker style
markersize of ms float
xdata 1D array
ydata 1D array
zorder float

Lijnkleur

Je hebt enkele methoden om de kleur in het color argument een naam te geven.

Alias ​​van één letter

De standaard ingebouwde kleuren hebben de alias zoals hieronder,

Alias Kleur
b blauw
g groen
r rood
c cyaan
m magenta
y geel
k zwart
w wit

Matplotlib-lijndiagram -Kleur enkele alias

Html Hex String

Je kan bijvoorbeeld een geldige html-hex-string doorgeven aan de parameter color.

color = "#f44265"

RGB Tuple

Je kan ook de kleur opgeven met een R,G,B tuple, waarbij de waarde R, G, B in het bereik van ligt in [0, 1] plaats van in het normale bereik van [0, 255].

De kleur die wordt weergegeven met de bovenstaande html-hex-string heeft de RGB waarde van (0.9569, 0.2588, 0.3891).

color = (0.9569, 0.2588, 0.3891)

Lijnstijl

Matplotlib heeft 4 ingebouwde lijnstijlen,

Lijnstijl
- Matplotlib-lijndiagram - Lijnstijl - ‘-’
-- Matplotlib-lijndiagram - Lijnstijl - ‘-’
: Matplotlib-lijndiagram - Lijnstijl - ‘:’
:- Matplotlib-lijndiagram - Lijnstijl - ‘-.’
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 4 * np.pi, 1000)  

for index, line_style in enumerate(['-', '--', ':', '-.']):
    y = np.sin(x - index*np.pi/2)
    plt.plot(x, y, 'k', linestyle=line_style, lw=2)

plt.title("Line Style")
plt.grid(True)

plt.show()

Matplotlib-lijndiagram - lijnstijl

Lijnbreedte

Je kan de lijnbreedte opgeven met de parameter linewidth zoals in

linewidth = 2 # unit is points

of gebruik gewoon de afkorting,

lw = 2
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 20, 21)  

for line_width in [0.5, 1, 2, 4, 8]:
    y = line_width * x
    plt.plot(x, y, 'k', linewidth=line_width)

plt.title("Line Width")
plt.grid(True)

plt.show()

Matplotlib-lijndiagram - lijndikte

Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn