用python绘制图形_python绘制图形

1 '''

2 File Name: draw3 Author: tim4 Date: 2018/8/15 16:475 Description: 图形绘制。十分有用,对于工作中实验性的项目,可以快速展示效果。如果使用java,还需要配合前端展示。6 '''

7

8 importmatplotlib.pyplot as plt9 import numpy as np #模块取别名

10

11

12 #直方图

13 defdraw_hist():14 mu = 100

15 sigma = 20

16

17 x = mu + sigma * np.random.randn(20000) #样本数量

18 plt.hist(x, bins=100, color='green', normed=True) #bins:显示有几个直方,normed是否对数据进行标准化

19

20 plt._show()21

22

23 #条形图

24 defdraw_bar():25 y = [20, 10, 30, 25, 15] #Y轴数据

26 index = np.arange(5) #X轴数据,也可以是index = [0,5]

27

28 plt.bar(left=index, height=y, color='blue', width=0.5)29 plt.show()30

31

32 #折线图

33 defdraw_plot():34 x = np.linspace(-10, 10, 100) #-10到10,100个点

35 y = x ** 3 #x的3次幂

36

37 plt.plot(x, y, linestyle='--', color='orange', marker='<')38 plt.xlabel('X')39 plt.ylabel('Y')40

41 plt.show()42

43

44 #散点图

45 defdraw_scatter():46 x = np.random.randn(1000)47 y = x + np.random.randn(1000) * 0.5

48

49 plt.scatter(x, y, s=5, marker='<') #s表示面积,marker表示图形

50 plt.show()51

52

53 #饼状图

54 defdraw_pie():55 labels = 'A', 'B', 'C', 'D' #4个模块

56 fracs = [15, 30, 45, 10] #每个模块占比例

57

58 plt.axes(aspect=1) #使x、y轴比例相同

59 explode = [0, 0.5, 0, 0] #突出某一部分区域

60

61 plt.pie(x=fracs, labels=labels, autopct='%.0f%%', explode=explode) #autopct显示百分比

62 plt.show()63

64

65 #带图例

66 defdraw_with_legend():67 x = np.arange(1, 11, 1) #x轴坐标,1开始,11结束,步长为1

68

69 plt.plot(x, x * 2) #第一条线,x,y坐标

70 plt.plot(x, x * 3)71 plt.plot(x, x * 4)72

73 plt.legend(['Normal', 'Fast', 'Faster']) #设置图例,与上面的线对应

74 plt.grid(True, color='green', linestyle='--', linewidth=1) #绘制网格

75

76 plt.show()77

78

79 #start

80 if __name__ == '__main__':81 #draw_hist()

82 #draw_bar()

83 draw_plot()84 #draw_scatter()

85 #draw_pie()

86 #draw_with_legend()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值