总结了16个常用的matlibplot画图技巧(附源码)

本文分享了16个使用Matplotlib进行数据可视化的实用技巧,包括添加文本框、指示箭头、改变折线形状等,适合希望提升图表制作水平的数据分析师和技术人员。

点击上方蓝色小字,关注“涛哥聊Python

重磅干货,第一时间送达

来源:python数据分析之禅

今天给大家分享16个用matlibplot画图的常用技巧,也是我经常记不住的,废话不多说,直接上干货。

1.添加文本框

为文本添加添加背景框

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
fig=plt.figure()
fig,axe=plt.subplots()
axe.text(0.5,0.5,'我是文本框',bbox={'facecolor':'cyan','alpha':0.5,'pad':0.7})  #添加文本框
plt.show()


2.添加指示箭头

import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['axes.unicode_minus'] = False
fig=plt.figure()
fig,axe=plt.subplots()
t=np.arange(0.0,2.0,0.01)
s=np.sin(2*np.pi*t)
axe.plot(t,s,linestyle='-',label='line1')
axe.annotate('我是正弦函数',xy=(1.25,1),xytext=(1.9,1),
                  arrowprops=dict(facecolor='red',shrink=0.2),
                 horizontalalignment='center',verticalalignment='center')
plt.show()


3.改变折线形状

fig,axe=plt.subplots()
np.random.seed(100)
x=np.arange(0, 10, 1)
y1=np.random.rand(10)
axe.plot(x, y1, '--o')
plt.show()


4.柱状图横置

fig,axe=plt.subplots()
data_m=(40, 60, 120, 180, 20, 200)
index = np.arange(6)
width=0.4
axe.barh(index, data_m, width,align='center',alpha=0.8, label='men')
plt.show()


5.移动坐标轴位置

import matplotlib.pyplot as plt
import numpy as np
fig=plt.figure()
fig,axe=plt.subplots()
axe.spines['right'].set_color('none')
axe.spines['top'].set_color('none')
axe.spines['bottom'].set_position(('data',1))
axe.spines['left'].set_position(('data',1))
plt.show()


6.设置坐标轴范围

fig=plt.figure()
fig,axe=plt.subplots()
plt.xlim(0,10)
plt.ylim(0,8000)
plt.show()


7.改变坐标轴颜色

import matplotlib.pyplot as plt
import numpy as np
fig=plt.figure()
fig,axe=plt.subplots()
axe.spines['right'].set_color('yellow')
axe.spines['top'].set_color('red')
plt.show()


8.设置坐标轴刻度

fig=plt.figure()
fig,axe=plt.subplots()
axe.set_xticks([0,1,2,3,4,5])
plt.show()


9.改变刻度

fig=plt.figure()
fig,axe=plt.subplots()
axe.set_xticks([0,1,2,3,4,5])
axe.set_xticklabels(['Taxi','Metro','Walk','Bus','Bicycle','Driving'])
plt.show()


9.坐标倾斜

fig=plt.figure()
fig,axe=plt.subplots()
axe.set_xticks([0,1,2,3,4,5])
axe.set_xticklabels(['Taxi','Metro','Walk','Bus','Bicycle','Driving'],rotation=45)
plt.show()


10.绘制子图

fig=plt.figure()
fig,axe=plt.subplots(4,4,figsize=(10,10))
plt.show()


11.加网格线

fig=plt.figure()
fig,axe=plt.subplots()
axe.grid(True)
plt.show()


12.改变图形颜色

fig,axe=plt.subplots()
data_m=(40, 60, 120, 180, 20, 200)
index = np.arange(6)
axe.bar(index, data_m,color='y')
plt.show()


13.改变样式

fig,axe=plt.subplots()
data_m=(40, 60, 120, 180, 20, 200)
index = np.arange(6)
axe.bar(index, data_m)
plt.style.use('dark_background')
plt.show()


样式列表:

plt.style.available
['bmh',
 'classic',
 'dark_background',
 'fast',
 'fivethirtyeight',
 'ggplot',
 'grayscale',
 'seaborn-bright',
 'seaborn-colorblind',
 'seaborn-dark-palette',
 'seaborn-dark',
 'seaborn-darkgrid',
 'seaborn-deep',
 'seaborn-muted',
 'seaborn-notebook',
 'seaborn-paper',
 'seaborn-pastel',
 'seaborn-poster',
 'seaborn-talk',
 'seaborn-ticks',
 'seaborn-white',
 'seaborn-whitegrid',
 'seaborn',
 'Solarize_Light2',
 'tableau-colorblind10',
 '_classic_test']


14.添加表格

fig,axe=plt.subplots()
data_m=(40, 60, 120, 180, 20, 200)
data_f=(30, 100, 150, 30, 20, 50)
index = np.arange(6)
width=0.4
#bar charts
axe.bar(index, data_m, width, color='c', label='men')
axe.bar(index, data_f, width, color='b', bottom=data_m, label='women')
axe.set_xticks([])
axe.legend()
#table
data=(data_m,data_f)
rows=('male','female')
columns=('Taxi','Metro','Walk','Bus','Bicycle','Driving')
axe.table(cellText=data, rowLabels=rows, colLabels=columns)
plt.show()


15.饼状图分离

fig, axe = plt.subplots()
labels = 'Taxi', 'Metro', 'Walk', 'Bus','Bicycle','Drive'
sizes = [10, 30, 5, 25, 5, 25]
explode = (0.1, 0.1, 0.5, 0.1, 0.1, 0.1)   #控制分隔距离
axe.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
        shadow=True, startangle=90)
axe.axis('equal')
plt.show()


16.保存绘制的图片

fig, axe = plt.subplots()
labels = 'Taxi', 'Metro', 'Walk', 'Bus','Bicycle','Drive'
sizes = [10, 30, 5, 25, 5, 25]
explode = (0.1, 0.1, 0.5, 0.1, 0.1, 0.1)   #控制分隔距离
axe.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
        shadow=True, startangle=90)
axe.axis('equal')
plt.savefig('temp.png', dpi=fig.dpi)

-END-

《剑指offer》

这本书大家一定如雷贯耳吧,基本上只要你是程序员就一定会用到这本书,书里面剖析了50个典型的程序员面试题,从基础知识、代码质量、解题思路、优化效率和综合能力五个方面系统整理了影响面试的5个要点,是程序员找工作必备的一本书。

这本书也给大家准备了高清PDF版电子书,扫描下方二维码,后台回复关键词:offer  即可获取这本电子书!

????长按上方二维码 2 秒
回复「offer」即可获取资料
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值