博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
day27-3 matplatlib模块
阅读量:7048 次
发布时间:2019-06-28

本文共 3494 字,大约阅读时间需要 11 分钟。

目录

matplotlib

  • 图形可视化,主要用来画图
  • 别问,问就是看不懂

条形图

import matplotlib.pyplot as plt# 只识别英语,所以通过以下两行增加中文字体from matplotlib.font_manager import FontProperties# 字体路径根据电脑而定font = FontProperties(fname='M:\STKAITI.TTF')# jupyter 默认不显示图片,通过这一行告诉他显示%matplotlib inlineclasses = ['1班', '2班', '3班', '4班'] # 相当于columnsstudent_amounts = [30, 20, 30, 40]   # 值classes_index = range(len(classes))    # [0, 1, 2, 3]plt.bar(classes_index, student_amounts)plt.xticks(classes_index, classes, FontProperties=font)for ind,student_amount in enumerate(student_amounts):    print(ind,student_amount)    plt.text(ind,student_amount+1,student_amount)plt.xlabel('班级', FontProperties=font)plt.ylabel('学生人数', FontProperties=font)plt.title('班级-学生人数', FontProperties=font)plt.show()
0 301 202 303 40

png

## 直方图

import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.font_manager import FontProperties%matplotlib inlinefont = FontProperties(fname='M:\STKAITI.TTF')mu1, mu2, sigma = 50, 100, 10x1 = mu2 + sigma * np.random.randn(10000)print(x1)
[ 93.49947877  86.87378653  98.0194217  ... 108.33555519  90.58512015 102.19048574]
x1 = np.random.randn(10000)print(x1)
[ 0.85927045 -0.8061112   1.30878058 ... -0.32700199 -0.67669564  0.25750884]
x2 = mu2 + sigma*np.random.randn(10000)print(x2)
[101.62589858 109.86489987 117.41374105 ...  97.52364544 107.21076273  99.56765772]
plt.hist(x1, bins=100)plt.hist(x2, bins=100)plt.show()

png

plt.style.use('ggplot')fig = plt.figure()# 相当于把一整块画板分成了1行2列的两个画板ax1 = fig.add_subplot(121)ax1.hist(x1, bins=100, color='red')ax1.set_title('红色', fontproperties=font)ax2 = fig.add_subplot(122)ax2.hist(x2, bins=100, color='yellow')ax2.set_title('黄色', fontproperties=font)fig.suptitle('大标题', fontproperties=font, fontsize=15, weight='bold')plt.show()

png

折线图

import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.font_manager import FontProperties%matplotlib inlinefont = FontProperties(fname='M:\STKAITI.TTF')
plt.style.use('ggplot')np.random.seed(1)data1 = np.random.rand(40).cumsum()data2 = np.random.rand(40).cumsum()data3 = np.random.rand(40).cumsum()data4 = np.random.rand(40).cumsum()
plt.plot(data1, color='r', linestyle='-', alpha=0.5, label='红色')plt.plot(data2, color='green', linestyle='--', label='绿色')plt.plot(data3, color='yellow', linestyle=':', label='黄色')plt.plot(data4, color='blue', linestyle='-.', label='蓝色')plt.legend(prop=font)plt.show()

png

arr = np.array([1, 2, 3, 4])arr.cumsum()#   1,1+2,1+2+3,1+2+3+4
array([ 1,  3,  6, 10], dtype=int32)

散点图

import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.font_manager import FontProperties%matplotlib inlinefont = FontProperties(fname='M:\STKAITI.TTF')
x = np.arange(1, 20)x
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,       18, 19])
y_linear = x**2y_linear
array([  1,   4,   9,  16,  25,  36,  49,  64,  81, 100, 121, 144, 169,       196, 225, 256, 289, 324, 361], dtype=int32)
y_log = np.log(x)y_log
array([0.        , 0.69314718, 1.09861229, 1.38629436, 1.60943791,       1.79175947, 1.94591015, 2.07944154, 2.19722458, 2.30258509,       2.39789527, 2.48490665, 2.56494936, 2.63905733, 2.7080502 ,       2.77258872, 2.83321334, 2.89037176, 2.94443898])
fig = plt.figure()ax1 = fig.add_subplot(311)ax1.scatter(x, y_linear, color='red', marker='o', s=100)ax1.scatter(x, y_log, color='blue', marker='*', s=30)ax1.set_title('scatter')ax2 = fig.add_subplot(313)ax2.plot(x, y_linear)ax2.plot(x, y_log)ax2.set_title('plot')plt.plotplt.show()

png

转载于:https://www.cnblogs.com/lucky75/p/11011617.html

你可能感兴趣的文章
贝中斯公式相关知识
查看>>
UseAdaptiveSizePolicy与CMS垃圾回收同时使用导致的JVM报错
查看>>
编程生涯
查看>>
WebStorm设置编辑器中的字体大小
查看>>
JAVA 8 默认方法-Default Methods
查看>>
Eclipse启动时选择workspace设置
查看>>
左图有文本,图片自由缩放
查看>>
SQL Server中的锁
查看>>
js时间戳格式化成日期格式的多种方法
查看>>
gdb fabs错误输出
查看>>
jquery-plugin-biggerLink,highLight-层加亮_andy 阳光生活_百度空间
查看>>
最快破亿还是荣耀,这是年轻人的狂欢
查看>>
西藏世界级英雄史诗《格萨尔》藏译汉项目已出版15部书籍
查看>>
远海星空什么样?远望7号带你看银河
查看>>
深入理解javascript系列(十五):高阶函数
查看>>
客户端动态化系列之——Weex
查看>>
【火炉炼AI】深度学习008-Keras解决多分类问题
查看>>
D-LinkDSP-W215智能插座远程命令执行
查看>>
BCTF Writeup
查看>>
Material Design 控件知识梳理(1) Android Design Support Library 是什么
查看>>