Matplotlib
- 先写一个最简单的:
import matplotlib.pyplot as plt
plt.plot([1,4],[2,8]) #plot画折线图
plt.show()
确定两个点画一条线
-
import matplotlib.pyplot as plt x=[1,23,4,56,7,6] #x轴数据 y=[22,44,56,67,43,2] #y轴数据 s=[22,43,33,44,43,7] plt.plot(x,y,s,linewidth=5) #画图linewidth设置线条宽度 plt.xlabel('sad') #x轴名称 plt.ylabel('摇号') #y轴名称 plt.rcParams["font.sans-serif"]=["SimHei"] #防止中文乱码 plt.title('折线') #标题 #ncol表示显示成几列 fontsize表示图例大小 图例的具体位置: (x,y,width,height) plt.legend(['sd','faw'],ncol=2,fontsize=18,bbox_to_anchor=[0,1,1,0.2]) plt.show() #显示图像
- plt.plot(x,y,linewidth=5) #linewidth设置线条宽度
- plt.savefig(“resalt”) #保存图片默认png格式,resalt为图片名称
- plt.sabplot(2,2,1) #将画布分为两行,两列放在第一区
- plt.bar() #画柱形图
- plt.scatter() #画散点图
- plt.stackplot() #画堆栈图
- plt.pie() #画饼图
- plt.polar() #极坐标图
- plt.brh()#横向条形图
- 堆栈图
import matplotlib.pyplot as plt #导包
mss=[1,2,3,4,5,6]
a=[1,2,3,4,5,6]
b=[6,5,4,3,2,1]
c=[3,5,2,7,8,5]
plt.stackplot(mss,a,b,c,) #四条数据
plt.xlabel('sfse') #x轴名称
plt.ylabel('sffse') #y轴名称
plt.title('sfefe') #大标题
plt.show() #显示图像
条形图:
-
import matplotlib.pyplot as plt fruits = ['apple', 'blueberry', 'cherry', 'orange'] counts = [40, 100, 30, 55] bar_labels = ['red', 'blue', '_red', 'orange'] bar_colors = ['tab:red', 'tab:blue', 'tab:red', 'tab:orange'] #颜色 plt.bar(fruits, counts, label=bar_labels, color=bar_colors) plt.ylabel('fruit supply') #y轴名称 plt.title('Fruit supply by kind and color') #大标题 plt.legend(title='Fruit color') #导航栏名称 plt.show()
- 饼图
import matplotlib.pyplot as plt
size=[3,3,2,1,1]
plt.pie(size,labels=["Windows", "MAC", "Linux", "Android", "Other"],
startangle=90, #从什么地方开始画
shadow=True, #阴影效果
autopct='%1.2f%%', #数据显示小数点后几位
explode=(0.1,0.2,0,0,0), #模块分裂
colors=['red', 'pink', 'blue', 'green'])
plt.title("手机系统占比分析")
plt.show()
- 双柱状图:
from matplotlib import pyplot as plt
import numpy as np
arr = np.arange(2017, 2022) #生成2017——2022的数据
x1 = [49, 48, 45, 52, 50] # x轴
x2 = [60, 62, 61, 65, 63]
y = [2017, 2018, 2019, 2020, 2021]
plt.bar(arr, x1,color='purple', label='LMY')
plt.bar(arr, x2,color='yellow', label='Mother', alpha=0.3) #alpha透明度
plt.title("LMY and her mother's weight")
plt.xlabel('weight')
plt.ylabel('year')
plt.legend()
plt.show()
- 散点图:
import matplotlib.pyplot as plt
x=["sff","fsfe","swe","serf","few"]
y=[22,56,23,53,55]
plt.scatter(x,y,label="sfsf",color="c",marker="*",linewidths=10) #marker设置点图案,linewidths设置点大小
plt.title("dsfse")
plt.xlabel("dsfe")
plt.ylabel("fsfse")
plt.legend()
plt.show()