python的Matplotlib庫(常用)

Matplotlib庫的教程鏈接:點擊查看

#matplotlib庫常用的
import matplotlib.pyplot as plt
import csv
import numpy as np
import matplotlib.animation as animation
from matplotlib import style
import random

slices = [7,2,2,13]
activities = ['sleeping','eating','working','playing']
cols = ['c','m','r','b']
x=[]
y=[]

plt.plot([1,2,3,4,5],[1,3,5,7,2],label="one")#折線圖
plt.bar([1,2,3,4],[1,2,3,4],label="two",color="red")#柱狀圖(g爲綠色,b爲藍色,r爲紅色,等等。 你還可以使用十六進制顏色代碼,如#191970)
plt.scatter([5,3,2,1,4],[4,1,3,2,5],label="three",color="g",s=25,marker="^")#散點圖marker參數爲散點圖類型【https://matplotlib.org/api/markers_api.html】

#餅狀圖
# plt.pie(slices,
#         labels=activities,#名稱
#         colors=cols,#顏色
#         startangle=90,#角度
#         shadow= True,#陰影
#         explode=(0,0.1,0,0),#拉出的切片
#         autopct='%1.1f%%')#百分比放到圖表上

#讀取CSV電子表格的數據[這個地方可以用在搭建一個模板文檔,自動填入數據進行可視化分析]
with open('11.txt','r') as csvfile:
    plots = csv.reader(csvfile, delimiter=',')
    for row in plots:
        x.append(int(row[0]))
        y.append(int(row[1]))
plt.plot(x,y, label='six',color="b")

#使用另一個模塊[numpy]
x, y = np.loadtxt('11.txt', delimiter=',', unpack=True)
plt.plot(x,y, label='seven',color="r")

#從網絡加載數據【上一篇文章https://blog.csdn.net/qq_38698632/article/details/104520834】

#調用實時圖表
# style.use('fivethirtyeight')

# fig = plt.figure()
# ax1 = fig.add_subplot(1,1,1)
# def animate(i):
#     graph_data = open('11.txt','r').read()
#     lines = graph_data.split('\n')
#     xs = []
#     ys = []
#     for line in lines:
#         if len(line) > 1:
#             x, y = line.split(',')
#             xs.append(x)
#             ys.append(y)
#     ax1.clear()
#     ax1.plot(xs, ys)
# ani = animation.FuncAnimation(fig, animate, interval=5000)

#子圖
# style.use('fivethirtyeight')

# fig = plt.figure()

# def create_plots():
#     xs = []
#     ys = []

#     for i in range(10):
#         x = i
#         y = random.randrange(10)

#         xs.append(x)
#         ys.append(y)
#     return xs, ys

# ax1 = fig.add_subplot(221)
# ax2 = fig.add_subplot(222)
# ax3 = fig.add_subplot(212)


plt.xlabel("x")#x軸名稱
plt.ylabel("y")#y軸名稱

plt.title("name")#圖的標題
plt.legend()#默認圖例

plt.show()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章