Matplotlib基础使用方法

当使用Matplotlib库时,一般用于创建各种类型的图表,包括折线图、散点图、柱状图、饼图等。以下是一些基本的Matplotlib用法介绍:
基本绘图
import matplotlib.pyplot as plt

# 创建简单的折线图
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]

plt.plot(x, y)

# 添加标题和标签
plt.title('Simple Line Plot')
plt.xlabel('X-axis Label')
plt.ylabel('Y-axis Label')

# 显示图形
plt.show()
散点图
import matplotlib.pyplot as plt

# 创建散点图
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]

plt.scatter(x, y, color='red', marker='o')

# 添加标题和标签
plt.title('Scatter Plot')
plt.xlabel('X-axis Label')
plt.ylabel('Y-axis Label')

# 显示图形
plt.show()
柱状图
import matplotlib.pyplot as plt

# 创建柱状图
categories = ['Category A', 'Category B', 'Category C']
values = [3, 7, 5]

plt.bar(categories, values, color='blue')

# 添加标题和标签
plt.title('Bar Chart')
plt.xlabel('Categories')
plt.ylabel('Values')

# 显示图形
plt.show()
饼图
import matplotlib.pyplot as plt

# 创建饼图
labels = ['Category A', 'Category B', 'Category C']
sizes = [30, 45, 25]

plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90, colors=['red', 'green', 'blue'])

# 添加标题
plt.title('Pie Chart')

# 显示图形
plt.show()
常用函数

        plt.plot()是Matplotlib库中用于创建折线图的函数。它至少接受两个参数,分别是x轴和y轴的数据,并以这些数据为基础创建折线图。

plt.plot(x, y, color='red', linestyle='--', marker='o', label='Data Points')

        `plt.plot()`函数的一些常用参数:

        `color`:指定线的颜色。

        `linestyle`:指定线的样式,如

  • 13
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值