0036-python生态探索:数据可视化-Matplotlib

本文介绍了Python的Matplotlib库的基本用法,包括直接绘图、单图框架、多图框架、饼图以及图注释的创建。通过示例代码展示了如何配置图形元素,如设置坐标轴限制、添加图例、饼图百分比显示等,帮助读者理解并掌握Matplotlib进行数据可视化的技巧。
摘要由CSDN通过智能技术生成

直接绘图

import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4], [5, 14, 2, 39], color='red', linewidth=10)
plt.xlim(0.5, 4.5)
plt.show()

单图框架 

import matplotlib.pyplot as plt
fig = plt.figure()   #配置要素
ax = fig.add_subplot(111)    #添加表,第一行第一列第一个位置
ax.set(xlim=[0, 9], ylim=[-4, 4], title='first',
       ylabel='Y', xlabel='X')
plt.show()

多图框架

import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=2, ncols=2)
axes[0,0].set(title='Upper Left')
axes[0,1].set(title='Upper Right')
axes[1,0].set(title='Lower Left')
axes[1,1].set(title='Lower Right')
plt.show()

饼图 

import matplotlib.pyplot as plt
labels = 'work', 'sleep', 'study', 'other'
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0) 
fig1, (ax1, ax2) = plt.subplots(2)
ax1.pie(sizes, labels=labels, autopct='%1.1f%%', shadow=True)  #格式化百分比输出
ax1.axis('equal')
ax2.pie(sizes, autopct='%1.2f%%', shadow=True, startangle=90, explode=explode,
    pctdistance=0.8) #百分比距离圆心距离
ax2.axis('equal')
ax2.legend(labels=labels, loc='upper right')
plt.show()

图注释

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4], [10, 20, 25, 30], label='energy')
ax.plot([1, 2, 3, 4], [30, 23, 13, 4], label='oil')
ax.scatter([1, 2, 3, 4], [12, 20, 22, 17], label='Point')
ax.set(ylabel='Mount', xlabel='Time', title='sale for two teams')
ax.legend()
plt.show()

 

问题参考:

Python--Matplotlib(基本用法)_Gingkens的博客-CSDN博客_matplotlib

Python绘图问题:Matplotlib中%matplotlib inline是什么、如何使用?_LthID的博客-CSDN博客_%matplotlib inline 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值