matplotlib基础汇总_04

3D图形
导包


import numpy as np
import matplotlib.pyplot as plt
#3d图形必须的
from mpl_toolkits.mplot3d.axes3d import Axes3D
%matplotlib inline
生成数据


#系数,由X,Y生成Z
a = 0.7
b =  np.pi 

#计算Z轴的值
def mk_Z(X, Y):
    return 2 + a - 2 * np.cos(X) * np.cos(Y) - a * np.cos(b - 2*X)

#生成X,Y,Z
x = np.linspace(0, 2*np.pi, 100)
y = np.linspace(0, 2*np.pi, 100)
X,Y = np.meshgrid(x, y)
Z = mk_Z(X, Y)
绘制图形

fig = plt.figure(figsize=(14,6))

#创建3d的视图,使用属性projection
ax = fig.add_subplot(1, 2, 1, projection='3d')

ax.plot_surface(X,Y,Z,rstride = 5,cstride = 5)

#创建3d视图,使用colorbar,添加颜色柱
ax = fig.add_subplot(1, 2, 2, projection='3d')
p = ax.plot_surface(X, Y, Z, rstride=5, cstride=5, cmap='rainbow', antialiased=True)
cb = fig.colorbar(p, shrink=0.5)

matplotlib基础汇总_04

 

 

玫瑰图
#极坐标条形图
def showRose(values,title):
    
    max_value = values.max()
    # 分为8个面元
    N = 8
    # 面元的分隔角度
    angle = np.arange(0.,2 * np.pi, 2 * np.pi / N)
    # 每个面元的大小(半径)
    radius = np.array(values)
    # 设置极坐标条形图
    
    plt.axes([0, 0, 2, 2], polar=True,facecolor = 'g')
    
    colors = [(1 - x/max_value, 1 - x/max_value, 0.75) for x in radius]
    # 画图
    
    plt.bar(angle, radius, width=(2*np.pi/N), bottom=0.0, color=colors)
    plt.title(title,x=0.2, fontsize=20)
绘制图形

#拉韦纳(Ravenna)又译“腊万纳”“拉文纳”“拉温拿”。意大利北部城市。位于距亚得里亚海10公里的沿海平原上


data = np.load('Ravenna_wind.npy')
hist, angle = np.histogram(data,8,[0,360])
showRose(hist,'Ravenna')

matplotlib基础汇总_04

 

 

城市气候与海洋关系
导包

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas import Series,DataFrame
%matplotlib inline
加载数据

#意大利小镇费拉拉
ferrara1 = pd.read_csv('./ferrara_150715.csv')
ferrara2 = pd.read_csv('./ferrara_250715.csv')
ferrara3 = pd.read_csv('./ferrara_270615.csv')
ferrara = pd.concat([ferrara1,ferrara2,ferrara3],ignore_index=True) 
去除没用的列

asti.drop(['Unnamed: 0'],axis = 1,inplace=True)

bologna.drop(['Unnamed: 0'],axis = 1,inplace=True)

cesena.drop(['Unnamed: 0'],axis = 1,inplace=True)

ferrara.drop(['Unnamed: 0'],axis = 1,inplace=True)

mantova.drop(['Unnamed: 0'],axis = 1,inplace=True)

milano.drop(['Unnamed: 0'],axis = 1,inplace=True)

piacenza.drop(['Unnamed: 0'],axis = 1,inplace=True)

ravenna.drop(['Unnamed: 0'],axis = 1,inplace=True)

torino.drop(['Unnamed: 0'],axis = 1,inplace=True)
获取个城市距离海边距离,最高温度,最低温度,最高湿度,最低湿度


dist = [ravenna['dist'][0],
     cesena['dist'][0],
     faenza['dist'][0],
     ferrara['dist'][0],
     bologna['dist'][0],
     mantova['dist'][0],
     piacenza['dist'][0],
     milano['dist'][0],
     asti['dist'][0],
     torino['dist'][0]
]
temp_max = [ravenna['temp'].max(),
     cesena['temp'].max(),
     faenza['temp'].max(),
     ferrara['temp'].max(),
     bologna['temp'].max(),
     mantova['temp'].max(),
     piacenza['temp'].max(),
     milano['temp'].max(),
     asti['temp'].max(),
     torino['temp'].max()
]
temp_min = [ravenna['temp'].min(),
     cesena['temp'].min(),
     faenza['temp'].min(),
     ferrara['temp'].min(),
     bologna['temp'].min(),
     mantova['temp'].min(),
     piacenza['temp'].min(),
     milano['temp'].min(),
     asti['temp'].min(),
     torino['temp'].min()
]
hum_min = [ravenna['humidity'].min(),
     cesena['humidity'].min(),
     faenza['humidity'].min(),
     ferrara['humidity'].min(),
     bologna['humidity'].min(),
     mantova['humidity'].min(),
     piacenza['humidity'].min(),
     milano['humidity'].min(),
     asti['humidity'].min(),
     torino['humidity'].min()
]
hum_max = [ravenna['humidity'].max(),
     cesena['humidity'].max(),
     faenza['humidity'].max(),
     ferrara['humidity'].max(),
     bologna['humidity'].max(),
     mantova['humidity'].max(),
     piacenza['humidity'].max(),
     milano['humidity'].max(),
     asti['humidity'].max(),
     torino['humidity'].max()
]
显示最高温度与离海远近的关系

plt.axis([0,400,32,35])
plt.plot(dist,temp_max,'ro')

matplotlib基础汇总_04

 

根据距海远近划分数据


观察发现,离海近的可以形成一条直线,离海远的也能形成一条直线。
首先使用numpy:把列表转换为numpy数组,用于后续计算。
分别以100公里和50公里为分界点,划分为离海近和离海远的两组数据

# 把列表转换为numpy数组
x = np.array(dist)
display('x:',x)
y = np.array(temp_max)
display('y:',y)

# 离海近的一组数据
x1 = x[x<100]
x1 = x1.reshape((x1.size,1))
display('x1:',x1)
y1 = y[x<100]
display('y1:',y1)

# 离海远的一组数据
x2 = x[x>50]
x2 = x2.reshape((x2.size,1))
display('x2:',x2)
y2 = y[x>50]
display('y2:',y2)
机器学习计算回归模型


from sklearn.svm import SVR
svr_lin1 = SVR(kernel='linear', C=1e3)
svr_lin2 = SVR(kernel='linear', C=1e3)
svr_lin1.fit(x1, y1)
svr_lin2.fit(x2, y2)
xp1 = np.arange(10,100,10).reshape((9,1))
xp2 = np.arange(50,400,50).reshape((7,1))
yp1 = svr_lin1.predict(xp1)
yp2 = svr_lin2.predict(xp2)
绘制回归曲线


plt.plot(xp1, yp1, c='r', label='Strong sea effect')
plt.plot(xp2, yp2, c='b', label='Light sea effect')
#plt.axis('tight')
plt.legend()
plt.scatter(x, y, c='k', label='data')

matplotlib基础汇总_04

 

 

 

最低温度与海洋距离关系

plt.axis((0,400,16,21))
plt.plot(dist,temp_min,'bo')

matplotlib基础汇总_04

 

 

最低湿度与海洋距离关系


plt.axis([0,400,70,120])
plt.plot(dist,hum_min,'bo')
最高湿度与海洋距离关系

plt.axis([0,400,70,120])
plt.plot(dist,hum_max,'bo')
平均湿度与海洋距离的关系

hum_mean = [ravenna['humidity'].mean(),
     cesena['humidity'].mean(),
     faenza['humidity'].mean(),
     ferrara['humidity'].mean(),
     bologna['humidity'].mean(),
     mantova['humidity'].mean(),
     piacenza['humidity'].mean(),
     milano['humidity'].mean(),
     asti['humidity'].mean(),
     torino['humidity'].mean()
]
plt.plot(dist,hum_mean,'bo')
风速与风向的关系

plt.plot(ravenna['wind_deg'],ravenna['wind_speed'],'ro')
在子图中,同时比较风向与湿度和风力的关系

plt.subplot(211)
plt.plot(cesena['wind_deg'],cesena['humidity'],'bo')
plt.subplot(212)
plt.plot(cesena['wind_deg'],cesena['wind_speed'],'bo')
玫瑰图

def showRoseWind(values,city_name):
    '''
    查看风向图,半径越大,代表这个方向上的风越多
    '''
    max_value = values.max()
    # 分为8个面元
    N = 8
    # 面元的分隔角度
    theta = np.arange(0.,2 * np.pi, 2 * np.pi / N)
    # 每个面元的大小(半径)
    radii = np.array(values)
    # 设置极坐标条形图
    plt.axes([0.025, 0.025, 0.95, 0.95], polar=True)
    colors = [(1 - x/max_value, 1 - x/max_value, 0.75) for x in radii]
    # 画图
    plt.bar(theta, radii, width=(2*np.pi/N), bottom=0.0, color=colors)
    plt.title(city_name,x=0.2, fontsize=20)
用numpy创建一个直方图,将360度划分为8个面元,将数据分类到这8个面元中


hist, bin = np.histogram(ravenna['wind_deg'],8,[0,360])
print(hist)
hist = hist/hist.sum()
print(bin)
showRoseWind(hist,'Ravenna')

matplotlib基础汇总_04

 

 

计算米兰各个方向的风速

print(milano[milano['wind_deg']<45]['wind_speed'].mean())
print(milano[(milano['wind_deg']>44) & (milano['wind_deg']<90)]['wind_speed'].mean())
print(milano[(milano['wind_deg']>89) & (milano['wind_deg']<135)]['wind_speed'].mean())
print(milano[(milano['wind_deg']>134) & (milano['wind_deg']<180)]['wind_speed'].mean())
print(milano[(milano['wind_deg']>179) & (milano['wind_deg']<225)]['wind_speed'].mean())
print(milano[(milano['wind_deg']>224) & (milano['wind_deg']<270)]['wind_speed'].mean())
print(milano[(milano['wind_deg']>269) & (milano['wind_deg']<315)]['wind_speed'].mean())
print(milano[milano['wind_deg']>314]['wind_speed'].mean())
将各个方向风速保存到列表中

degs = np.arange(45,361,45)
tmp =  []
for deg in degs:
    tmp.append(milano[(milano['wind_deg']>(deg-46)) & (milano['wind_deg']<deg)]['wind_speed'].mean())
speeds = np.array(tmp)
print(speeds)
画出各个方向的风速

N = 8
theta = np.arange(0.,2 * np.pi, 2 * np.pi / N)
radii = np.array(speeds)
plt.axes([0.025, 0.025, 0.95, 0.95], polar=True)
colors = [(1-x/10.0, 1-x/10.0, 0.75) for x in radii]
bars = plt.bar(theta, radii, width=(2*np.pi/N), bottom=0.0, color=colors)
plt.title('Milano',x=0.2, fontsize=20)

matplotlib基础汇总_04

 

 

抽取函数

def RoseWind_Speed(city):
   degs = np.arange(45,361,45)
   tmp =  []
   for deg in degs:
      tmp.append(city[(city['wind_deg']>(deg-46)) & (city['wind_deg']<deg)]['wind_speed'].mean())
   return np.array(tmp)


def showRoseWind_Speed(speeds,city_name):
   N = 8
   theta = np.arange(0.,2 * np.pi, 2 * np.pi / N)
   radii = np.array(speeds)
   plt.axes([0.025, 0.025, 0.95, 0.95], polar=True)
   colors = [(1-x/10.0, 1-x/10.0, 0.75) for x in radii]
   bars = plt.bar(theta, radii, width=(2*np.pi/N), bottom=0.0, color=colors)
   plt.title(city_name,x=0.2, fontsize=20)
函数调用

showRoseWind_Speed(RoseWind_Speed(ravenna),'Ravenna')

matplotlib基础汇总_04

 

 


2020-05-24

上一篇:树莓派+PHP+Mairadb数据库读取DHT11


下一篇:mysql数据库访问常见操作