《Python数学编程》练习-day013: matplotlib(3)

《Python数学编程》练习-day013: matplotlib(3)

提示→《Python数学编程》练习系列往期笔记,如下 👇:

Day1:《Python数学编程》练习-day001:斐波那契序列
Day2:《Python数学编程》练习-day002:分数操作
Day3:《Python数学编程》练习-day003:基本数学运算
Day4:《Python书序编程》练习-day004:复数
Day5:《Python书序编程》练习-day005:数值输入
Day6:《Python书序编程》练习-day006:计算整数因子
Day7:《Python书序编程》练习-day007:生成乘法表
Day8:《Python书序编程》练习-day008:测量单位转换
Day9:《Python书序编程》练习-day009:求解二次方程的根
Day11:《Python书序编程》练习-day011:matplotlib(1)
Day12:《Python书序编程》练习-day011:matplotlib(2)


提示:菜鸟一枚,此系列文主要是用于我自己的学习记录,如果能对您有帮助,我荣幸至极。

《Python数学编程》练习-day011: matplotlib(1)

前言

今天这篇笔记主要记录我在使用jupyter notebook,用matplotlib绘图出现的空图层问题——知其所以然:

  1. 绘制公式图形;
  2. 为什么使用matplot.use() ;
  3. TkAgg是什么,解决哪些问题;
  4. matplotlib其他的渲染组合。

提示:以下是本篇文章正文内容,下面案例可供参考

一、绘制牛顿万有引力定律公式图形

牛 顿 万 有 引 力 定 律 公 式 : 牛顿万有引力定律公式:

  F = G m 1 m 2 r 2 \ F=\frac{Gm_1m_2 }{r^2}  F=r2Gm1m2

这里主要绘制引力与距离的关系,因此质量要是定值。
实现代码如下:

# 牛顿万有引力
# 实现质量为m1、m2两物体随距离变化,两者之间引力之间的变化——即是引力与距离之间的关系
# 实现步骤:1.绘制图形;2.定义引力计算公式:1)距离生成;2)距离对应的引力

import matplotlib.pyplot as plt
import matplotlib
# 如果您的脚本依赖于特定的后端,则可以使用功能matplotlib.use():
# Agg rendering to a Tk canvas (requires TkInter). This backend can be activated in IPython with %matplotlib tk.
matplotlib.use('TkAgg')

#Drew the Graph/定义一个绘图函数用于画图
def draw_graph(x, y):
    plt.plot(x, y, marker = 'o')
    plt.xlabel('Distance in meters(m)')
    plt.ylabel('Gravitational force and distance(N)')
    plt.show()
    plt.savefig('C:/Users/zhangfei/Desktop/python_math/force_distance2.png')
    
# 定义引力公式和距离
def generate_F_r():
    #Generate value for r(距离), m
    r = range(20, 1001, 50)
    
    # enpty list to store the calculated values of F, N
    F = []
    
    # Constant, G:引力常数 Nm**2kg**-2
    G = 6.674*(10**-11)
    
    # two masses, kg
    m1 = 0.5
    m2 = 1.5
    
    # Calculate force and add it to the list, F:计算引力
    for dist in r:
        force = G*(m1*m2)/(dist**2)
        F.append(force)
        
    # call the draw_graph function(调用画图函数)
    draw_graph(r, F)
    
if __name__ == '__main__':
    generate_F_r()

运行结果如下:
在这里插入图片描述未使用matplotlib.use(‘TkAgg’)时,导出的图片(一片空白):
在这里插入图片描述

二、为什么使用matplot.use() ?

为什么要使用为什么使用matplot.use() ,官方Usage Guide这样说明的:

  1. If your script depends on a specific backend you can use the function matplotlib.use():
    This should be done before any figure is created, otherwise Matplotlib may fail to switch the backend and raise an ImportError.
    Using use will require changes in your code if users want to use a different backend. Therefore, you should avoid explicitly calling use unless absolutely necessary.

三、TkAgg是什么,解决哪些问题?

1.TkAgg:

Agg rendering to a Tk canvas (requires TkInter). This backend can be activated in IPython with %matplotlib tk.

这解释为何之前导出的图片上未显示绘制的内容,是因为绘制内容没有渲染到画布上所导致的。

四、matplotlib其他的渲染组合?

除了TkAgg之外,matplotlib好有其他渲染组合:
在这里插入图片描述这里是原地址:Usage Guide(This tutorial covers some basic usage patterns and best-practices to help you get started with Matplotlib.)

总结

使用matplotlib:

  1. 绘制公式图形;
  2. 为什么使用matplot.use() ;
  3. TkAgg是什么,解决哪些问题;
  4. matplotlib其他的渲染组合。
  5. matplotlib使用说明。
  6. 不要惧怕问题,同样不要仅仅知道解决问题的方法,还要弄懂其背后的原理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Z-Code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值