繁体   English   中英

Plot 一定范围的值与 pandas 和 matplotlib

[英]Plot certain range of values with pandas and matplotlib

我已经解析出数据 form.json 而不是绘制它们,但我只想要它的某个范围
例如 year-mounth= 2014-12 到 2020-03 代码是

import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_json("observed-solar-cycle-indices.json", orient='records')
data = pd.DataFrame(data)

print(data)

x = data['time-tag']
y = data['ssn']

plt.plot(x, y, 'o')
plt.xlabel('Year-day'), plt.ylabel('SSN')
plt.show()

这是结果,你可以看到它太多了1

here is the json file: https://services.swpc.noaa.gov/json/solar-cycle/observed-solar-cycle-indices.json How to either parse out certain value from the JSON file or plot a certain range?

您可以搜索 x 和 y 值的开始日期和结束日期的索引值。 使用它来创建一组较小的列表,您可以使用 plot。

例如,它可能类似于

x = data['time-tag']
y = data['ssn']

start_index = x.index('2014-314')
end_index = x.index('2020-083')

x_subsection = x[start_index : end_index]
y_subsection = y[start_index : end_index]

plt.plot(x_subsection, y_subsection, 'o')
plt.xlabel('Year-day'), plt.ylabel('SSN')
plt.show()

您可能需要使用 np.array() 将 dataframe 转换为数组。

以下应该有效:

Select 数据使用开始和结束日期

ndata = data[ (data['time-tag'] > '2014-01') & (data['time-tag'] < '2020-12')] 

Plot 数据。 x轴标注适配为每12个才显示一次 label

x = ndata['time-tag']
y = ndata['ssn']

fig, ax = plt.subplots()

plt.plot(x, y, 'o')

every_nth = 12
for n, label in enumerate(ax.xaxis.get_ticklabels()):
    if n % every_nth != 0:
        label.set_visible(False)

plt.xlabel('Year-Month') 
plt.xticks(rotation='vertical')
plt.ylabel('SSN')
plt.show()

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM