#%%
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
#%% md
从寻找数据间关联到最终做出决定,大致分为以下四步:
- 可视化(Visualize):使用不同种类的图表对原始数据进行可视化处理,使复杂的数据更容易理解与使用;
- 分析(Analysis):数据分析的目的是获取有用的信息,
这个过程主要涉及对数据的清洗、检查、转换以及对数据的建模;
- 文档说明(Document insight):文档说明属于整理、汇总阶段,
将有用的数据或者信息整理出来;
- 数据集转换(Transform Data Set):
指将数据进行分类、分级、统计记录格式与编码格式等。
#%%
# 不显示图片
%matplotlib inline
#%%
# 图片中文显示
plt.rcParams["font.sans-serif"] = "SimHei"
#%%
# 显示负号
plt.rcParams["axes.unicode_minus"] = True
#%%
# 支持svg矢量图
%config InlineBackend.figure_format ='svg'
#%%
#检查字体库
from matplotlib.font_manager import FontManager
fm = FontManager()
my_font = set(f.name for f in fm.ttflist)
print(my_font)
#%%
# 基本绘图
# 1.抛物线 利用等差数列
x = np.linspace(-5,5,10000)
plt.plot(x**2)
#%% md
颜色与样式
## 3.3 样式和颜色
| 符号 | '-','--','-.',':','.',',',,o,^,v,<,>,s,+,x,D,d,1,2,3,4,h,H,p,\| ,_ |
| ---- | ------------------------------------------------------------ |
| 颜色 | b(蓝色),g(绿色),r(红色),c(青色),m(品红),y(黄色),k(黑色),w(白色) |
#%%
x = np.linspace(-5,5,50)
plt.plot(x**2,'r',ls='--')
#%% md
# 画布配置
| 参数 | 说明 |
| ------- | ------------------------------------------------------ |
| figsize | 指定画布的大小,(宽度,高度),单位为英寸。 |
| dpi | 指定绘图对象的分辨率,即每英寸多少个像素,默认值为80。 |
#%%
plt.figure(figsize=(20,10))
x = np.linspace(0,100,100000)
y = np.sin(x)
plt.plot(x,y,)
#%%
# 坐标轴 和 轴标签
plt.xlim(0,6)
plt.ylim(0,3)
plt.xlabel('时间')
plt.ylabel('价格')
plt.title('表')
plt.show()
#%%
# 设置图例
plt.figure(figsize=(10,5))
x = np.linspace(0,10,1000000)
plt.plot(x,np.sin(x),'r')
plt.plot(x,np.cos(x),'b')
plt.legend(loc = 'center')
#%%
# 添加注释
x = np.arange(1,31)
y = x ** 2
plt.plot(x,y,marker = 'o')
for xy in zip(x,y):
plt.annotate("(%s,%s)"%xy,xy=xy)
plt.show()
#%%
# 画多个图
plt.figure(figsize=(20,10))
x = np.linspace(0,10,1000)
plt.plot(x,np.sin(x),'r+')
plt.plot(x,np.cos(x),'b+')
plt.plot(x,-np.sin(x),'k+')
plt.plot(x,-np.cos(x),'g+')
#%% md
<!-- #多图布局 子图使用subplot() -->
#%% md
多图布局,子图使用 subplot() 代表图的位置
#%%
fig = plt.figure(figsize=(10,10))
x = np.linspace(1,10,2000)
y = np.sin(x)
# 子图1
ax1 = plt.subplot(2,2,1) # 两行两列中 第一个图
ax1.plot(x,y)
ax1.set_title('子图1')
ax2 = plt.subplot(2,2,2) # 两行两列中 第2个图
ax2.plot(x,y)
ax2.set_title('子图2')
ax3 = plt.subplot(2,2,3) # 两行两列中 第3个图
ax3.plot(x,y)
ax3.set_title('子图3')
ax4 = plt.subplot(2,2,4) # 两行两列中 第4个图
ax4.plot(x,y)
ax4.set_title('子图4')
fig.tight_layout() #自动布局
#%%
# 子图
x = np.linspace(1,10,1000)
flg , ax = plt.subplots(3,3)
ax1 , ax2, ax3 = ax
ax11 , ax12, ax13 = ax1
ax21 , ax22, ax23 = ax2
ax31 , ax32, ax33 = ax3
# 设置宽和高
fig.set_figwidth(8)
fig.set_figheight(5)
fig.tight_layout()
ax11.plot(x,np.sin(x),'r')
ax11.set_title('图一')
ax12.plot(x,np.cos(x))
ax12.set_title('图二')
ax13.plot(x,np.tan(x))
ax13.set_title('图三')
ax21.plot(x,np.sin(x)+np.cos(x))
ax21.set_title('图四')
ax22.plot(x,np.cos(x)+np.cos(x))
ax22.set_title('图五')
ax23.plot(x,np.tan(x)+np.tan(x))
ax23.set_title('图六')
ax31.plot(x,-np.sin(x))
ax31.set_title('图七')
ax32.plot(x,-np.cos(x))
ax32.set_title('图八')
ax33.plot(x,-np.tan(x))
ax33.set_title('图九')
#%% md
# 图像嵌套 add_subplot()
#%%
fig = plt.figure(figsize=(20,10))
# 子图一
axes1 = fig.add_subplot(1,1,1)
axes1.plot([0,1],[3,4],marker='o')
# 子图二
axes2 = fig.add_subplot(2,2,1) #新建子图1,(2,2,1)表示创建2x2子图中的第一个
axes2.plot([0,1],marker='o')
# 图像嵌套 add_axes() & axes()
fig = plt.figure(figsize=(10,6))
x = np.linspace(0,10,100)
y = np.sin(x)
plt.plot(x,y)
# 嵌套图一
axes11 = plt.axes([0.5,0.5,0.3,0.3])
axes11.plot(x,y,'g')
# 嵌套图二
axes22 = fig.add_axes([0.18,0.18,0.3,0.3]) #从画布的18%开始 宽和高是画布的30%
axes22.plot(x,y,'r')
#%% md
# 常见视图
#%%
# 1.单条折线 marker:样本点的样式
x = ['Mon','Tus','wed','Thur','Fri','Sat','Sun']
y = np.random.randint(25,55,7)
plt.plot(x,y,'m',marker='o',markersize=5)
#%%
# 2 多条折线图
x = [5,8,12,14,16,18,20]
y1 = [18,21,29,31,26,24,20]
y2 =[15,18,24,30,31,25,24]
plt.plot(x,y1,'r',marker ='o',markersize = 5)
plt.plot(x,y2,'m',marker = 'o',markersize = 5)
plt.title("温度对比折线图")
plt.xlabel('时间(h)')
plt.ylabel('温度(℃)')
#%%
# 柱状图
langs = ['C',"C++","Java","Python","PHP"]
students = [23,17,35,29,12]
plt.xlabel("语言")
plt.ylabel('热度')
plt.bar(langs,students)
plt.show()
#%%
# 3.饼图
size = [2,5,12]
labels = ["娱乐","育儿","饮食"]
plt.pie(size,labels=labels,autopct='%1.2f%%',shadow=True,startangle=1000)
plt.show()
Matplotlib
最新推荐文章于 2024-09-07 00:11:42 发布
2735

被折叠的 条评论
为什么被折叠?



