1.matplotlib库是python的绘图库,可以很轻松的进行数据图形化,并提供多样化的输出格式
2.Matplotlib库常常与Numpy库,scipy库进行联合使用,这种组合广泛用于替代MatLab,是一个强大的科学计算环境,有助于我们通过python学习数据科学或者机器学习
3.Matplotlib应用——
1.折线图
import matplotlib.pyplot as plt
import numpy as np
plt.figure(figsize=(16, 9), dpi=80)
plt.rcParams["font.sans-serif"] = ["SimHei"]
x = np.arange(1, 11, 1)
y_ticks = np.arange(0, 101, 10)
y = np.random.randint(1, 100, size=(10))
y_2 = np.random.randint(1, 100, size=(10))
plt.plot(x, y, label="最高标准")
plt.plot(x, y_2, label="最低标准", color="r")
plt.xticks(x)
plt.yticks(y_ticks)
plt.ylim(0, 110)
plt.xlim(0, 11)
plt.xlabel("x轴")
plt.ylabel("y轴")
plt.title("折线图")
plt.text(1, 10, "解释说明")
plt.legend()
plt.show()
2.柱状图
import matplotlib.pyplot as plt
import numpy as np
y = np.arange(10, 101, 10)
x = np.arange(1, 11)
label = ["一", "二", "三", "四", "五", "六", "七", "八", "九", "十"]
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.figure(figsize=(16, 9), dpi=80)
plt.bar(x, y, width=0.5, label="完成量", color="#ed5736")
plt.xticks(np.arange(1, 11, 1), label)
plt.title("测试", fontsize=50)
plt.text(1, 10, "10%", weight="bold", color="b", fontsize=20, ha="center", va="bottom")
plt.text(2, 20, "20%", weight="bold", color="r", fontsize=20, verticalalignment='bottom', horizontalalignment="center")
plt.text(3, 30, "30%", weight="bold", color="r", fontsize=20, verticalalignment='bottom', horizontalalignment="center")
plt.text(4, 40, "40%", weight="bold", color="r", fontsize=20, verticalalignment='bottom', horizontalalignment="center")
plt.text(5, 50, "50%", weight="bold", color="r", fontsize=20, verticalalignment='bottom', horizontalalignment="center")
plt.text(6, 60, "60%", weight="bold", color="r", fontsize=20, verticalalignment='bottom', horizontalalignment="center")
plt.text(7, 70, "70%", weight="bold", color="r", fontsize=20, verticalalignment='bottom', horizontalalignment="center")
plt.text(8, 80, "80%", weight="bold", color="r", fontsize=20, verticalalignment='bottom', horizontalalignment="center")
plt.text(9, 90, "90%", weight="bold", color="r", fontsize=20, verticalalignment='bottom', horizontalalignment="center")
plt.text(10, 100, "100%", weight="bold", color="r", fontsize=20, verticalalignment='bottom',
horizontalalignment="center")
plt.annotate("未达标平均线", xy=(1, 80), arrowprops=dict(arrowstyle="<->"), fontsize=20, xytext=(1, 60))
plt.ylim(0, 120)
plt.xlim(0, 11)
plt.axhline(80, linestyle="--", color="r", label="80%水平线", )
plt.xlabel("x轴", fontsize=24)
plt.ylabel("y轴", fontsize=24)
plt.legend(loc="upper left", fontsize=20)
plt.grid( axis="y", )
plt.show()
3.雷达图
import matplotlib.pyplot as plt
import numpy as np
plt.figure(figsize=(16, 9), dpi=80)
plt.rcParams["font.sans-serif"] = ["SimHei"]
data =np.random.rand(6,6)
# rradar_labels=["研究型(I)","艺术性(A)","社会性(S)","企业性(E)","传统性(C)","现实性(R)"]
# radar_labels= np.concatenate((radar_labels,[radar_labels[0]]))
anghles=np.linspace(0,2*np.pi,6,endpoint=False)
anghles = np.concatenate((anghles,[anghles[0]]))
data=np.concatenate((data,[data[0]]))
plt.polar(anghles,data)
plt.thetagrids((anghles*180/np.pi))
plt.fill(anghles,data,alpha=0.25)
plt.show()
yyds呀!