【Python学习笔记】Matplotlib画图

一、基本函数

1. plt.plot

参数功能选项
color改变折线颜色‘r’,‘g’,‘b’,‘black’,‘gray’
marker改变数据点的形状‘.’: 小圆点;‘,’: 像素点;‘o’: 实心圆;‘v’: 垂直三角形;‘^’: 正三角形;‘<’: 左三角形;‘>’: 右三角形;‘1’: 向下平行竖线;‘2’: 向上平行竖线;‘3’: 向左平行横线;‘4’: ;右平行横线;‘s’: 正方形;‘p’: 五边形;‘*’: 星号;‘h’: 六边形1;‘H’: 六边形2;‘+’: 加号;‘x’: X号;‘d’: 小菱形;‘D’: 大菱形;’
markerfacecolor改变数据点的填充色
markeredgecolor改变数据点的边缘色
markersize改变数据点的大小从0到无穷大的浮点数
linestyle改变折线样式‘-’: 实线 ;‘–’: 虚线;‘-.’: 点划线;‘:’: 点线;‘’: 无线条,只显示标记;‘None’: 无线条,不显示标记;’ ': 无线条,不显示标记
linewidth改变折线宽度从0到无穷大的浮点数
label绘制的线条的标签字符串类型
alpha线条和标记的透明度从0到1的浮点数
aa是否开启抗锯齿Ture;False

2. plt.text

参数功能选项
s标签的符号字符串
fontsize标签字体大小整数
verticalalignment垂直对齐方式center,top,bottom,baseline
horizontalalignment水平对齐方式center, right,left
rotation标签的旋转角度以逆时针计算,取整
style设置字体的风格
weight设置字体的粗细
string注释文本内容
color注释文本内容的字体颜色
bbox给字体添加框bbox=dict(facecolor=‘red’, alpha=0.5) 等

3. 设置刻度

# 网格样式线型,网格颜色,网格透明度
plt.xticks(np.linspace(0,epochs,11), size = 16, weight=1000)
# 不设置
plt.xticks([])
# 第二次修改刻度
import matplotlib.pyplot as plt

months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
sales = [12000, 15000, 18000, 14000, 16000, 20000, 23000, 21000, 19000, 22000, 25000, 28000]

plt.bar(months, sales)
plt.xticks([2, 5, 8, 11], ['Q1', 'Q2', 'Q3', 'Q4'])
plt.xlabel('Month')
plt.ylabel('Sales')
plt.title('Sales by Quarter')
plt.show()
# y轴改为对数坐标
plt.yscale("log")
plt.yticks([0.0001, 0.001, 0.01, 0.1, 1, 10])

4. 设置坐标

plt.xlabel("x",fontdict={'weight' : 1000,'size' : 16})
plt.ylabel("y",fontdict={'weight' : 'normal','size' : 16})

5.设置网格

# 网格样式线型,网格颜色,网格透明度
plt.grid(ls=":",color="gray",alpha=0.5)

二、绘制折线图

import matplotlib.pyplot as plt
import numpy as np

# fig = plt.figure(figsize=(20,20), dpi=1024)

x1 = np.array([1, 2, 3, 4])
y1 = np.array([1, 1.5, 2, 2.5])
x2 = x1
y2 = y1 * 1.5

plt.plot(x1, y1, '-', marker='o', color='b', linewidth=1, markersize=2, label='A')
plt.plot(x2, y2, '--', marker='^', color='orange',linewidth=1, markersize=2, label='B')

for a, b in zip(x1, y1):
    plt.text(a, b, (a,b), ha='center', va='bottom', color='b')
for a, b in zip(x2, y2):
    plt.text(a, b, (a,b), ha='center', va='bottom', color='orange')
plt.xlabel('x')
plt.ylabel('y')
plt.xlim(1,4)
plt.legend()
plt.title('result')
# plt.savefig(f'./result.png', dpi=1024)
plt.show()

在这里插入图片描述

三、绘制散点图

# 其他的参数参考折线图
import matplotlib.pyplot as plt

# x坐标和y坐标的数据
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

# 绘制散点图
plt.scatter(x, y)

# 在每个点上显示相应的值
for i in range(len(x)):
    plt.text(x[i], y[i], f'({x[i]}, {y[i]})', ha='center', va='bottom')

# 添加标题和坐标轴标签
plt.title('Scatter Plot with Values')
plt.xlabel('X')
plt.ylabel('Y')

# 展示图形
plt.show()

在这里插入图片描述

四、绘制柱状图

import matplotlib.pyplot as plt

# x轴标签和对应的数值
labels = ['A', 'B', 'C', 'D', 'E']
values = [10, 20, 15, 25, 30]

# 绘制柱状图
plt.bar(labels, values)

# 在每个柱子上显示相应的值
for i in range(len(labels)):
    plt.text(labels[i], values[i] + 1, str(values[i]), ha='center')

# 添加标题和坐标轴标签
plt.title('Bar Chart with Values')
plt.xlabel('X')
plt.ylabel('Y')

# 展示图形
plt.show()

在这里插入图片描述

五、绘制双坐标图

import matplotlib.pyplot as plt

# x轴数据
x = [1, 2, 3, 4, 5]

# 第一个y轴的数据
y1 = [10, 20, 30, 40, 50]

# 第二个y轴的数据
y2 = [1, 4, 9, 16, 25]

# 创建第一个y轴(左侧轴)
fig, ax1 = plt.subplots()

# 绘制第一个y轴的线形图
line1 = ax1.plot(x, y1, 'g-', label='Y1')
ax1.set_xlabel('X')
ax1.set_ylabel('Y1', color='g')

# 创建第二个y轴(右侧轴)
ax2 = ax1.twinx()

# 绘制第二个y轴的散点图
scatter = ax2.scatter(x, y2, c='r', marker='o', label='Y2')
ax2.set_ylabel('Y2', color='r')

# 在散点图上显示每个数据点的值
for i in range(len(x)):
    ax2.text(x[i], y2[i]+1, str(y2[i]), ha='center', va='bottom')

# 调整图例
handles1, labels1 = ax1.get_legend_handles_labels()
handles2, labels2 = ax2.get_legend_handles_labels()
plt.legend(handles1 + handles2, labels1 + labels2)

# 添加标题
plt.title('Double Axis Plot with Values')

# 展示图形
plt.show()

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

修炼清爽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值