csv文件来源出处
注,可以直接通过pandas读取csv文件的网络地址
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
**#调用pandas读取csv函数**
data_LA = data_LA = pd.read_csv('./LATemperature2019.csv')
data_NY = pd.read_csv('./NYTemperature2019.csv')
#取数据
#date_LA _str= data_LA['DATE']
#TAVG_LA = data_LA['TAVG']
**#这样直接从csv取出来的数据还是pandas DateFrame,可以转化成数组**
date_LA_str = data_LA['DATE'].to_numpy()
TAVG_LA = data_LA['TAVG'].to_numpy()
**#把datetime转化为day of year**
nrec = len(TAVG_LA)
date_LA =np.zeros(nrec, dtype=object)
for i in range(nrec):
date_LA[i]=datetime.strptime(date_LA_str[i], '%Y-%m-%d' )
#doy_LA存储一年中第几天
#datetime类型.timetuple():生成struct_time结构体
#struct_time.tm_yday:一年中第几天
doy_LA = np.zeros(nrec)
for i in range(nrec):
doy_LA[i] = date_LA[i].timetuple().tm_yday
#argsort()排序函数
argsort_idx = p.argsort(doy_LA)
doy_LA=doy_LA[argsort_idx]
TAVG_LA = TAVG_LA[argsort_idx]
#对另一个表重复以上操作
date_NY_str = data_NY['DATE'].to_numpy()
TAVG_NY = data_NY['TAVG'].to_numpy()
**#把datetime转化为day of year**
nrec = len(TAVG_NY)
date_NY =np.zeros(nrec, dtype=object)
for i in range(nrec):
date_NY[i]=datetime.strptime(date_NY_str[i], '%Y-%m-%d' )
#doy_NY存储一年中第几天
#datetime类型.timetuple():生成struct_time结构体
#struct_time.tm_yday:一年中第几天
doy_NY = np.zeros(nrec)
for i in range(nrec):
doy_NY[i] = date_NY[i].timetuple().tm_yday
#argsort()排序函数
argsort_idx = p.argsort(doy_NY)
doy_NY=doy_NY[argsort_idx]
TAVG_NY = TAVG_NY[argsort_idx]
#画图
fig = plt.figure()
sub = fig.add_subplot(111)
sub.plot(TAVG_LA,TAVG_NY)
sub.set_xlabel('Temperature LA', fontsize=15)
sub.set_ylabel('Temperature NY', fontsize=15)
plot.show()
其实可以通过
sub.plot中关键字linestyle = ‘none’ (线条虚实为0)再设置marker=‘0’(关键点点形状圆形)
sub.plot(TAVG_LA,TAVG_NY, linestyle=‘none’ ,marker=‘0’ )
sub.scatter(x,y,marker=)
sub.scatter(TAVG_LA,TAVG_NY,marker='o')
画图效果与上图一致
sub.scatter(TAVG_LA,TAVG_NY,marker='o',c='red')
c=一个颜色|颜色数组|浮点数数组:散点颜色
此处可以传入一个和x,y长度一样的颜色的数组,然后指定一个颜色表(cmap)。这样每一个散点都有不同的颜色
sub.scatter(TAVG_LA,TAVG_NY,marker='o',c=doy_LA,cmap='twilight')
默认s=20
sub.scatter(TAVG_LA,TAVG_NY,marker='o',c=doy_LA,cmap='twilight', s=10)
s此处可以传入一个和x,y长度一样的颜色的数组,这样每一个散点都有不同的大小
s = 1+12*np.sin(np.pi * np.linspace(0,1,len(TAVG_NY)))
sub.scatter(TAVG_LA,TAVG_NY,marker='o',c=doy_LA,cmap='twilight', s = 1+12*np.sin(np.pi * np.linspace(0,1,len(TAVG_NY))))
用了颜色表添加一个颜色棒是很有必要的
scat=sub.scatter(TAVG_LA,TAVG_NY,marker='o',c=doy_LA,cmap='twilight', s = 1+12*np.sin(np.pi * np.linspace(0,1,len(TAVG_NY))))
cb = fig.colorbar(scat)
归一化坐标,默认等于1。
cb = fig.colorbar(scat,shrink=0.75)
默认值为20
cb = fig.colorbar(scat,shrink=0.75, aspect=12)
cb = fig.colorbar(scat,shrink=0.75,
aspect=12,ticks=np.arange(0,365,60,dtype=int))
cb = fig.colorbar(scat,label='day of year')
但是这样无法调整label的大小
cb = fig.colorbar(scat)
cb.ax.set_ylabel('day of year',fontsize=15)
cb.ax.set_xlabel('day of year',fontsize=15)
cb.ax.set_title('day of year',fontsize=15)