Matplotlib中的“plt”和“ax”,设置大小刻度,设置实线和虚线方格线

一、plt还是ax

看了许多书本中的画图示例,有直接在plt上画的,也有用ax画的,这两者究竟是什么,又有哪些区别呢。
从下面这一行代码进行解读:

fig,ax=plt.subplots()
  • 什么是fig?

在任何绘图之前,我们需要一个Figure对象,至少要有这一层才可以画。.plt 对应的就是最高层 scripting layer。这就是为什么它简单上手,但要调细节就不灵了。即:

fig=plt.figure()
  • 什么是ax,axes?
    axis 指的就是 x 坐标轴,y 坐标轴等,代表的是一根坐标轴。而 axes 在英文里是 axis 的复数形式,也就是说,axes 代表的其实是 figure 当中的一套坐标轴。之所以说一套而不是两个坐标轴,是因为如果你画三维的图,axes 就代表 3 根坐标轴了。所以,在一个 figure 当中,每添加一次 subplot ,其实就是添加了一套坐标轴,也就是添加了一个 axes,放在二维坐标里就是你添加了两根坐标轴,分别是 x 轴和 y 轴。

ax.plot 是在 artist layer 上操作。基本上可以实现任何细节的调试。

二、画图

1.引入库

代码如下(示例):

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

2.读入数据

代码如下(示例):
数据集格式
在这里插入图片描述

3.实现的结果

在这里插入图片描述

3.1 plt代码


# Create figure
plt.figure(figsize=(12,6),dpi=300)
# Create bar plot
labels = data['MovieTitle']
t = data['Tomatometer']
a = data['AudienceScore']

x=np.arange(0,len(labels)*2,2)

width = 0.6
plt.bar(x-width/2,t,width=width,label='Tomatometer')
plt.bar(x+width/2,a,width=width,label='AudienceScore')

# Specify ticks

# Get current Axes for setting tick labels and horizontal grid

# Set tick labels
plt.xticks(x,labels=labels,rotation=20)
# Add minor ticks for y-axis in the interval of 5
plt.minorticks_on()

y = []
for i in range(0,101,20):
    if i % 20 ==0:
        y.append(str(i)+'%')

y1 = np.arange(0,101,20)

plt.yticks(y1,y)
# Add major horizontal grid with solid lines
plt.grid(which='major', axis='y',ls='-')


# Add minor horizontal grid with dashed lines
plt.grid(which='minor', axis='y',ls=':')

# Add title
plt.title('Movie comparison')
# Add legend
plt.legend()
# Show plot
plt.show()

3.2 ax代码


from matplotlib.ticker import MultipleLocator

# Create figure
plt.figure(figsize=(16,8),dpi=300)
# Create bar plot
labels = data['MovieTitle']
t = data['Tomatometer']
a = data['AudienceScore']
x=np.arange(0,len(labels)*2,2)
width = 0.4
ax=plt.gca()
ax.bar(x-width/2,t,width=width,label='Tomatometer')
ax.bar(x+width/2,a,width=width,label='AudienceScore')
# Specify ticks
plt.xticks(x,labels=labels,rotation=20)
# Get current Axes for setting tick labels and horizontal grid

# Set tick labels
yticks = ["0%","20%","40%","60%","80%","100%"]
y_ = np.arange(0,101,20)
ax.set_yticks(y_)
ax.set_yticklabels(yticks,fontsize=10) #
# Add minor ticks for y-axis in the interval of 5
yminorLocator = MultipleLocator(5)
ax.yaxis.set_minor_locator(yminorLocator)
# Add major horizontal grid with solid lines
ax.grid(which='major',linestyle='-',axis='y')
# Add minor horizontal grid with dashed lines
ax.grid(which="minor",linestyle=':')
# Add title
ax.set_title("Compare with Tomatometer & Audidence Score",fontsize=15)
# Add legend
ax.legend()
# Show plot
plt.show()


总结

如果将Matplotlib绘图和我们平常画画相类比,可以把Figure想象成一张纸(一般被称之为画布),Axes代表的则是纸中的一片区域(当然可以有多个区域,这是后续要说到的subplots),上一张更形象一点的图。

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值