您当前的位置:首页 > IT编程 > python
| C语言 | Java | VB | VC | python | Android | TensorFlow | C++ | oracle | 学术与代码 | cnn卷积神经网络 | gnn | 图像修复 | Keras | 数据集 | Neo4j | 自然语言处理 | 深度学习 | 医学CAD | 医学影像 | 超参数 | pointnet | pytorch | 异常检测 | Transformers | 情感分类 | 知识图谱 |

自学教程:Python 图形绘制详细代码(二)

51自学网 2022-02-21 10:44:25
  python
这篇教程Python 图形绘制详细代码(二)写得很实用,希望能帮到您。

4、条形图

下面介绍条形图的画法。

4.1 代码

import matplotlib.pyplot as plt# x-coordinates of left sides of barsleft = [1, 2, 3, 4, 5]# heights of barsheight = [10, 24, 36, 40, 5]# labels for barstick_label = ['one', 'two', 'three', 'four', 'five']# plotting a bar chartplt.bar(left, height, tick_label = tick_label,        width = 0.8, color = ['red', 'green'])# naming the x-axisplt.xlabel('x - axis')# naming the y-axisplt.ylabel('y - axis')# plot titleplt.title('My bar chart!')# function to show the plotplt.show()

4.2 输出

4.3 代码的部分解释

  • 1)使用 plt.bar() 函数来绘制条形图。
  • 2)x轴与height两个参数必须有。
  • 3)可以通过定义 tick_labels 为 x 轴坐标指定另外的名称。

5、直方图

5.1 代码

import matplotlib.pyplot as plt# frequenciesages = [2,5,70,40,30,45,50,45,43,40,44,        60,7,13,57,18,90,77,32,21,20,40]# setting the ranges and no. of intervalsrange = (0, 100)bins = 10 # plotting a histogramplt.hist(ages, bins, range, color = 'green',        histtype = 'bar', rwidth = 0.8)# x-axis labelplt.xlabel('age')# frequency labelplt.ylabel('No. of people')# plot titleplt.title('My histogram')# function to show the plotplt.show()

5.2 输出

5.3 代码的部分解释

万事OK自学网:51自学网_软件自学网_CAD自学网自学excel、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。