python - 如何绘制不同长度的数组

标签 python numpy matplotlib

如何绘制不同长度但在 x 轴上正确延伸的数组?下面的代码生成 2 个数据集,第二个数据集较短。我对每组数据进行插值,每个数据点使用多个样本对数据进行重新采样。当我绘制所有数据时,较短的数据集不会延伸到绘图的末尾。我不需要子图,我需要将数据相互叠加。

#!/usr/bin/env python3

from scipy import interpolate
import matplotlib.pyplot as plt
import numpy as np

num_points = 100

# Generate an array of data, interpolate, re-sample and graph
x1 = np.arange(0, num_points)
y1 = np.cos(x1)
f1 = interpolate.interp1d(x1, y1, kind='cubic')
xnew1 = np.arange(0, num_points - 1, 0.2)
ynew1 = f1(xnew1)  

plt.plot(x1, y1, color='g', label='input 1')
plt.plot(x1, y1, 'o', color='g')
plt.plot(xnew1, ynew1, color='m', label='interp 1')
plt.plot(xnew1, ynew1, '+', color='m')

# Generate ana array different size of data, interpolate, re-sample and graph
x2 = np.arange(0, num_points/2)
y2 = np.sin(x2)
f2 = interpolate.interp1d(x2, y2, kind='cubic')
xnew2 = np.arange(0, (num_points/2) - 1, 0.2)
ynew2 = f2(xnew2)  

plt.plot(x2, y2, color='k', label='input 2')
plt.plot(x2, y2, 'o', color='k')
plt.plot(xnew2, ynew2, color='r', label='interp 2')
plt.plot(xnew2, ynew2, '+', color='r')

plt.legend(loc='upper left')
plt.show()

Plot of both data sets

最佳答案

如果我理解正确,这可以通过使用共享相同 y 轴的两个不同轴来完成,如 this matplotlib example 中所述。 .

根据您的情况,您可以通过进行以下修改来实现此目的:

from scipy import interpolate
import matplotlib.pyplot as plt
import numpy as np
num_points = 100

# Generate an array of data, interpolate, re-sample and graph
x1 = np.arange(0, num_points)
y1 = np.cos(x1)
f1 = interpolate.interp1d(x1, y1, kind='cubic')
xnew1 = np.arange(0, num_points - 1, 0.2)
ynew1 = f1(xnew1)  

fig, ax1 = plt.subplots()  # Create the first axis

ax1.plot(x1, y1, color='g', label='input 1')
ax1.plot(x1, y1, 'o', color='g')
ax1.plot(xnew1, ynew1, color='m', label='interp 1')
ax1.plot(xnew1, ynew1, '+', color='m')

ax2 = ax1.twiny()          # Create a twin which shares the y-axis

# Generate an array different size of data, interpolate, re-sample and graph
x2 = np.arange(0, num_points/2)
y2 = np.sin(x2)
f2 = interpolate.interp1d(x2, y2, kind='cubic')
xnew2 = np.arange(0, (num_points/2) - 1, 0.2)
ynew2 = f2(xnew2)  

ax2.plot(x2, y2, color='k', label='input 2')
ax2.plot(x2, y2, 'o', color='k')
ax2.plot(xnew2, ynew2, color='r', label='interp 2')
ax2.plot(xnew2, ynew2, '+', color='r')
plt.figlegend(loc='upper left', bbox_to_anchor=(0.065, 0.3, 0.5, 0.5))
plt.show()

这会给你一些看起来像

的东西

Output from modified code

编辑

为了正确显示图例,您可以为所有子图构建一个图例,如 this demo 中所述。 。请注意,使用此方法需要对图例的边界框进行一些人工处理,并且有比我在行中指定 4 元组 float 更简洁的方法来执行此操作

plt.figlegend(loc='upper left', bbox_to_anchor=(0.065, 0.3, 0.5, 0.5))

关于python - 如何绘制不同长度的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58926848/

相关文章:

python - 用 Python 自动化无聊的事情 - Collat​​z Sequence

python - 如果行值等于 x,则前向填充列 - Pandas

python - 在python中从网上下载一个excel文件

python - 使用覆盖率,我如何测试这条线?

python - Numpy:如何快速替换矩阵中的相等值?

Python 一种热向量编码

python - 获取 Matplotlib 表格的单元格宽度以覆盖文本和周围的图形

python - 对指定像素执行不同的邻域操作

python - matplotlib:在时间序列图中将周期标签置于周期数据的中心

matplotlib - 如何删除Seaborn热图上的轴刻度线