gpt4 book ai didi

python - matplotlib:有轴保持率

转载 作者:太空宇宙 更新时间:2023-11-04 06:12:50 24 4
gpt4 key购买 nike

我是 matplotlib 的新手,我有一个非常简单(我猜)的问题。

我有一些数据需要用 50x70“单位”(它们是英尺,实际上代表一个房间)的矩形表示,但我似乎无法让 matplotlib 绘制具有相同比例的矩形在两个轴上并同时保持 50x70“尺寸”。

我试过以下方法:

import json
import matplotlib
import os
import sys
import traceback
import matplotlib.pyplot as plt

DATA_FILE = os.path.join(os.path.expanduser("~"), "results.json")
FLOOR_DIMENSIONS = (50, 70)

if __name__ == "__main__":
if len(sys.argv) > 1:
DATA_FILE = os.path.abspath(sys.argv[0])
print "Gonna see what happens with file %s" % DATA_FILE
try:
with open(DATA_FILE, 'r') as f:
result_dict = json.load(f)
except (IOError, OSError, ValueError), e:
print "Received %s %s when trying to parse json from %s\n"\
"Showing traceback: %s" % (type(e), e, DATA_FILE, traceback.format_exc())
result_dict = {}
for d_mac in result_dict:
data = result_dict[d_mac]
if len(data) < 3:
continue
x_s = list(d['x'] for d in data)
y_s = list(d['y'] for d in data)
plt.scatter(x_s, y_s, marker='o', c=numpy.random.rand(5,1), s=15)
plt.xlim([0, FLOOR_DIMENSIONS[0]])
plt.ylim([0, FLOOR_DIMENSIONS[1]])
#plt.axis('equal')
plt.show()
sys.exit(0)

这样做,我得到:

enter image description here

将我的数据绘制在一个正方形内,改变 X-Y 比例(X 是 50 点,Y 是 70,因此 Y 显示“缩小”)

我尝试的另一个选项是取消注释 plt.axis('equal') 行,但这会“切断”Y 轴(不是从 0 开始到 70 结束,而是开始在 15 和 55 结束,可能是因为没有 y < 15 和 y > 55 的数据)

enter image description here

但我也不想要那个,我想要从 Y=0 开始到 Y=70 结束的“ Canvas ”,如果没有数据就显示一个空白区域。

我需要画这样的东西:

enter image description here

这是我通过手动调整渲染绘图的窗口大小得到的 :-D

提前致谢!

最佳答案

添加 plt.axis('scaled')

编辑:axis('image') 可能更适合您的需求。

更多轴设置可以在the documentation中找到.

import matplotlib.pyplot as plt
import numpy as np

xs = np.arange(50)
ys = (np.random.random(50)*70) + 15

plt.scatter(xs,ys)

plt.axis('image')
plt.axis([0, 50, 0, 70])

plt.show()

给出:

correct

在更新的示例中,我知道 ys 实际上有最大值 ~85,偏移量只是为了演示适当的轴执行。

关于python - matplotlib:有轴保持率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17751323/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com