请教大神
算质量为100千克的物品,在太阳系的各个行星上重量分别是多少?画出对比图。(直方图、折线图或其它图形均可) 你什么都不说清楚,用哪个库画,公式是什么,你的思路呢?啥都没有就来问,特别过分 这是一个涉及物理学和数据可视化的问题,我们可以通过使用Python中的科学计算库NumPy和可视化库Matplotlib来解决它。
以下是一个可能的解决方案:
运行以下代码前请先安装 numpy 和 matplotlib
import numpy as np
import matplotlib.pyplot as plt
# 各行星的质量倍数
mass_ratios = np.array()
# 各行星的名字
planet_names = ['Mercury', 'Venus', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto']
# 计算物品在各行星上的重量
weight_on_planets = mass_ratios * 100# 单位是千克
# 绘制直方图
fig, ax = plt.subplots()
ax.bar(planet_names, weight_on_planets)
ax.set_xlabel('Planet')
ax.set_ylabel('Weight (kg)')
ax.set_title('Weight of a 100 kg object on different planets')
plt.show()
# 绘制折线图
fig, ax = plt.subplots()
ax.plot(planet_names, weight_on_planets, marker='o')
ax.set_xlabel('Planet')
ax.set_ylabel('Weight (kg)')
ax.set_title('Weight of a 100 kg object on different planets')
plt.show()
这个代码会产生两个图形,第一个是直方图,第二个是折线图。它们分别显示了质量为100千克的物品在每个行星上的重量,并比较了它们之间的差异。
isdkz 发表于 2023-3-31 20:13
这是一个涉及物理学和数据可视化的问题,我们可以通过使用Python中的科学计算库NumPy和可视化库Matplotlib ...
谢谢大佬 isdkz 发表于 2023-3-31 20:13
这是一个涉及物理学和数据可视化的问题,我们可以通过使用Python中的科学计算库NumPy和可视化库Matplotlib ...
但是要是没有numpy库怎么办,还可以解决吗
isdkz 发表于 2023-3-31 20:13
这是一个涉及物理学和数据可视化的问题,我们可以通过使用Python中的科学计算库NumPy和可视化库Matplotlib ...
大佬,能不能直接用turtle库搞,最后结果绘制出折线图或者柱状图就行了
一张不够花、 发表于 2023-3-31 20:20
但是要是没有numpy库怎么办,还可以解决吗
pip一下 一张不够花、 发表于 2023-3-31 20:20
但是要是没有numpy库怎么办,还可以解决吗
pip install numpy -i https://mirrors.aliyun.com/pypi/simple 一张不够花、 发表于 2023-3-31 20:22
大佬,能不能直接用turtle库搞,最后结果绘制出折线图或者柱状图就行了
用 turtle 也可以,不过不好看
直方图:
import turtle
# 各行星的质量倍数
mass_ratios =
# 各行星的名字
planet_names = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto']
# 计算物品在各行星上的重量
weights =
# 创建Turtle对象并设置属性
t = turtle.Turtle()
t.speed(0)
t.pensize(2)
# 绘制直方图
for i, w in enumerate(weights):
t.penup()
t.goto(i * 50 - 200, 0)
t.pendown()
t.left(90)
t.forward(w)
t.write(f"{w:.2f}")
t.right(90)
t.forward(20)
t.right(90)
t.forward(w)
t.left(90)
# 绘制横坐标和纵坐标
t.penup()
t.goto(-250, -150)
t.pendown()
t.forward(500)
t.left(180)
t.forward(10)
t.right(90)
t.forward(300)
t.write('Weight (kg)')
t.penup()
t.goto(-200, -170)
t.write('Planets')
for i, p in enumerate(planet_names):
t.penup()
t.goto(i * 50 - 200, -200)
t.write(p)
# 点击屏幕关闭窗口
turtle.done()
折线图:
import turtle
# 各行星的质量倍数
mass_ratios =
# 各行星的名字
planet_names = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto']
# 计算物品在各行星上的重量
weights =
# 创建Turtle对象并设置属性
t = turtle.Turtle()
t.speed(0)
t.pensize(3)
# 绘制折线图
for i, w in enumerate(weights):
t.penup()
t.goto(i * 50 - 200, w)
t.pendown()
t.dot(10)
t.write(f"{w:.2f}")
if i < len(weights) - 1:
next_w = weights
t.goto((i+1) * 50 - 200, next_w)
# 绘制横坐标和纵坐标
t.penup()
t.goto(-250, -150)
t.pendown()
t.forward(500)
t.left(180)
t.forward(10)
t.right(90)
t.forward(300)
t.write('Weight (kg)')
t.penup()
t.goto(-200, -170)
t.write('Planets')
for i, p in enumerate(planet_names):
t.penup()
t.goto(i * 50 - 200, -200)
t.write(p)
# 点击屏幕关闭窗口
turtle.done() 一张不够花、 发表于 2023-3-31 20:22
大佬,能不能直接用turtle库搞,最后结果绘制出折线图或者柱状图就行了
画这种数学上的图还是得用matplotlib这种专业的
turtle画图不好控制,一般画不好看 isdkz 发表于 2023-3-31 20:44
画这种数学上的图还是得用matplotlib这种专业的
turtle画图不好控制,一般画不好看
嗷,好的好的 谢谢大佬 一张不够花、 发表于 2023-3-31 20:59
嗷,好的好的 谢谢大佬
不客气,我在上面也发了turtle的,你可以拿去运行看看
页:
[1]