从 json 文件 python 中读取 (x,y) 值对
Reading (x,y) value pairs from a json file python
我有一个 Geojson 文件,其中包含如下内容:
[
{
"type": "MultiLineString",
"coordinates": [
[
[
-118.223243,
34.050979
],
[
-118.223138,
34.050832
],
[
-118.223074,
34.050732
],[
-118.221781,
34.035823
]
]
]
}
我有一个 python 文件,它必须读取文件,将坐标存储到一个变量中,这样我就可以使用该变量中的信息使用 matplotlib
绘制线条
我已经尝试编写代码来读取内容并获取行,但坐标似乎完全不对。没有得到预期的输出。
import matplotlib.pyplot as plt
import json
with open('response.json') as json_file:
m1 = json.load(json_file)
for i in m1:
for j in i["coordinates"]:
for k in j:
for l in k:
plt.plot(l)
plt.show()
预期结果:应根据坐标绘制多条线
实际结果:没有任何反应。根本没有绘制线条
以下是您将如何遍历每对坐标并存储点,然后绘制为 x 与 y。不过,很可能 efficient/better 练习使用 numpy 之类的东西:
points = {"x":[], "y":[]}
for data in m1:
for coordinateList in data["coordinates"]:
for coordinate in coordinateList:
points["x"].append(coordinate[0])
points["y"].append(coordinate[1])
plt.plot(points["x"],points["y"])
我有一个 Geojson 文件,其中包含如下内容:
[
{
"type": "MultiLineString",
"coordinates": [
[
[
-118.223243,
34.050979
],
[
-118.223138,
34.050832
],
[
-118.223074,
34.050732
],[
-118.221781,
34.035823
]
]
]
}
我有一个 python 文件,它必须读取文件,将坐标存储到一个变量中,这样我就可以使用该变量中的信息使用 matplotlib
绘制线条我已经尝试编写代码来读取内容并获取行,但坐标似乎完全不对。没有得到预期的输出。
import matplotlib.pyplot as plt
import json
with open('response.json') as json_file:
m1 = json.load(json_file)
for i in m1:
for j in i["coordinates"]:
for k in j:
for l in k:
plt.plot(l)
plt.show()
预期结果:应根据坐标绘制多条线
实际结果:没有任何反应。根本没有绘制线条
以下是您将如何遍历每对坐标并存储点,然后绘制为 x 与 y。不过,很可能 efficient/better 练习使用 numpy 之类的东西:
points = {"x":[], "y":[]}
for data in m1:
for coordinateList in data["coordinates"]:
for coordinate in coordinateList:
points["x"].append(coordinate[0])
points["y"].append(coordinate[1])
plt.plot(points["x"],points["y"])