技術共有

Matplotlib を始める

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

#折れ線グラフはデータの変化を表すために使用されます

plt.plot(x,y)

#ヒストグラムは連続データをカウントするために使用されます 間隔なし

plt.hist(データ配列、グループ数)

#棒グラフは、離散配列をカウントし、その変更を反映するために使用されます 隙間がある

plt.bar(x,y,幅 = 0.3)
plt.barh(y,x,高さ = 0.3)

#散布図は、xy 軸間の関係の傾向を示すために使用されます。

plt.scatter(x,y)

  1. #导入pyplot
  2. from matplotlib import pyplot as plt
  3. #限制大小和像素
  4. plt.figure(figsize = (20,8),dpi = 80)
  5. #数据集中对应的x坐标 是一个迭代对象
  6. x = rnage(2,26,2)#共12个数据
  7. #数据集中对应的y坐标 是一个迭代对象
  8. y = [15,13,14.5,17,20,25,26,26,24,22,18,15]#共12个数据
  9. #x和y组成了12个数据点
  10. #绘制x轴的刻度
  11. plt.xticks( range(2,25,1) )
  12. #plt.xticks(x)
  13. #plt.xticks( [i/2 for i in range(2,49)] )
  14. #绘制y轴的刻度
  15. plt.yticks( range(min(y),max(y)+1 )
  16. #传入x y 通过plot绘制折线图
  17. plt.plot(x,y)
  18. #保存
  19. plt.savefig("./t1.png")
  20. #展示图形
  21. plt.show()

 

 

  1. from matplotlib import pyplot as plt
  2. import random
  3. #设置字体参数
  4. font = {
  5. 'family' : 'YouYuan',
  6. 'weight' : 'bold',
  7. 'size' : 12
  8. }
  9. #将字体参数应用到全局
  10. plt.rc('font',**font)
  11. #设置数据集的x y坐标
  12. x = range(0,120)#共120个数(分钟)
  13. y = [ random.randint(20,35) for i in range(120) ]
  14. #设置图形大小
  15. plt.figure( figsize = (20,8),dpi = 60 )
  16. #设置刻度
  17. #分别表示开始的位置和结束的位置(不包括)和步长
  18. _x = x[::10]#表示每10分钟为1个刻度
  19. _xtick_labels = ["现在是{}时{}分".format(i//60 + 10,i%60) for i in _x]
  20. #刻度集合 刻度值 旋转程度
  21. plt.xticks(_x,_xtick_labels,rotation = -90)
  22. #绘制图片
  23. plt.plot(x,y)
  24. #绘制网格 设置透明度
  25. plt.grid(alpha=0.3)
  26. #设置标签
  27. plt.xlabel("时间")
  28. plt.ylabel("温度 单位(C)",labelpad = 20)#偏移
  29. plt.tile("10点到12嗲每分钟的气温变化情况")
  30. #展示图片
  31. plt.show()

 

  1. #coding = utf-8
  2. from matplotlib import pyplot as plt
  3. from matplotlib import font_manager
  4. font = {
  5. 'family':'YouYuan'
  6. 'weight':'bold'
  7. 'size':12
  8. }
  9. plt.rc('font',**font)
  10. #give the data
  11. y_1 = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1]
  12. y_2 = [1,0,3,1,2,2,3,3,2,1,2,1,1,1,1,1,1,1,1,1]
  13. x = range(11,31)
  14. #set the size of the graph
  15. plt.figure(figsize=(20,8),dpi=80)
  16. #set the scale of axis x and axis y
  17. _x = x
  18. _xticks_labels = ["{}岁".format(i) for i in _x]
  19. plt.xticks(_x,_xticks_labels)#data from(data numbers) format
  20. plt.yticks(range(0,7))
  21. #draw the picture respectively
  22. plt.plot(x,y_1,label = "自己",color = "orange",linestyle = ":")
  23. plt.plot(x,y_2,label = "同桌",color = "cyan",linestyle = "--")
  24. #set the grid and the tansparency
  25. plt.grid(alpha = 0.3,color = "black",linestyle = "dotted")#color can be #00ff00
  26. #put the position of the legend
  27. plt.legend(loc="upper left")
  28. #draw the picture
  29. plt.show()

 

 

 

  1. #coding = utf-8
  2. from matplotlib import pyplot as plt
  3. from matplotlib import font_manager
  4. font = {
  5. 'family':'YouYuan',
  6. 'weight':'bold',
  7. 'size' :10
  8. }
  9. plt.rc("font",**font)
  10. #give the data
  11. x_3 = range(1,29)
  12. y_3 = [11,17,16,11,12,11,12,6,6,7,8,9,12,15,14,17,18,21,16,17,20,14,15,15,19,21,22,23]#28 numbers
  13. x_10 = range(51,82)
  14. y_10=[26,26,28,19,21,17,16,19,18,20,20,19,22,23,17,20,21,20,22,15,11,15,5,13,17,10,11,13,12,
  15. 13,6] #31 numbers
  16. plt.figure(figsize=(20,8),dpi=80)
  17. #in order to put the seperate x together put the data to 2 sides
  18. _x = list(x_3)+list(x_10)
  19. #use scatter to draw the picture
  20. plt.scatter(x_3,y_3,label = "March")
  21. plt.scatter(x_10,y_10,label = "Octomber")
  22. #give the scale of axis x
  23. _xtick_labels = ["3月{}日".format(i) for i in x_3]
  24. _xtick_labels += ["10月{}日".format(i) for i in x_10]
  25. plt.xticks(_x[::3],_xtick_labels[::3],rotation = 45)#every 3 days is one scale
  26. #add the position of legend
  27. plt.legend(loc="upper right")
  28. #add the descreptive information
  29. plt.xlabel("时间")
  30. plt.ylabel("温度")
  31. #add grid
  32. plt.grid(alpha = 0.4)
  33. #show the picture
  34. plt.show()

 

 

 

  1. from matplotlib import pyplot as plt
  2. from matplotlib import font_manager
  3. font ={
  4. 'family':'YouYuan',
  5. 'weight':'bold',
  6. 'size':10
  7. }
  8. plt.rc("font",**font)
  9. #You can make it more tidy by adding n to the same position
  10. a = ["电影1n111","电影2","电影3n2","电影3n34","电影5n4","电影6n4","电影6n66667","电影8","电影9","电影1n0","电影1n1","电影1n2","电影1n3","电影1n4","电影1n5","电影1n6","电影1n7","电影1n8"]
  11. b = [56.01,26.94,17.53,16.49,15.45,12.96,11.8,41.21,32.34,22.23,77.7,88.8,12,14,16,1,19,33]
  12. plt.figure(figsize=(20,15),dpi=80)
  13. #draw the bar picture
  14. #if You want to change the pisition of x and y just do the tips 1 and 2
  15. plt.bar(a,b,width = 0.3)
  16. #1. plt.barh(a,b,height = 0.3)
  17. #fix the scale of x
  18. _xtick_labels = [i for i in a]
  19. plt.xticks(a,_xtick_labels,rotation = 60)
  20. #2.plt.yticks(a,_xtick_labels,rotation = 60)
  21. plt.show()

 

 

  1. #coding = utf-8
  2. from matplotlib import pyplot as plt
  3. from matplotlib import font_manager
  4. font = {
  5. 'family':'YouYuan',
  6. 'weight':'bold',
  7. 'size':10
  8. }
  9. #give the data axis x and y
  10. a = ["猩球崛起3:终极之战","敦刻尔克","蜘蛛侠:英雄归来","战狼2"]
  11. b_14 = [2358,399,2358,362]
  12. b_15 = [12357,156,2045,168]
  13. b_16 = [15746,312,4497,319]
  14. #set the size of the graphic
  15. plt.figure(figsize=(20,8),dpi=80)
  16. #change the pos of the scale
  17. x_14 = range(len(a))
  18. x_15 = [i+bar_width for i in x_14]#clost to just right
  19. x_16 = [i+bar_width for i in x_15]
  20. #trans the data collections into
  21. plt.bar(x_14,b_14,width = bar_width,label="14日")#first one is scale
  22. plt.bar(x_15,b_15,width = bar_width,label="15日")
  23. plt.bar(x_16,b_16,width = bar_width,label="16日")
  24. plt.legend(loc="upper left")
  25. #give the scale parameters
  26. plt.xticks(x_15,a)
  27. plt.show()

 

 

 

  1. from matplotlib import pyplot as plt
  2. from matplotlib import font_manager
  3. import random
  4. font = {
  5. 'family':'YouYuan',
  6. 'weight':'bold',
  7. 'size':1
  8. }
  9. random.seed(0)
  10. a = [random.randint(70,154) for i in range(120)]
  11. a.append(160)
  12. a.append(60)
  13. plt.figure(figsize = (20,80),dpi = 80)
  14. #calculation 1.组数==极差/组距 2.组距==极差/组数
  15. #1.
  16. #fix the num_bins then calculate the distance
  17. #tip: the (max(a)-min(a))/num_bins must == 0
  18. num_bins=20
  19. d = (max(a)-min(a))//num_bins
  20. #draw the picture
  21. plt.hist(a,num_bins)#原始数组a和分组
  22. #fix the scale of axis a
  23. plt.xticks(range(min(a),max(a)+d,d))
  24. plt.grid()
  25. plt.show()

 

 

  1. import random
  2. from matplotlib import pyplot as plt
  3. from matplotlib import font_manager
  4. font = {
  5. 'family':'YouYuan',
  6. 'weight':'bold',
  7. 'size':1
  8. }
  9. random.seed(0)
  10. a = [random.randint(70,154) for i in range(120)]
  11. a.append(160)
  12. a.append(60)
  13. plt.figure(figsize = (20,80),dpi = 80)
  14. #计算组数 组数==极差/组距
  15. #将hist中第2个参数的值写为plt.xticks中的值 然后组距可以随便调
  16. #其实就是一个刻度就是一组 然后用d的值分出来所需要的组数
  17. d = 5
  18. plt.hist(a,range(min(a),max(a)+d,d))#原始数组a和分组
  19. #设置x轴的刻度
  20. plt.xticks(range(min(a),max(a)+d,d))#刚好包含max(a)
  21. #画网格
  22. plt.grid()
  23. plt.show()

 

  1. #coding=UTF-8
  2. from matplotlib import pyplot as plt
  3. from matplotlib import font_manager
  4. #这是统计过的所以用bar条形图 不连在一起
  5. #没有统计过用hist直方图 连在一起
  6. interval = [0,5,10,15,20,25,30,35,40,45,60,90]
  7. width = [5,5,5,5,5,5,5,5,5,15,30,60]
  8. quantity = [836,2737,3723,3926,3596,1438,3273,642,824,613,215,47]
  9. plt.bar(range(len(quantity)),quantity,width=1)#width为1可以让条形图连在一起
  10. #设置x轴的刻度
  11. _x = [i*0.5 for i in range(len(interval)+1)]# 分成了多少份
  12. _xtick_labels = interval+[150] #额外加上一个元素
  13. plt.xticks(_x,_xtick_labels)
  14. plt.grid(alpha = 0.4)
  15. plt.show()