[数据可视化] matplotlib基本设置

1.颜色对照表

在这里插入图片描述

2.基本作图参数

(1)线条符号
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * np.pi, 50)
plt.plot(x, np.sin(x))
plt.plot(x, np.sin(x)-1, marker='o')
plt.plot(x, np.sin(x)-2, marker='*')

plt.show()

在这里插入图片描述

(2)画布
import matplotlib.pyplot as plt

fig = plt.figure() # 创建一个画布
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # [距离左边,下边,坐标轴宽度,坐标轴高度]
plt.show()

在这里插入图片描述

(3)坐标轴
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 10)
y = np.sin(x)

# 创建画布
fig = plt.figure(figsize=(10, 5), dpi=100) # figsize表示画布的长宽,单位为英寸,dpi表示每英寸的像素值
# 创建坐标轴
ax1 = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # 第一个坐标轴的范围
ax2 = fig.add_axes([0.2, 0.5, 0.4, 0.3]) # 第二个坐标轴的范围

ax1.plot(x, y)
ax2.plot(x, y)
plt.show()

在这里插入图片描述

(4)保存图片
fig.savefig("fig.png") # 保存图片

(5)标题、轴标、图例、颜色、线型、透明度

import matplotlib.pyplot as plt

x = [1,2,3,4]
y1 = [1,2,3,3]
fig = plt.figure() # 创建一个画布
ax = fig.add_subplot(111) # 将ax加入画布中
ax.set_title('title') # 设置标题
ax.set_xlabel('X') # 设置X轴标签
ax.set_ylabel('Y') # 设置Y轴标签
ax.plot(x, y1, label='line1', color='r') # 设置line1图例,设置颜色,支持颜色名和RGB值
ax.plot(x, [i+1 for i in y1], label='line2', color='#1155dd') # 设置line2图例
ax.plot(x, [i+2 for i in y1], label='line3', ls=':', lw=2.5) # ls控制线的类型,lw控制线的宽度
ax.plot(x, [i+3 for i in y1], label='line4', alpha=0.5) # alpha控制透明度
# legend参数:'best','upper right', 'upper left', 'lower right', 'lower left', 'center left', 'center right', 'lower center', 'upper center', 'center'
ax.legend(loc="best") # 选择图例摆放位置
plt.show()

在这里插入图片描述

(6)范围
import numpy as np
import matplotlib.pyplot as plt

x = np.array([0, 1, 2, 3])
fig, ax = plt.subplots(1, 3, figsize=(12, 4)) # 画三个子图,一行三列
ax[0].plot(x, x**2)
ax[1].plot(x, x**2)
ax[1].set_xlim([0, 5]) # 设置横坐标轴范围
ax[1].set_ylim([0, 12]) # 设置纵坐标轴范围
ax[2].plot(x, x**2)
ax[2].axis('tight') # 自动调整到比较紧凑的范围
plt.show()

在这里插入图片描述

(7)对数刻度
import numpy as np
import matplotlib.pyplot as plt

x = np.arange(50)
fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
ax.plot(x, x**2)
ax.set_yscale('log') # 设置对数刻度
plt.show()

在这里插入图片描述

(8)坐标轴标号和符号
import numpy as np
import matplotlib.pyplot as plt

x = np.array([0.5, 1.5, 2.5, 3.5])
fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
ax.plot(x, x**2)
ax.set_xticks([0, 1, 2, 3, 4]) # 显示数字刻度
ax.set_xticklabels(['a', 'b', 'c', 'd', 'e']) # 显示字符刻度
plt.show()

在这里插入图片描述

(9)坐标轴刻度与坐标轴标签间距
import numpy as np
import matplotlib.pyplot as plt

x = np.arange(5)

fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
ax.plot(x, x**2)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.xaxis.labelpad = 20 # 设置x轴与轴标签的距离
ax.yaxis.labelpad = 30 # 设置y轴与轴标签的距离
plt.show()

在这里插入图片描述

(10)调整坐标轴位置
import numpy as np
import matplotlib.pyplot as plt

x = np.arange(5)
fig, ax = plt.subplots(1,1)
ax.plot(x, x**2)
fig.subplots_adjust(left=0.1, right=0.6, bottom=0.15, top=0.6) # 调整坐标轴位置
plt.show()

在这里插入图片描述

(11)网格
import numpy as np
import matplotlib.pyplot as plt

x = np.arange(5)
fig, ax = plt.subplots(1, 1)
ax.plot(x, x**2)
ax.grid(color='r', ls='dashed', lw=0.5, alpha=0.5) # 设置网格
plt.show()

在这里插入图片描述

(12)轴属性
import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])

ax.spines['top'].set_color('none') # 删除上坐标轴
ax.spines['right'].set_color('none') # 删除右坐标轴
ax.xaxis.set_ticks_position('bottom') # 刻度显示位置
ax.spines['bottom'].set_position(('data',0)) # 底部坐标轴从0刻度线开始

ax.yaxis.set_ticks_position('left') # 刻度显示位置
ax.spines['left'].set_position(('data',0)) # 左边坐标轴从0刻度线开始

x = np.linspace(-5, 5, 50)
ax.plot(x, x**2)
plt.show()

在这里插入图片描述

(13)双坐标轴
import numpy as np
import matplotlib.pyplot as plt

x = np.arange(5)
fig = plt.figure()
ax1 = fig.add_axes([0.1, 0.1, 0.8, 0.8])
ax1.plot(x, x**2, color='c')
ax1.set_ylabel('left label', color='c')

ax2 = ax1.twinx() # 共用x轴,双y轴
ax2.plot(x, x**3, color='r')
ax2.set_ylabel('right label', color='r')

# 设置坐标轴颜色
for i in ax1.get_yticklabels():
    i.set_color('c')

for j in ax2.get_yticklabels():
    j.set_color('r')

plt.show()

在这里插入图片描述

(14)add_subplot方法
import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(2, 2, 1) # 2行2列的子图
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3)
ax4 = fig.add_subplot(2, 2, 4)

plt.plot([1, 2, 3, 4]) # plt.plot()命令应用在最近的subplot上

plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt

fig = plt.figure(5)
plt.subplot(221)
plt.subplot(222)
plt.subplot(212)
plt.subplots_adjust(left=0.08, right=0.95, wspace=0.25, hspace=0.45) # 子图之间的间隔
plt.show()

在这里插入图片描述

(15)subplots方法
import matplotlib.pyplot as plt

# 参数
# nrows:subplot的行数
# ncols:subplot的列数
# sharex:所有subplot使用相同的x轴刻度
# sharey:所有subplot使用相同的y轴刻度
# subplot_kw:用于创建subplot的关键字字典

fig, axes = plt.subplots(2, 2)

ax1 = axes[0, 0]
ax2 = axes[0, 1]
ax3 = axes[1, 0]
ax4 = axes[1, 1]
plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np

fig, axes = plt.subplots(nrows=2, ncols=2, sharex=True, sharey=True)
for i in range(2):
    for j in range(2):
        axes[i, j].hist(np.random.randn(500), bins=50, color='c', alpha=0.5)

plt.show()

在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt

ax1 = plt.subplot(2, 2, 1)
ax2 = plt.subplot(2, 2, 2)
plt.plot(np.arange(4))
ax3 = plt.subplot(2, 2, 3)
plt.scatter(np.arange(4), np.arange(4))
ax4 = plt.subplot(2, 2, 4)
plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt

fig, axes = plt.subplots(2, 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")

for ax in axes.flat:
    ax.set(xticks=[], yticks=[])

plt.show()

在这里插入图片描述

(16)设置字体、添加文字
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rcParams

rcParams.update({'font.size': 12,'font.family': 'SimHei'})

x = np.arange(10)
y = x**2

plt.plot(x, y)
plt.title('二次函数')
plt.text(0.15, 0.2, r"$y=x^2$", fontsize=20, color='r')
plt.show()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值