python数据分析-Matplotlib_v2相关操作复习1-2

本文详细介绍使用Matplotlib进行数据可视化的步骤,包括环境搭建、基本绘图操作、多图展示及子图设置等,适合初学者快速掌握Matplotlib绘图技巧。

目录

1.Setting up

2.Matplotlib basics

1.Setting up

#import required libraries
import numpy as np
import pandas as pd

#import matplotlib
import matplotlib.pyplot as plt

#display plots in the notebook itself
%matplotlib inline

2.Matplotlib basics

# set plot size
height = [150,160,165,185]
weight = [70,80,90,100]

#draw the plot 
plt.plot(height,weight)
#out<<

#draw the plot
plt.plot(height,weight)
# add title
plt.title("Relationship between height and weight")
#labels x axis
plt.xlabel("Height")
#label y axis
plt.ylabel("Weight")
#out<<

在这里插入图片描述

calories_burnt = [65, 75, 95, 99]
#draw the plot for calories burnt
plt.plot(calories_burnt)
#draw the plot for weight
plt.plot(weight)
#out<<

在这里插入图片描述

# draw the plot for calories burnt
plt.plot(calories_burnt)
# draw the plot for weight
plt.plot(weight)

#add lengend int the lower right part of the figure
plt.lengend(labels = ['Calories Burnt','Weight'],loc = 'lower right')
#out<<

在这里插入图片描述

# draw the plot
plt.plot(calories_burnt)
plt.plot(weight)

# add legend in the lower right part of the figure
plt.legend(labels=['Calories Burnt', 'Weight'], loc='lower right')
#set labels int each of these persons
plt.xticks([0,1,2,3],['p1','p2','p3','p4']);
#out<<

在这里插入图片描述

#create 4 plots
fig,ax = plt.subplots(nrowa = 2,ncols = 2,figsize = (6,6))

#plot on 0 row and 0 column 
ax[0,0].plot(calories_burnt,'go')

#plot on 0 row and 0 column 
ax[0,1].plot("Weight")

#set titles for subplots
ax[0,0].set_title("Calories Burnt")
ax[0,1].set_title("Weight")

#set labels for each of these persons
ax[0,0],set_xticklabels(labels = ['p1','p2','p3','p4'])
ax[0,1].set_xticklabels(labels = ['p1','p2','p3','p4'])
#out<<

在这里插入图片描述

# create 2 plots
fig,ax = plt.subplots(nrow = 1,ncols = 2,figsize = (6,6),sharex = True,sharey = True)

# plot on 0 row and 0 column
ax[0].plot(calories_burnt,'go')

# plot on 0 row and 1 column
ax[1].plot(weight)

# set titles for subplots
ax[0].set_title("Calories Burnt")
ax[1].set_title("Weight")

# set ticks for each of these persons
ax[0].set_xticks([0,1,2,3]);
ax[1].set_xticks([0,1,2,3]);

# set labels for each of these persons
ax[0].set_xticklabels(labels=['p1', 'p2', 'p3', 'p4']);
ax[1].set_xticklabels(labels=['p1', 'p2', 'p3', 'p4']);
#out<<

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值