1.繪製正弦dom
from matplotlib.pyplot import plot, show import math T = range(100) # 0~99 # X周的範圍2pi,分紅100份 X = [(2 * math.pi) * t / len(T) for t in T] # sin和cos Y = [math.sin(value) for value in X] print(X) plot(X, Y) # 繪製 show()
2.在同一個二維座標系繪製出一元二次曲線和正弦、餘弦曲線函數
import numpy from matplotlib.pyplot import plot, show # 在同一個二維座標系繪製出一元二次曲線和正弦、餘弦曲線 # 一元二次方程 X = numpy.linspace(-3, 3, 200) Y = X ** 2 - 2 * X + 1 plot(X, Y) # sin X = numpy.linspace(0, 2 * numpy.pi, 100) Y = numpy.sin(X) plot(X, Y) # cos X = numpy.linspace(0, 2 * numpy.pi, 100) Y = numpy.cos(X) plot(X, Y) show()
3.繪製隨機點spa
import random import matplotlib.pyplot as plt # 繪製隨機點 count = 1024 X = [random.random() for i in range(count)] Y = [random.random() for i in range(count)] # 繪製連續的 # plt.plot(X, Y) # 繪製隨機的 plt.scatter(X, Y) plt.show()
4.繪製垂直柱狀圖blog
from matplotlib.pyplot import plot, show, bar, barh # 繪製垂直柱狀圖 # x座標 X = [0, 1, 2, 3] # y軸座標 Y = [22, 11, 44, 15] bar(X, Y) show()
5.繪製多組垂直柱狀圖import
import matplotlib.pyplot as plt # 繪製多組垂直柱狀圖 Y = [ [11, 22, 44, 66], [55, 66, 77, 22], [44, 55, 66, 77] ] plt.bar(range(4), Y[0], width=0.25) plt.bar([x + 0.25 for x in range(4)], Y[1], width=0.25) plt.bar([x + 0.5 for x in range(4)], Y[2], width=0.25) plt.show()
6.在同一個窗口繪製直方圖和盒狀圖numpy
import numpy import matplotlib.pyplot as plot # hist:直方圖 # boxplot:盒狀圖 # randn函數返回一個或一組樣本,具備標準正態分佈 data = numpy.random.randn(100) # print(data) print("平均值==", numpy.average(data)) # 把顯示分爲兩個區域 fig, (ax1, ax2) = plot.subplots(1, 2, figsize=(8, 4)) print(fig) # Figure(800x400) # 直方圖,分爲100份 ax1.hist(data, 100) # 盒狀圖 ax2.boxplot(data) # 顯示 plot.show()
7.繪製有百分比和標籤的餅狀圖im
import matplotlib.pyplot as plt X = [22, 33, 11, 66] labels = ["張三", "李四", "王二", "麻子"] plt.pie(X, autopct="%.2f%%", labels=labels) plt.legend(labels) plt.axis("equal") plt.show()