matplotlib模块

匿名 (未验证) 提交于 2019-12-02 23:32:01

matplotlib官方文档:https://matplotlib.org/contents.html?v=20190307135750

matplotlib是一个绘图库,它可以创建常用的统计图,包括条形图、箱型图、折线图、散点图和直方图。

import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties %matplotlib inline font = FontProperties(fname='/Library/Fonts/Heiti.ttc')  # 修改背景为条纹 plt.style.use('ggplot')  classes = ['3班', '4班', '5班', '6班']  classes_index = range(len(classes)) print(list(classes_index))
[0, 1, 2, 3]
student_amounts = [66, 55, 45, 70]  # 画布设置 fig = plt.figure() # 1,1,1表示一张画布切割成1行1列共一张图的第1个;2,2,1表示一张画布切割成2行2列共4张图的第一个(左上角) ax1 = fig.add_subplot(1, 1, 1) ax1.bar(classes_index, student_amounts, align='center', color='darkblue') ax1.xaxis.set_ticks_position('bottom') ax1.yaxis.set_ticks_position('left')  plt.xticks(classes_index, classes, rotation=0,            fontsize=13, fontproperties=font) plt.xlabel('班级', fontproperties=font, fontsize=15) plt.ylabel('学生人数', fontproperties=font, fontsize=15) plt.title('班级-学生人数', fontproperties=font, fontsize=20) # 保存图片,bbox_inches='tight'去掉图形四周的空白 # plt.savefig('classes_students.png', dpi=400, bbox_inches='tight') plt.show()

import numpy as np import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties %matplotlib inline font = FontProperties(fname='/Library/Fonts/Heiti.ttc')  # 修改背景为条纹 plt.style.use('ggplot')  mu1, mu2, sigma = 50, 100, 10 # 构造均值为50的符合正态分布的数据 x1 = mu1+sigma*np.random.randn(10000) print(x1)
[59.00855949 43.16272141 48.77109774 ... 57.94645859 54.70312714  58.94125528]
# 构造均值为100的符合正态分布的数据 x2 = mu2+sigma*np.random.randn(10000) print(x2)
[115.19915511  82.09208214 110.88092454 ...  95.0872103  104.21549068  133.36025251]
fig = plt.figure() ax1 = fig.add_subplot(121) # bins=50表示每个变量的值分成50份,即会有50根柱子 ax1.hist(x1, bins=50, color='darkgreen')  ax2 = fig.add_subplot(122) ax2.hist(x2, bins=50, color='orange')  fig.suptitle('两个正态分布', fontproperties=font, fontweight='bold', fontsize=15) ax1.set_title('绿色的正态分布', fontproperties=font) ax2.set_title('橙色的正态分布', fontproperties=font) plt.show()

import numpy as np from numpy.random import randn import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties %matplotlib inline font = FontProperties(fname='/Library/Fonts/Heiti.ttc')  # 修改背景为条纹 plt.style.use('ggplot')  np.random.seed(1)  # 使用numpy的累加和,保证数据取值范围不会在(0,1)内波动 plot_data1 = randn(40).cumsum() print(plot_data1)
[ 1.62434536  1.01258895  0.4844172  -0.58855142  0.2768562  -2.02468249  -0.27987073 -1.04107763 -0.72203853 -0.97140891  0.49069903 -1.56944168  -1.89185888 -2.27591324 -1.1421438  -2.24203506 -2.41446327 -3.29232169  -3.25010794 -2.66729273 -3.76791191 -2.6231882  -1.72159748 -1.21910314  -0.31824719 -1.00197505 -1.12486527 -2.06063471 -2.32852279 -1.79816732  -2.48982807 -2.8865816  -3.5737543  -4.41895994 -5.09020607 -5.10287067  -6.22018102 -5.98576532 -4.32596314 -3.58391898]
plot_data2 = randn(40).cumsum() plot_data3 = randn(40).cumsum() plot_data4 = randn(40).cumsum()  plt.plot(plot_data1, marker='o', color='red', linestyle='-', label='红实线') plt.plot(plot_data2, marker='x', color='orange', linestyle='--', label='橙虚线') plt.plot(plot_data3, marker='*', color='yellow', linestyle='-.', label='黄点线') plt.plot(plot_data4, marker='s', color='green', linestyle=':', label='绿点图')  # loc='best'给label自动选择最好的位置 plt.legend(loc='best', prop=font) plt.show()

import numpy as np from numpy.random import randn import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties %matplotlib inline font = FontProperties(fname='/Library/Fonts/Heiti.ttc')  # 修改背景为条纹 plt.style.use('ggplot')   x = np.arange(1, 20, 1) print(x)
[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]
# 拟合一条水平散点线 np.random.seed(1) y_linear = x+10*np.random.randn(19) print(y_linear)
[ 17.24345364  -4.11756414  -2.28171752  -6.72968622  13.65407629  -17.01538697  24.44811764   0.38793099  12.19039096   7.50629625   25.62107937  -8.60140709   9.77582796  10.15945645  26.33769442    5.00108733  15.27571792   9.22141582  19.42213747]
 
[  6.82815214  -7.00619177  20.4472371   25.01590721  30.02494339   45.00855949  42.16272141  62.77109774  71.64230566  97.3211192  126.30355467 137.08339248 165.03246473 189.128273   216.54794359  249.28753869 288.87335401 312.82689651 363.34415698]
# s是散点大小 fig = plt.figure() ax1 = fig.add_subplot(121) plt.scatter(x, y_linear, s=30, color='r', label='蓝点') plt.scatter(x, y_quad, s=100, color='b', label='红点')  ax2 = fig.add_subplot(122) plt.plot(x, y_linear, color='r') plt.plot(x, y_quad, color='b')  # 限制x轴和y轴的范围取值 plt.xlim(min(x)-1, max(x)+1) plt.ylim(min(y_quad)-10, max(y_quad)+10) fig.suptitle('散点图+直线图', fontproperties=font, fontsize=20) ax1.set_title('散点图', fontproperties=font) ax1.legend(prop=font) ax2.set_title('直线图', fontproperties=font) plt.show()

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!