Python数据可视化(使用Matplotlib绘制高级图表)

1 绘制等高线图

import numpy as np
import matplotlib.pyplot as plt
#计算高度
def calcu_elevation(x1, y1):
h = (1-x1/2 + x1 ** 5 + y1 ** 3) * np.exp(-x1** 2 - y1** 2)
return h
n = 256
x = np.linspace(-2, 2, n)
y = np.linspace(-2, 2, n)
#利用 meshgrid() 函数生成网格数据
x_grid, y_grid = np.meshgrid(x, y)
fig = plt.figure()
ax = fig.add_subplot(111)
#绘制等高线
con = ax.contour(x_grid, y_grid, calcu_elevation(x_grid, y_grid), 8, colors=‘black’)
#填充等高线的颜色
ax.contourf(x_grid, y_grid, calcu_elevation(x_grid, y_grid), 8, alpha=0.75, cmap=plt.cm.copper)
#为等高线添加文字标签
ax.clabel(con, inline=True, fmt=‘%1.1f’, fontsize=10)
ax.set_xticks([])
ax.set_yticks([])
plt.show()
在这里插入图片描述

2 绘制矢量场流线图

import numpy as np
import matplotlib.pyplot as plt
y, x = np.mgrid[0:5:50j, 0:5:50j]
u = x
v = y
fig = plt.figure()
ax = fig.add_subplot(111)
#绘制矢量场流线图
ax.streamplot(x, y, u, v)
plt.show()
在这里插入图片描述

3 绘制棉棒图

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams[‘font.sans-serif’] = ‘SimHei’
plt.rcParams[‘axes.unicode_minus’] = False
x = np.arange(1, 16)
y = np.array([5.9, 6.2, 6.7, 7.0, 7.0, 7.1, 7.2, 7.4,
7.5, 7.6, 7.7, 7.7, 7.7, 7.8, 7.9])
labels = np.array([‘宝骏310’, ‘宝马i3’, ‘致享’, ‘焕驰’, ‘力帆530’,
‘派力奥’, ‘悦翔V3’, ‘乐风RV’, ‘奥迪A1’, ‘威驰FS’,
‘夏利N7’, ‘启辰R30’, ‘和悦A13RS’, ‘致炫’, ‘赛欧’])
fig = plt.figure(figsize=(10, 6), dpi= 80)
ax = fig.add_subplot(111)
#绘制棉棒图
markerline, stemlines, baseline = ax.stem(x, y, linefmt=‘–’,
markerfmt=‘o’, label=‘TestStem’, use_line_collection=True)
#设置棉棒图线段的属性
plt.setp(s

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值