3D图形
线形图&散点图
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D
x = np.linspace(0,60,300)
y = np.sin(x)
z = np.cos(x)
fig = plt.figure(figsize=(9,6))
a3 = Axes3D(fig) # 二维变成3D
a3.plot(x,y,z)

plt.figure(figsize=(9,6))
# projection='3d' 指定了这个子图的投影类型为3D
a3 = plt.subplot(111, projection = '3d')
a3.plot(x,y,z) # 普通线形图
a3.set_xlabel('X')
a3.set_ylabel('Y')
a3.set_zlabel('Z')
# 散点图
x = np.random.randint(0,60,size = 20)
y = np.random.randn(20)
z = np.random.randn(20)
a3.scatter(x,y,z,color= 'red')
# 调整视图的角度
# elev=20示观察者向上仰角 20 度看图
# azim=-30 观察者沿着 z 轴逆时针旋转 30 度来观察图形
a3.view_init(elev = 20,azim=-30)

3D条形图
month = np.arange(1,5)
# 每个月 4周 每周都会产生数据
# 三个维度:月、周、销量
fig = plt.figure(figsize=(9,6))
ax3 = Axes3D(fig)
for m in month:
# 每个月都要绘制条形图
ax3.bar(np.arange(1,5), # 理解成横坐标
np.random.randint(1,10,size = 4), # 纵坐标
zs = m ,
zdir = 'x',# 在哪个方向上,一排排排列
alpha = 0.7,# alpha 透明度
width = 0.5)
ax3.set_xlabel('Month',fontsize = 18,color = 'red')
ax3.set_xticks(month)
ax3.set_ylabel('Week',fontsize = 18,color = 'red')
ax3.set_yticks([1,2,3,4])
ax3.set_zlabel('Sales',fontsize = 18,color = 'green')

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 创建一个网格,其中 x 方向和 y 方向都是 [0, 1, 2, 3, 4]
x, y = np.meshgrid(np.arange(5), np.arange(5))
x = x.flatten()
y = y.flatten()
z = np.zeros_like(x)
dx = dy = 0.3
dz = np.random.randint(1, 10, len(z))
# 绘制3D条形图
ax.bar3d(x, y, z, dx, dy, dz, shade=True)
# 设置坐标轴标签
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()

seaborn 快速入门
import seaborn as sns
# style='ticks' 坐标轴显示刻度线
# context='paper': 指定绘图的上下文环境, 从而调整图形的大小和比例
sns.set(style = 'ticks',context = 'paper',font = 'STKaiti') # 设置样式
plt.figure(figsize=(9,6))
x = np.linspace(0,2*np.pi,20)
y = np.sin(x)
# lineplot方法,画一条线
sns.lineplot(x = x, y = y, color = 'green', ls = '--')
sns.lineplot(x = x, y = np.cos(x), color = 'red', ls = '-.')

线形图
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
sns.set(style = 'dark',context = 'notebook',font = 'SimHei') # 设置样式
plt.figure(figsize=(9,6))
# 加载数据
fmri = pd.read_csv('./fmri.csv') # fmri这一核磁共振数据
fmri['event'].unique() # array(['stim', 'cue'], dtype=object)
fmri.head()
| subject | timepoint | event | region | signal | |
|---|---|---|---|---|---|
| 0 | s13 | 18 | stim | parietal | -0.017552 |
| 1 | s5 | 14 | stim | parietal | -0.080883 |
| 2 | s12 | 18 | stim | parietal | -0.081033 |
| 3 | s11 | 18 | stim | parietal | -0.046134 |
| 4 | s10 | 18 | stim | parietal | -0.037970 |
ax = sns.lineplot(x = 'timepoint',y = 'signal',
hue = 'event',# 根据event属性分类,绘制
style = 'event' ,# 根据event属性分类,指定样式
data= fmri,
palette='ocean', # 画板,颜色
markers=True,
markersize = 10)
plt.xlabel('时间节点',fontsize = 30)

热力图
flights = pd.read_csv('./flights.csv') # 飞行数据
flights
| year | month | passengers | |
|---|---|---|---|
| 0 | 1949 | January | 112 |
| 1 | 1949 | February | 118 |
| 2 | 1949 | March | 132 |
| 3 | 1949 | April | 129 |
| 4 | 1949 | May | 121 |
| ... | ... | ... | ... |
| 139 | 1960 | August | 606 |
| 140 | 1960 | September | 508 |
| 141 | 1960 | October | 461 |
| 142 | 1960 | November | 390 |
| 143 | 1960 | December | 432 |
144 rows × 3 columns
# !!! pivot数据重塑,改变DataFrame形状
flights = flights.pivot("month", "year", "passengers") # 年,月份,飞机乘客数量
flights
| year | 1949 | 1950 | 1951 | 1952 | 1953 | 1954 | 1955 | 1956 | 1957 | 1958 | 1959 | 1960 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| month | ||||||||||||
| April | 129 | 135 | 163 | 181 | 235 | 227 | 269 | 313 | 348 | 348 | 396 | 461 |
| August | 148 | 170 | 199 | 242 | 272 | 293 | 347 | 405 | 467 | 505 | 559 | 606 |
| December | 118 | 140 | 166 | 194 | 201 | 229 | 278 | 306 | 336 | 337 | 405 | 432 |
| February | 118 | 126 | 150 | 180 | 196 | 188 | 233 | 277 | 301 | 318 | 342 | 391 |
| January | 112 | 115 | 145 | 171 | 196 | 204 | 242 | 284 | 315 | 340 | 360 | 417 |
| July | 148 | 170 | 199 | 230 | 264 | 302 | 364 | 413 | 465 | 491 | 548 | 622 |
| June | 135 | 149 | 178 | 218 | 243 | 264 | 315 | 374 | 422 | 435 | 472 | 535 |
| March | 132 | 141 | 178 | 193 | 236 | 235 | 267 | 317 | 356 | 362 | 406 | 419 |
| May | 121 | 125 | 172 | 183 | 229 | 234 | 270 | 318 | 355 | 363 | 420 | 472 |
| November | 104 | 114 | 146 | 172 | 180 | 203 | 237 | 271 | 305 | 310 | 362 | 390 |
| October | 119 | 133 | 162 | 191 | 211 | 229 | 274 | 306 | 347 | 359 | 407 | 461 |
| September | 136 | 158 | 184 | 209 | 237 | 259 | 312 | 355 | 404 | 404 | 463 | 508 |
plt.figure(figsize=(12,9))
sns.heatmap(flights, annot=True,fmt = 'd',cmap = 'RdBu_r',linewidths=5)

条形图
tips = pd.read_csv('./tips.csv') # 小费
tips.head(10)
| total_bill | tip | sex | smoker | day | time | size | |
|---|---|---|---|---|---|---|---|
| 0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
| 1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
| 2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
| 3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
| 4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
| 5 | 25.29 | 4.71 | Male | No | Sun | Dinner | 4 |
| 6 | 8.77 | 2.00 | Male | No | Sun | Dinner | 2 |
| 7 | 26.88 | 3.12 | Male | No | Sun | Dinner | 4 |
| 8 | 15.04 | 1.96 | Male | No | Sun | Dinner | 2 |
| 9 | 14.78 | 3.23 | Male | No | Sun | Dinner | 2 |
plt.figure(figsize = (9,6))
sns.set(style = 'whitegrid')
# palette='viridis' 使用序列调色板
# orient='h 设置水平方向
ax = sns.barplot(x = "total_bill", y = "day",
data = tips,
palette='viridis', orient='h')




















