【发布时间】:2021-06-23 10:12:33
【问题描述】:
尝试绘制一天中的紫外线指数。数据来自一个 api,经过一些处理后,它为我提供了一个不错的列表中的数据。不过剧情实在是太恶心了。 Link to graph。 Y轴值是重复的,到处乱扔,所以有多个0,多个0的两边都是正数,解释起来简直就是一场噩梦。提前致谢
代码:
import requests
import re
from matplotlib import pyplot as plt
import numpy as np
# Get weather information
response = requests.get("https://api.openweathermap.org/data/2.5/onecall?lat=55.583328&lon=13.0&lang=se&exclude=minutely,daily,alerts&units=metric&appid=0f0212703cfecb4699dfc2c7edde950a")
# Save weather information to file
with open("weather.csv", 'w') as file:
file.write(response.text)
# Opens the file and gets all the values of "uv-index"
with open("Weather.csv", 'r') as text:
pattern = 'uvi":(.*?),'
Line = text.read()
substring = np.array(re.findall(pattern, Line))
# Creates an x-axis as a list with the same size as y-axis
# If they're not the same size, error is given:
# ValueError: x and y must have same first dimension, but have shapes (12,) and (49,)
x_axis = []
for i in range(len(substring)):
x_axis.append(i)
x_axis = np.array(x_axis)
# Determines size and plots graph
fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(x_axis, substring)
# Creates labels
plt.title("UV-Index today")
plt.xlabel("Time")
plt.ylabel("UV-Index")
# Saves the plot as image and shows it on screen
plt.savefig("UV-Index" + ".png")
plt.show()
【问题讨论】:
-
可能是您的
y值不是数字?
标签: python matplotlib graphing