Python用月(列),年(行)和温度(上下文)绘制

时间:2019-06-18 11:59:11

标签: python python-3.x

我是Python自学入门者,目前在使用电子表格进行绘图时遇到问题。

我从NOAA下载了一些温度数据集进行练习。数据如下图所示。

https://i.imgur.com/bj8s0ZO.jpg(或https://imgur.com/bj8s0ZO

我想绘制一个时间序列线图(即sample figure,但是该图未显示每年的月份。我想绘制1990年至2018年所有年份的月份)以显示1990年至2018年的温度变化,但不知道如何做到。

我试图调整电子表格的形状,但是效果不佳,并且在代码中使用了太多行。谁能教我如何用一种有效的方法作图?

谢谢

3 个答案:

答案 0 :(得分:0)

使用熊猫数据框和matplotlib:

import pandas as pd
import matplotlib.pyplot as plt

# manualy setup data
df = pd.DataFrame({'Year':[1990, 1991,1992,1993,1994],'Jan':[50.7, 51.3, 48.3, 48.3, 49.2],'Feb':[50.7, 51.3, 48.3, 48.3, 49.2],'Annual':[50.7, 51.3, 48.3, 48.3, 49.2]})

# data from file
data = pd.read_csv('data.csv')
# calculate annual anomaly
df['anomaly'] = df['Annual'] - df['Annual'].mean()

# calculate Jan anomaly
df['Jan_anomaly'] = df['Jan'] - df['Jan'].mean()

# plot data
df.plot(x='Year', y=['anomaly','Jan_anomaly'])

plt.show()

输出:

enter image description here

答案 1 :(得分:0)

使用它来转换数据框:

    monthname=[]
    value=[]
    for row in range(0,len(df)):
        for col in range(1,13):
            monthname.append(str(col)+"-"+str(df['Year'][row]))
            value.append(df.iloc[row,col])

    df1=pd.DataFrame()
    df1['Date']=monthname
    df1['Val']=value
    df1['Date']=pd.to_datetime(df1['Date'],format='%m-%Y')

答案 2 :(得分:0)

我想拆开数据帧是这里的关键... 可能是这样的: (不要判断这段代码的优点,但它应该可以工作:-D)

import pandas as pd
import numpy as np

import matplotlib as mpl
import matplotlib.pyplot as plt

# let's generate a data-structure like yours
years = np.arange(1995, 2000, 1, dtype=int)
data = np.array([years, *[np.random.rand(len(years)) for i in range(13)]]).T

# make a data-frame
data = pd.DataFrame(data, columns = ['year', *np.linspace(1,12,12), 'annual'])
data.set_index('year', inplace=True)
# drop what we don't need
data.drop(columns=['annual'], inplace=True)

# unstack it and reset the index
data = pd.DataFrame(data.T.unstack()).reset_index()
# generate a datetime-index from the obtained columns
index = pd.to_datetime(data.year.astype(int).astype(str) + '/' + data.level_1.astype(int).astype(str))
data.set_index(index, inplace=True)
# drop the columns that we no longer need
data.drop(columns=['year', 'level_1'], inplace=True)


# make a figure
f, ax = plt.subplots(figsize=(12,5))
# plot the data
ax.plot(data)

# set the minor axis-ticks to monthly
ax.xaxis.set_minor_locator(mpl.dates.MonthLocator(interval=2))
ax.xaxis.set_minor_formatter(mpl.dates.DateFormatter('%m'))

# set the major axis-ticks to yearly
ax.xaxis.set_major_locator(mpl.dates.YearLocator())
ax.xaxis.set_major_formatter(mpl.dates.DateFormatter('\n%Y'))