Hinweise zur Verwendung von Matplotlib (1) Erste Schritte mit der Verwendung

Der erste Punkt, den wir wissen müssen, ist, dass matplotlib eine Bibliothek zum Zeichnen statistischer Diagramme in Python ist, die viele Methoden zum Zeichnen bietet.

Ich werde nicht über die Installation von matplotlib sprechen

Versuchen wir es direkt mit einem Code

from matplotlib import pyplot as plt #导入包并重命名
x=range(2,26,2) #创建横左边
y=[12,34,23,45,67,43,87,45,67,12,90,55] #创建纵坐标
plt.plot(x,y) # 画图
plt.show() #将图显示出来

Das von diesem Code erzeugte Bild ist wie folgt (ein bisschen hässlich, aber harmlos):
pyplot_01

Dies ist der einfachste Zeichnungscode, aber für uns, die wir nach Schönheit streben, ist er bei weitem nicht genug. Wir brauchen wahrscheinlich die folgenden Fragen zum Ende:

Der Code, der dem obigen Problem entspricht, ist unten angegeben (Schritt für Schritt zum ursprünglichen Code hinzufügen):

  • Ändern Sie die Größe des Bildes
from matplotlib import pyplot as plt #导入包并重命名
plt.figure(figsize=(20,8),dpi=80)
# (20.8)宽和高,dpi像素点密集度
x=range(2,26,2) #创建横左边
y=[12,34,23,45,67,43,87,45,67,12,90,55] #创建纵坐标
plt.plot(x,y) # 画图
plt.show() #将图显示出来

Die Bildausgabe ist wie folgt:
pyplot_02

Wie sich bestimmte Zahlen und dpi auf die endgültige Bildgröße auswirken, können Sie selbst ausprobieren.

  • Speichern Sie das Bild lokal
#只需要在画完图之后加一句
plt.savefig("path\filename.png")
# path为图片保存的路径,建议使用相对路径

Der Effekt ist nicht zu zeigen, aber der Ordner enthält weitere Dateien

  • Passen Sie die Skala an
from matplotlib import pyplot as plt #导入包并重命名
plt.figure(figsize=(20,8),dpi=80)
# (20.8)宽和高,dpi像素点密集度
x=range(2,26,2) #创建横左边
y=[12,34,23,45,67,43,87,45,67,12,90,55] #创建纵坐标
plt.xticks([1,2,3,6,7,18,19])
plt.plot(x,y) # 画图
plt.show() #将图显示出来

Der Effekt ist in der folgenden Abbildung dargestellt. Sobald die entsprechende Beziehung zwischen der x- und der y-Achse ermittelt wurde, kann der Skalenwert meiner Abszisse nach Belieben angegeben werden.
pyplot_03
In Analogie ist die Skala der y-Achse

plt.yticks([1,2,3,4,5]) 
  • Beschreiben Sie die x-, y-Achse (einschließlich Name und Einheit).
from matplotlib import pyplot as plt  # 导入包并重命名
plt.figure(figsize=(20, 8), dpi=100)
# (20.8)宽和高,dpi像素点密集度
x = range(2, 26, 2)  # 创建横左边
y = [12, 34, 23, 45, 67, 43, 87, 45, 67, 12, 90, 55]  # 创建纵坐标
plt.xticks([1,2,3.5,6,7,18,19])
plt.xlabel("time")
plt.ylabel("temp")
plt.title("temp-time")
plt.plot(x, y)  # 画图
plt.show()  # 将图显示出来

Das Anzeigeergebnis ist wie folgt:
pyplot_04
Es wird hier ein Problem geben, der hinzugefügte Text kann nicht chinesisch sein, wir müssen dies einrichten, siehe Blog

  • Linienattribute

Das Linienattribut gibt an, ob der Bildlinienstil dick oder dünn, durchgezogene Linie oder gestrichelte Linie ist. Wir können der Plotmethode Linienattributparameter hinzufügen, um den Vergleich zu erleichtern. Um den Vergleich zu erleichtern, setzen wir hier zwei Kurven, dh verwenden plt.plot zweimal. ()

from matplotlib import pyplot as plt  # 导入包并重命名
plt.figure(figsize=(20, 8), dpi=100)
# (20.8)宽和高,dpi像素点密集度
x = range(2, 26, 2)  # 创建横左边
y_1= [12, 34, 23, 45, 67, 43, 87, 45, 67, 12, 90, 55]  # 创建纵坐标
y_2= [11, 36, 20, 40, 60, 49, 80, 49, 69, 10, 90, 55]
plt.xticks([1,2,3.5,6,7,18,19])
plt.xlabel("time")
plt.ylabel("temp")
plt.title("temp-time")
plt.plot(x, y_1,label="today",color="red",linestyle="--")  # 画图
plt.plot(x, y_1,label="yesterday",color="blue",linestyle=":")  
plt.grid()
plt.legend() #添加图例
plt.show()  # 将图显示出来

Die Renderings lauten wie folgt:
pyplot_05
Zur Erinnerung: plt.legend () kann die Position durch Hinzufügen von Parametern ändern. Weitere Informationen finden Sie im Blog

  • Zeichnen Sie ein Streudiagramm

Das Zeichnen eines Streudiagramms ist eigentlich sehr einfach. Ersetzen Sie einfach plt.plot () durch plt.scatter ()

from matplotlib import pyplot as plt  # 导入包并重命名
plt.figure(figsize=(20, 8), dpi=100)
# (20.8)宽和高,dpi像素点密集度
x = range(2, 26, 2)  # 创建横左边
y_1= [12, 34, 23, 45, 67, 43, 87, 45, 67, 12, 90, 55]  # 创建纵坐标
y_2= [11, 36, 20, 40, 60, 49, 80, 49, 69, 10, 90, 55]
plt.xticks([1,2,3.5,6,7,18,19])
plt.xlabel("time")
plt.ylabel("temp")
plt.title("temp-time")
plt.scatter(x, y_1,label="today",color="red",linestyle="--")  # 画图
plt.scatter(x, y_2,label="yesterday",color="blue",linestyle=":")  
plt.grid()
plt.legend() #添加图例
plt.show()  # 将图显示出来

Der Effekt ist wie folgt:
pyplot_06

  • Balkendiagramm zeichnen

Es ist auch sehr einfach, ein Balkendiagramm zu zeichnen. Ersetzen Sie plt.plot () durch plot.bar (). Der Schönheit halber werden wir die Daten des Diagramms ändern

from matplotlib import pyplot as plt  # 导入包并重命名
plt.figure(figsize=(20, 8), dpi=100)
# (20.8)宽和高,dpi像素点密集度
x = range(1,7) # 创建横左边
y = [3,5,6,4,3,2]
plt.xlabel("time")
plt.ylabel("temp")
plt.title("temp-time")
plt.bar(x, y,label="today",color="red",width=0.3)  # 画图
plt.grid()
plt.legend() #添加图例
plt.show()  # 将图显示出来

Die Anzeigeergebnisse sind wie folgt:
pyplot_07

Wenn wir mit einem Histogramm sprechen möchten, müssen wir die folgenden Operationen ausführen: Einfach ausgedrückt, versetzen Sie die Abszisse um eine Einheit der Spaltenbreite, da sie sich sonst überlappt.

from matplotlib import pyplot as plt  # 导入包并重命名
plt.figure(figsize=(20, 8), dpi=100)
# (20.8)宽和高,dpi像素点密集度
x = range(1, 7)  # 创建横左边
y_1 = [3, 5, 6, 4, 3, 2]
y_2 = [4, 1, 3, 6, 8, 9]
y_3 = [10, 2, 4, 7, 10, 8]
plt.xlabel("time")
plt.ylabel("temp")
plt.title("temp-time")
bar_width = 0.2 #柱宽
x_2 = [i + bar_width for i in x]#错开
x_3=[i + 2*bar_width for i in x]
plt.bar(x, y_1, label="today", color="red", width=bar_width)  # 画图
plt.bar(x_2, y_2, label="tomorrow", color="green", width=bar_width)  # 画图
plt.bar(x_3, y_3, label="yesterday", color="gray", width=bar_width)  # 画图
plt.grid()
plt.legend()  # 添加图例
plt.show()  # 将图显示出来

Die Renderings sind wie folgt:
pyplot_08

  • Andere statistische Grafiken

Für Pyplot reicht es nicht aus, das obige statistische Diagramm zu beherrschen. Auf der offiziellen Website von matplotlib finden Sie viele Vorlagen. Laden Sie einfach die Vorlage herunter und ändern Sie sie.

Ich denke du magst

Origin blog.csdn.net/qq_41459262/article/details/104522693
Empfohlen
Rangfolge