怎么说呢?这个库,画一些基本的形状挺好用的,还有pyecharts,但是我不想在可视化上浪费太多时间,就这样吧。
---------------------------------------------------------------------------------------------------------
之前学的:
# -*- coding: utf-8 -*- """ Created on Thu May 28 17:28:17 2020 @author: Administrator """ # from matplotlib import pyplot as plt fig=plt.figure(figsize=(15,5),dpi=80) x=range(2,26,2) y=[15,13,14,5,17,20,25,26,24,22,18,15] plt.plot(x,y) plt.yticks(y)#设置y坐标 plt.xticks(range(5,50,2))#设置x范围 plt.savefig("f://a.svg")#保存文件 plt.show()#展示 #折线图 from matplotlib import pyplot as plt import matplotlib import random from matplotlib import font_manager # font={ # 'family':'MicroSoft YaHei', # 'weight':'bold', # 'size':'20' # } # matplotlib.rc("font",**font) my_font=font_manager.FontProperties(fname="C:\Windows\Fonts\STKAITI.TTF") x=range(0,120) y=[random.randint(20,35) for i in range(120)] fig=plt.figure(figsize=(20,5),dpi=80) x_ticks=["10点{}分".format(i) for i in range(60)] x_ticks+=["11点{}分".format(i) for i in range(60)] plt.title("10-11点温度",fontproperties=my_font) plt.ylabel("温度",fontproperties=my_font) plt.xlabel("时间",fontproperties=my_font) plt.xticks(list(x)[::5],x_ticks[::5],rotation=45,fontproperties=my_font) plt.grid(alpha=0.4)#linestyle plt.plot(x,y,label="a",color="r",linestyle="--",linewidth=1) plt.plot(range(100,220),y,label="b") #图例 plt.legend(prop=my_font,loc=5) plt.savefig("f://wendu.png")#保存文件 plt.show() #散点图 from matplotlib import pyplot as pt from matplotlib import font_manager import random my_font=font_manager.FontProperties(fname="C:\Windows\Fonts\STKAITI.TTF") y_3=[random.randint(10,22) for i in range(31)] y_10=[random.randint(10,30)for i in range(31)] x_3=range(1,32) x_10=range(32,63) #设置图形大小 plt.figure(figsize=(20,8),dpi=80) #设置标识 plt.title("3月和10月温度",fontproperties=my_font) plt.ylabel("温度",fontproperties=my_font) plt.xlabel("日期",fontproperties=my_font) #设置x,y刻度 x_xticks=list(x_3)+list(x_10) x_label=["3月{}号".format(i) for i in range(1,32)] x_label+=["10月{}号".format(i) for i in range(1,32)] plt.xticks(x_xticks,x_label,rotation=90,fontproperties=my_font) plt.scatter(x_3,y_3,label="a") plt.scatter(x_10,y_10,label="b",color="r") #图例 plt.legend(prop=my_font,loc=5) plt.show() #条形图 from matplotlib import pyplot as pt from matplotlib import font_manager import random my_font=font_manager.FontProperties(fname="C:\Windows\Fonts\STKAITI.TTF") x=range(12) y_1=[random.randint(30,70) for i in range(12)] y_2=[random.randint(30,70) for i in range(12)] x_xticks=["鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"] #设置大小 plt.figure(figsize=(10,8),dpi=80,facecolor='snow') #设置标识 plt.title("生肖的智商",fontproperties=my_font) plt.ylabel("智商",fontproperties=my_font) plt.xlabel("生肖",fontproperties=my_font) #设置xy plt.xticks(range(0,24,2),x_xticks,rotation=0,fontproperties=my_font) plt.xticks(range(1,25,2),x_xticks,rotation=0,fontproperties=my_font) #横着画 #plt.barh(x,y_1,label="智商",height=1) plt.bar(range(0,24,2),y_1,label="智商",width=0.1) plt.bar(range(1,25,2),y_2,label="情商",width=0.1) #网格 plt.grid(alpha=0.4)#linestyle plt.legend(prop=my_font,loc=5) plt.show() #直方图 from matplotlib import pyplot as pt from matplotlib import font_manager import random my_font=font_manager.FontProperties(fname="C:\Windows\Fonts\STKAITI.TTF") a=[random.randint(60,100) for i in range(300)] #组距=5 w=5 #组数 n=(max(a)-min(a))//w #画刻度 x_3=range(min(a),max(a)+w,w) x_xticks=["第{}组".format(i) for i in range(8)] plt.xticks(x_3,x_xticks,fontproperties=my_font) #分为两组画出来 plt.hist(a,n,width=0.1) plt.show() #画字 from matplotlib import pyplot as plt from matplotlib import font_manager import random s='put some text\nwusen' plt.axis('off') plt.text(0, 0.8, s) #‘bmh’, ‘classic’, ‘dark_background’, ‘fast’, ‘fivethirtyeight’, ‘ggplot’, ‘grayscale’, ‘seaborn-bright’, ‘seaborn-muted’, ‘seaborn-notebook’, ‘seaborn-paper’, ‘seaborn-pastel’, ‘seaborn-poster’, ‘seaborn-talk’, ‘seaborn-ticks’, ‘seaborn-white’, ‘seaborn-whitegrid’, ‘seaborn’, ‘Solarize_Light2’, ‘tableau-colorblind10’, ‘_classic_test’ plt.style.use('classic') axs3=plt.subplot()
View Code
新版总结:
1.画折线图:plot
import numpy as np import matplotlib from matplotlib import pyplot as plt from matplotlib import font_manager # 设置字体方式一 # font={ # 'family':'MicroSoft YaHei', # 'weight':'bold', # 'size':'30' # } # matplotlib.rc("font",**font) # 设置字体方式二:里面有参数,自己去看 my_font=font_manager.FontProperties(fname="fonts/华文彩云.ttf") # 初始化画布figsize=(15,5)是长宽比,dpi代表倍数,像素就是figsize*dpi fig=plt.figure(figsize=(15,5),dpi=80) # 产生步长为2的一个列表 x = np.arange(0,20,2) # randint里面可以size参数可以是数值也可以是元组 y1 = np.random.randint(0,100,size=(10)) y2 = np.random.randint(0,100,size=(10)) # 这一句是把图形画上去 # linestyle:'-', '--', '-.', ':', 'None', ' ', '', 'solid', 'dashed', 'dashdot', 'dotted' plt.plot(x,y1,label="a",color="r",linestyle="--",linewidth=1) plt.plot(x,y2,label="b") # plt.yticks(y)#设置y坐标 #设置x范围,设置角度 plt.xticks(x,rotation=90) # 背景格子线 plt.grid(alpha=0.1)#linestyle # 画标题 plt.title("TITLE",fontproperties=my_font) # 画栅格 plt.legend(prop=my_font,loc=2) #保存文件,格式可以是svg png jpg ... # plt.savefig("f://b.svg") plt.show()#展示
View Code
2.散点图
# plt.scatter(x,y1,label="a",color="r",linestyle="--",linewidth=1)
View Code
3.条形图
# plt.bar(x,y3,label="c")
View Code
4.直方图,我不知道跟条形图啥区别
from matplotlib import pyplot as pt from matplotlib import font_manager import random my_font=font_manager.FontProperties(fname="fonts/华文彩云.ttf") a=[random.randint(60,100) for i in range(300)] print(len(a),a) #组距=5 w=5 #组数 n=(max(a)-min(a))//w print(n) #画刻度 x_3=range(min(a),max(a)+w,w) x_xticks=["第{}组".format(i) for i in range(8)] # x_xticks = ["A","B","C","D","E","F","G","H","I"] plt.xticks(x_3,x_xticks,fontproperties=my_font) #分为两组画出来 plt.hist(a,n,width=3) plt.show()
View Code
5也可以往图上写字,
from matplotlib import pyplot as plt # from matplotlib import font_manager # import random # s='put some text\nwusen' # plt.axis('off') # plt.text(0, 0.8, s) # #‘bmh’, ‘classic’, ‘dark_background’, ‘fast’, ‘fivethirtyeight’, ‘ggplot’, ‘grayscale’, ‘seaborn-bright’, ‘seaborn-muted’, ‘seaborn-notebook’, ‘seaborn-paper’, ‘seaborn-pastel’, ‘seaborn-poster’, ‘seaborn-talk’, ‘seaborn-ticks’, ‘seaborn-white’, ‘seaborn-whitegrid’, ‘seaborn’, ‘Solarize_Light2’, ‘tableau-colorblind10’, ‘_classic_test’ # plt.style.use('classic') # axs3=plt.subplot() # plt.show()