鱼C论坛

 找回密码
 立即注册
查看: 2502|回复: 11

[已解决]请教大神

[复制链接]
发表于 2023-3-31 19:56:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
算质量为100千克的物品,在太阳系的各个行星上重量分别是多少?画出对比图。(直方图、折线图或其它图形均可)
最佳答案
2023-3-31 20:13:11
这是一个涉及物理学和数据可视化的问题,我们可以通过使用Python中的科学计算库NumPy和可视化库Matplotlib来解决它。

以下是一个可能的解决方案:

运行以下代码前请先安装 numpy 和 matplotlib
  1. import numpy as np
  2. import matplotlib.pyplot as plt

  3. # 各行星的质量倍数
  4. mass_ratios = np.array([0.166, 0.377, 0.638, 0.91, 1.0, 1.886, 2.530, 8.871])
  5. # 各行星的名字
  6. planet_names = ['Mercury', 'Venus', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto']

  7. # 计算物品在各行星上的重量
  8. weight_on_planets = mass_ratios * 100  # 单位是千克

  9. # 绘制直方图
  10. fig, ax = plt.subplots()
  11. ax.bar(planet_names, weight_on_planets)
  12. ax.set_xlabel('Planet')
  13. ax.set_ylabel('Weight (kg)')
  14. ax.set_title('Weight of a 100 kg object on different planets')
  15. plt.show()

  16. # 绘制折线图
  17. fig, ax = plt.subplots()
  18. ax.plot(planet_names, weight_on_planets, marker='o')
  19. ax.set_xlabel('Planet')
  20. ax.set_ylabel('Weight (kg)')
  21. ax.set_title('Weight of a 100 kg object on different planets')
  22. plt.show()
复制代码



这个代码会产生两个图形,第一个是直方图,第二个是折线图。它们分别显示了质量为100千克的物品在每个行星上的重量,并比较了它们之间的差异。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-3-31 20:03:26 From FishC Mobile | 显示全部楼层
你什么都不说清楚,用哪个库画,公式是什么,你的思路呢?
啥都没有就来问,特别过分
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-3-31 20:13:11 | 显示全部楼层    本楼为最佳答案   
这是一个涉及物理学和数据可视化的问题,我们可以通过使用Python中的科学计算库NumPy和可视化库Matplotlib来解决它。

以下是一个可能的解决方案:

运行以下代码前请先安装 numpy 和 matplotlib
  1. import numpy as np
  2. import matplotlib.pyplot as plt

  3. # 各行星的质量倍数
  4. mass_ratios = np.array([0.166, 0.377, 0.638, 0.91, 1.0, 1.886, 2.530, 8.871])
  5. # 各行星的名字
  6. planet_names = ['Mercury', 'Venus', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto']

  7. # 计算物品在各行星上的重量
  8. weight_on_planets = mass_ratios * 100  # 单位是千克

  9. # 绘制直方图
  10. fig, ax = plt.subplots()
  11. ax.bar(planet_names, weight_on_planets)
  12. ax.set_xlabel('Planet')
  13. ax.set_ylabel('Weight (kg)')
  14. ax.set_title('Weight of a 100 kg object on different planets')
  15. plt.show()

  16. # 绘制折线图
  17. fig, ax = plt.subplots()
  18. ax.plot(planet_names, weight_on_planets, marker='o')
  19. ax.set_xlabel('Planet')
  20. ax.set_ylabel('Weight (kg)')
  21. ax.set_title('Weight of a 100 kg object on different planets')
  22. plt.show()
复制代码



这个代码会产生两个图形,第一个是直方图,第二个是折线图。它们分别显示了质量为100千克的物品在每个行星上的重量,并比较了它们之间的差异。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2023-3-31 20:15:50 | 显示全部楼层
isdkz 发表于 2023-3-31 20:13
这是一个涉及物理学和数据可视化的问题,我们可以通过使用Python中的科学计算库NumPy和可视化库Matplotlib ...

谢谢大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-3-31 20:20:11 | 显示全部楼层
isdkz 发表于 2023-3-31 20:13
这是一个涉及物理学和数据可视化的问题,我们可以通过使用Python中的科学计算库NumPy和可视化库Matplotlib ...

但是要是没有numpy库怎么办,还可以解决吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-3-31 20:22:41 | 显示全部楼层
isdkz 发表于 2023-3-31 20:13
这是一个涉及物理学和数据可视化的问题,我们可以通过使用Python中的科学计算库NumPy和可视化库Matplotlib ...

大佬,能不能直接用turtle库搞,最后结果绘制出折线图或者柱状图就行了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-3-31 20:23:32 | 显示全部楼层
一张不够花、 发表于 2023-3-31 20:20
但是要是没有numpy库怎么办,还可以解决吗
  1. pip一下
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-3-31 20:25:00 | 显示全部楼层
一张不够花、 发表于 2023-3-31 20:20
但是要是没有numpy库怎么办,还可以解决吗

  1. pip install numpy -i https://mirrors.aliyun.com/pypi/simple
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-3-31 20:31:10 | 显示全部楼层
一张不够花、 发表于 2023-3-31 20:22
大佬,能不能直接用turtle库搞,最后结果绘制出折线图或者柱状图就行了

用 turtle 也可以,不过不好看

直方图:
  1. import turtle

  2. # 各行星的质量倍数
  3. mass_ratios = [0.166, 0.377, 0.638, 0.91, 1.0, 1.886, 2.530, 8.871]
  4. # 各行星的名字
  5. planet_names = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto']

  6. # 计算物品在各行星上的重量
  7. weights = [r * 100 for r in mass_ratios]

  8. # 创建Turtle对象并设置属性
  9. t = turtle.Turtle()
  10. t.speed(0)
  11. t.pensize(2)

  12. # 绘制直方图
  13. for i, w in enumerate(weights):
  14.     t.penup()
  15.     t.goto(i * 50 - 200, 0)
  16.     t.pendown()
  17.     t.left(90)
  18.     t.forward(w)
  19.     t.write(f"{w:.2f}")
  20.     t.right(90)
  21.     t.forward(20)
  22.     t.right(90)
  23.     t.forward(w)
  24.     t.left(90)

  25. # 绘制横坐标和纵坐标
  26. t.penup()
  27. t.goto(-250, -150)
  28. t.pendown()
  29. t.forward(500)
  30. t.left(180)
  31. t.forward(10)
  32. t.right(90)
  33. t.forward(300)
  34. t.write('Weight (kg)')
  35. t.penup()
  36. t.goto(-200, -170)
  37. t.write('Planets')

  38. for i, p in enumerate(planet_names):
  39.     t.penup()
  40.     t.goto(i * 50 - 200, -200)
  41.     t.write(p)

  42. # 点击屏幕关闭窗口
  43. turtle.done()
复制代码


折线图:
  1. import turtle

  2. # 各行星的质量倍数
  3. mass_ratios = [0.166, 0.377, 0.638, 0.91, 1.0, 1.886, 2.530, 8.871]
  4. # 各行星的名字
  5. planet_names = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto']

  6. # 计算物品在各行星上的重量
  7. weights = [r * 100 for r in mass_ratios]

  8. # 创建Turtle对象并设置属性
  9. t = turtle.Turtle()
  10. t.speed(0)
  11. t.pensize(3)

  12. # 绘制折线图
  13. for i, w in enumerate(weights):
  14.     t.penup()
  15.     t.goto(i * 50 - 200, w)
  16.     t.pendown()
  17.     t.dot(10)
  18.     t.write(f"{w:.2f}")
  19.     if i < len(weights) - 1:
  20.         next_w = weights[i+1]
  21.         t.goto((i+1) * 50 - 200, next_w)

  22. # 绘制横坐标和纵坐标
  23. t.penup()
  24. t.goto(-250, -150)
  25. t.pendown()
  26. t.forward(500)
  27. t.left(180)
  28. t.forward(10)
  29. t.right(90)
  30. t.forward(300)
  31. t.write('Weight (kg)')
  32. t.penup()
  33. t.goto(-200, -170)
  34. t.write('Planets')

  35. for i, p in enumerate(planet_names):
  36.     t.penup()
  37.     t.goto(i * 50 - 200, -200)
  38.     t.write(p)

  39. # 点击屏幕关闭窗口
  40. turtle.done()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-3-31 20:44:30 | 显示全部楼层
一张不够花、 发表于 2023-3-31 20:22
大佬,能不能直接用turtle库搞,最后结果绘制出折线图或者柱状图就行了

画这种数学上的图还是得用matplotlib这种专业的

turtle画图不好控制,一般画不好看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-3-31 20:59:04 | 显示全部楼层
isdkz 发表于 2023-3-31 20:44
画这种数学上的图还是得用matplotlib这种专业的

turtle画图不好控制,一般画不好看

嗷,好的好的 谢谢大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-3-31 21:00:58 | 显示全部楼层
一张不够花、 发表于 2023-3-31 20:59
嗷,好的好的 谢谢大佬


不客气,我在上面也发了turtle的,你可以拿去运行看看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-28 13:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表