折线图
基础对应 x y 一一对应 (-3,10)...
# 面向过程的版本
x = [-3, 5, 7] #准备数据的x轴坐标
y = [10, 2, 5] #准备数据的y轴坐标
plt.figure(figsize=(15,3)) #figure 创建画布 figsize指定画布大小
plt.plot(x, y) #plot 绘图
plt.xlim(-5, 10) #xlim 设置x轴坐标的范围
plt.ylim(-3, 15) #ylim 设置y轴坐标的范围
plt.xlabel('X Axis',size=20) # 设置x轴标签 size字体大小
plt.ylabel('Y axis') # 设置y轴标签
plt.title('Line Plot',size=30) # 设置标题内容, size 字体大小
plt.show() #显示图片
# 面向对象 版本
fig, ax = plt.subplots(figsize=(15,3)) #创建坐标轴对象
ax.plot(x, y) #调用坐标轴的绘图方法
ax.set_xlim(0, 10) # 调用坐标轴的设置x轴上下限的方法
ax.set_ylim(-3, 8)
ax.set_xlabel('X axis') # 调用坐标轴的设置x轴标签的方法
ax.set_ylabel('Y axis',size = 20) # 调用坐标轴的设置y轴标签的方法
ax.set_title('Line Plot',size = 30) # 调用坐标轴的设置标题的方法
plt.show()
import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([1, 2, 6, 8])
ypoints = np.array([3, 8, 1, 10])
plt.plot(xpoints, ypoints)
plt.show()
实心圆 'o'
plt.plot(ypoints, 'r+')
#%%
import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([1, 8])
ypoints = np.array([3, 10])
plt.plot(xpoints, ypoints,'o')
plt.show()
两条线
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0,4*np.pi,0.1) # start,stop,step
# print(x)
y = np.sin(x)
z = np.cos(x)
plt.plot(x,y,x,z)
plt.show()
标记、线型和颜色
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([6, 2, 13, 10])
plt.plot(ypoints, '^-.g')
plt.show()
标记 以及内外层颜色
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([6, 2, 13, 10])
plt.plot(ypoints, marker = 'o', ms = 20, mec = '#4CAF50', mfc = '#4CAF50')
plt.show()
一图多线
# 简化
import matplotlib.pyplot as plt
import numpy as np
y1 = np.array([3, 7, 5, 9])
y2 = np.array([6, 2, 13, 10])
plt.plot(y1)
plt.plot(y2)
plt.show()
#原版
import matplotlib.pyplot as plt
import numpy as np
x1 = np.array([0, 1, 2, 3])
y1 = np.array([3, 7, 5, 9])
x2 = np.array([0, 1, 2, 3])
y2 = np.array([6, 2, 13, 10])
plt.plot(x1, y1, x2, y2)
plt.show()
网格线
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1, 2, 3, 4])
y = np.array([1, 4, 9, 16])
plt.title("RUNOOB grid() Test")
plt.xlabel("x - label")
plt.ylabel("y - label")
plt.plot(x, y)
plt.grid(axis='x') # 设置 y 就在轴方向显示网格线
plt.show()
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1, 2, 3, 4])
y = np.array([1, 4, 9, 16])
plt.title("RUNOOB grid() Test")
plt.xlabel("x - label")
plt.ylabel("y - label")
plt.plot(x, y)
plt.grid(color = 'g', linestyle = '-.', linewidth = 1)
plt.show()
主图下四个子图
import matplotlib.pyplot as plt
import numpy as np
#plot 1:
x = np.array([0, 6])
y = np.array([0, 100])
plt.subplot(2, 2, 1)
plt.plot(x,y)
plt.title("plot 1")
#plot 2:
x = np.array([1, 2, 3, 4])
y = np.array([1, 4, 9, 16])
plt.subplot(2, 2, 2)
plt.plot(x,y)
plt.title("plot 2")
#plot 3:
x = np.array([1, 2, 3, 4])
y = np.array([3, 5, 7, 9])
plt.subplot(2, 2, 3)
plt.plot(x,y)
plt.title("plot 3")
#plot 4:
x = np.array([1, 2, 3, 4])
y = np.array([4, 5, 6, 7])
plt.subplot(2, 2, 4)
plt.plot(x,y)
plt.title("plot 4")
plt.suptitle("RUNOOB subplot Test")
plt.show()
子图 以及共享轴坐标 散点图
import matplotlib.pyplot as plt
import numpy as np
# 创建一些测试数据 -- 图1
x = np.linspace(0, 2*np.pi, 400)
y = np.sin(x**2)
# print(x)
# print('-----------------------')
# print(y)
# 创建一个画像和子图 -- 图2
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('Simple plot')
# 创建两个子图 -- 图3
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(x, y)
ax1.set_title('Sharing Y axis')
ax2.scatter(x, y)
# 创建四个子图 -- 图4
fig, axs = plt.subplots(2, 2, subplot_kw=dict(projection="polar"))
axs[0, 0].plot(x, y)
axs[1, 1].scatter(x, y)
# 共享 x 轴
plt.subplots(2, 2, sharex='col')
# 共享 y 轴
plt.subplots(2, 2, sharey='row')
#
# 共享 x 轴和 y 轴
plt.subplots(2, 2, sharex='all', sharey='all')
# # 这个也是共享 x 轴和 y 轴
plt.subplots(2, 2, sharex=True, sharey=True)
print('-----------------------------------------')
# 创建标识为 10 的图,已经存在的则删除
fig, ax = plt.subplots(num=10, clear=True)
plt.show()
散点图 随机种子
import numpy as np
import matplotlib.pyplot as plt
# 随机数生成器的种子
np.random.seed(19680801)
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = (30 * np.random.rand(N))**2 # 0 to 15 point radii
plt.scatter(x, y, s=area, c=colors, alpha=0.5) # 设置颜色及透明度
plt.title("RUNOOB Scatter Test") # 设置标题
plt.show()
图例 以及色系
import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = np.array([0, 10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])
plt.scatter(x, y, c=colors, cmap='viridis')
plt.colorbar()
plt.show()
柱状图
import matplotlib.pyplot as plt
import numpy as np
x = np.array(["Runoob-1", "Runoob-2", "Runoob-3", "C-RUNOOB"])
y = np.array([12, 22, 6, 18])
plt.bar(x,y,color = ["r","g","b","k"],width = 0.8)
plt.show()
条形图
import matplotlib.pyplot as plt
import numpy as np
x = np.array(["Runoob-1", "Runoob-2", "Runoob-3", "C-RUNOOB"])
y = np.array([12, 22, 6, 18])
plt.barh(x,y,color = "#4CAF50",height = 0.8)
plt.show()
饼图
plt.show()
#%%
import matplotlib.pyplot as plt
import numpy as np
y = np.array([35, 25, 25, 15])
plt.pie(y,
labels=['A','B','C','D'], # 设置饼图标签
colors=["#d5695d", "#5d8ca8", "#65a479", "#a564c9"], # 设置饼图颜色
)
plt.title("RUNOOB Pie Test") # 设置标题
plt.show()
饼图样式
import matplotlib.pyplot as plt
# 数据
sizes = [15, 30, 45, 10]
# 饼图的标签
labels = ['A', 'B', 'C', 'D']
# 饼图的颜色
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']
# 突出显示第二个扇形
explode = (0.1, 0, 0.3, 0)
# 绘制饼图
plt.pie(
sizes,
labels=labels,
colors=colors,
autopct='%1.1f%%',
explode=explode,
shadow=True,
startangle=90)
# 标题
plt.title("RUNOOB Pie Test")
# 显示图形
plt.show()
直方图
import matplotlib.pyplot as plt
import numpy as np
# 生成三组随机数据
data1 = np.random.normal(0, 1, 1000)
data2 = np.random.normal(2, 1, 1000)
data3 = np.random.normal(-2, 1, 1000)
# 绘制直方图
plt.hist(data1, bins=5, alpha=0.5, label='Data 1')
plt.hist(data2, bins=5, alpha=0.5, label='Data 2')
plt.hist(data3, bins=5, alpha=0.5, label='Data 3')
# 设置图表属性
plt.title('RUNOOB hist() TEST')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.legend()
# 显示图表
plt.show()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# 使用 NumPy 生成随机数
random_data = np.random.normal(170, 10, 250)
# print(random_data)
# print(random_data.size)
# 将数据转换为 Pandas DataFrame
dataframe = pd.DataFrame(random_data)
# 使用 Pandas hist() 方法绘制直方图
dataframe.hist()
# 设置图表属性
plt.title('RUNOOB hist() Test')
plt.xlabel('X-Value')
plt.ylabel('Y-Value')
# 显示图表
plt.show()