在同一个 Pandas 图上绘制不同长度的数组
Graphing arrays with different lengths on the same Pandas graph
a
和 b
分别是 A Values
和 B Values
的日期时间索引。 A Values
的大小大于 B Values
我想重现一个代码,将它们放在同一个图表上。我如何才能编写代码,在同一图表上绘制 2 个不同长度的 numpy 数组?
import pandas as pd
import numpy as np
import datetime
a = np.array(['2017-09-15 07:11:00', '2017-09-15 12:11:00', '2017-12-22 06:35:00'], dtype='datetime64[ns]')
b = np.array(['2017-09-10 02:25:00', '2017-12-11 03:11:00'], dtype='datetime64[ns]')
graph= pd.DataFrame({
'A Values': np.array([11,-3,4]),
'B Values': np.array([2,4])
}, index= [a,b]).plot.line()
错误代码:
ValueError: all arrays must be same length
您可以将数组填充为相同大小,将小数组填充为零或 none:
使用此代码段来制作它:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import datetime
a = np.array(['2017-09-15 07:11:00', '2017-09-15 12:11:00', '2017-12-22 06:35:00'], dtype='datetime64[ns]')
b = np.array(['2017-09-10 02:25:00', '2017-12-11 03:11:00'], dtype='datetime64[ns]')
s1=[11,-3,4]
s2=[2,4]
m = max(len(s1),len(s2))
a1=[None] * m
a2=[None] * m
ax1=[None] * m
ax2=[None] * m
for i in range(len(s1)):
a1[i] = s1[i]
ax1[i]=a[i]
for i in range(len(s2)):
a2[i] = s2[i]
ax2[i]=b[i]
graph= pd.DataFrame({
'A Values': np.array(a1),
'B Values': np.array(a2)
}, index= [ax1,ax2]).plot.line()
plt.xticks(rotation=0)
plt.tick_params(axis='both', which='major', labelsize=5)
#labelsize=5 instead of labelsize=10
plt.show()
输出:
希望你觉得有用
只需捕获 matplotlib 为第一个图生成的轴对象,然后用它来绘制第二个系列:
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
a = np.array(['2017-09-15 07:11:00', '2017-09-15 12:11:00', '2017-12-22 06:35:00'], dtype='datetime64[ns]')
b = np.array(['2017-09-10 02:25:00', '2017-12-11 03:11:00'], dtype='datetime64[ns]')
ax1 = pd.DataFrame({'A Values': np.array([11,-3,4])}, index= a).plot.line()
pd.DataFrame({'B Values': np.array([2,4])}, index= b).plot.line(ax=ax1)
plt.show()
示例输出:
如果您想知道 matplotlib 与您的问题有什么关系 - 除非另有说明,pandas 将数据提供给 matplotlib 以生成图表。
a
和 b
分别是 A Values
和 B Values
的日期时间索引。 A Values
的大小大于 B Values
我想重现一个代码,将它们放在同一个图表上。我如何才能编写代码,在同一图表上绘制 2 个不同长度的 numpy 数组?
import pandas as pd
import numpy as np
import datetime
a = np.array(['2017-09-15 07:11:00', '2017-09-15 12:11:00', '2017-12-22 06:35:00'], dtype='datetime64[ns]')
b = np.array(['2017-09-10 02:25:00', '2017-12-11 03:11:00'], dtype='datetime64[ns]')
graph= pd.DataFrame({
'A Values': np.array([11,-3,4]),
'B Values': np.array([2,4])
}, index= [a,b]).plot.line()
错误代码:
ValueError: all arrays must be same length
您可以将数组填充为相同大小,将小数组填充为零或 none: 使用此代码段来制作它:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import datetime
a = np.array(['2017-09-15 07:11:00', '2017-09-15 12:11:00', '2017-12-22 06:35:00'], dtype='datetime64[ns]')
b = np.array(['2017-09-10 02:25:00', '2017-12-11 03:11:00'], dtype='datetime64[ns]')
s1=[11,-3,4]
s2=[2,4]
m = max(len(s1),len(s2))
a1=[None] * m
a2=[None] * m
ax1=[None] * m
ax2=[None] * m
for i in range(len(s1)):
a1[i] = s1[i]
ax1[i]=a[i]
for i in range(len(s2)):
a2[i] = s2[i]
ax2[i]=b[i]
graph= pd.DataFrame({
'A Values': np.array(a1),
'B Values': np.array(a2)
}, index= [ax1,ax2]).plot.line()
plt.xticks(rotation=0)
plt.tick_params(axis='both', which='major', labelsize=5)
#labelsize=5 instead of labelsize=10
plt.show()
输出:
希望你觉得有用
只需捕获 matplotlib 为第一个图生成的轴对象,然后用它来绘制第二个系列:
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
a = np.array(['2017-09-15 07:11:00', '2017-09-15 12:11:00', '2017-12-22 06:35:00'], dtype='datetime64[ns]')
b = np.array(['2017-09-10 02:25:00', '2017-12-11 03:11:00'], dtype='datetime64[ns]')
ax1 = pd.DataFrame({'A Values': np.array([11,-3,4])}, index= a).plot.line()
pd.DataFrame({'B Values': np.array([2,4])}, index= b).plot.line(ax=ax1)
plt.show()
示例输出:
如果您想知道 matplotlib 与您的问题有什么关系 - 除非另有说明,pandas 将数据提供给 matplotlib 以生成图表。