探索数据可视化:Matplotlib 高级绘图功能(四)

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()
subjecttimepointeventregionsignal
0s1318stimparietal-0.017552
1s514stimparietal-0.080883
2s1218stimparietal-0.081033
3s1118stimparietal-0.046134
4s1018stimparietal-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
yearmonthpassengers
01949January112
11949February118
21949March132
31949April129
41949May121
............
1391960August606
1401960September508
1411960October461
1421960November390
1431960December432

144 rows × 3 columns

# !!! pivot数据重塑,改变DataFrame形状
flights = flights.pivot("month", "year", "passengers") # 年,月份,飞机乘客数量
flights
year194919501951195219531954195519561957195819591960
month
April129135163181235227269313348348396461
August148170199242272293347405467505559606
December118140166194201229278306336337405432
February118126150180196188233277301318342391
January112115145171196204242284315340360417
July148170199230264302364413465491548622
June135149178218243264315374422435472535
March132141178193236235267317356362406419
May121125172183229234270318355363420472
November104114146172180203237271305310362390
October119133162191211229274306347359407461
September136158184209237259312355404404463508
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_billtipsexsmokerdaytimesize
016.991.01FemaleNoSunDinner2
110.341.66MaleNoSunDinner3
221.013.50MaleNoSunDinner3
323.683.31MaleNoSunDinner2
424.593.61FemaleNoSunDinner4
525.294.71MaleNoSunDinner4
68.772.00MaleNoSunDinner2
726.883.12MaleNoSunDinner4
815.041.96MaleNoSunDinner2
914.783.23MaleNoSunDinner2
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')

在这里插入图片描述

  • 20
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值