根据列表数据创建图形

时间:2020-09-08 12:10:41

标签: python python-3.x list dataframe

我有此列表:

[(200, array([1.81101387])), (300, array([2.73324657])), (400, array([3.66660659])), (500, array([4.61826639])), (600, array([5.57527109])), (700, array([6.54331291])), (800, array([7.5340437])), (900, array([8.53877086])), (1000, array([9.54263164])), (1100, array([10.57365805])), (1200, array([11.61853761])), (1300, array([12.67720361])), (1400, array([13.74957748])), (1500, array([14.83556906])), (1600, array([15.93507691])), (1700, array([17.07408599])), (1800, array([18.20190138])), (1900, array([19.37230333])), (2000, array([20.52788386])), (2100, array([21.72908566])), (2200, array([22.94628865])), (2300, array([24.14308228])), (2400, array([25.38997665])), (2500, array([26.65224533])), (2600, array([27.9296472])), (2700, array([29.26490691])), (2800, array([30.57345122])), (2900, array([31.89630179])), (3000, array([33.2811809])), (3100, array([34.63333589])), (3200, array([36.05016039])), (3300, array([37.43023372])), (3400, array([38.87746378])), (3500, array([40.34008923])), (3600, array([41.81764985])), (3700, array([43.30966982])), (3800, array([44.81565878])), (3900, array([46.33511277]))]

它的元素是(高度,时间),我想知道如何创建图形(删除array()的内容)。

我有一个想法是用熊猫创建一个数据框,然后代表这些列。问题是我不知道如何将元素分开以将它们引入数据框。

谢谢。

2 个答案:

答案 0 :(得分:1)

这应该可以工作(假设x是时间,y是高度)

import pandas as pd

# add the following lines if you have to copy and paste your data
from numpy import array
data = [(200, array([1.81101387])), (300, array([2.73324657])), (400, array([3.66660659])), (500, array([4.61826639])), (600, array([5.57527109])), (700, array([6.54331291])), (800, array([7.5340437])), (900, array([8.53877086])), (1000, array([9.54263164])), (1100, array([10.57365805])), (1200, array([11.61853761])), (1300, array([12.67720361])), (1400, array([13.74957748])), (1500, array([14.83556906])), (1600, array([15.93507691])), (1700, array([17.07408599])), (1800, array([18.20190138])), (1900, array([19.37230333])), (2000, array([20.52788386])), (2100, array([21.72908566])), (2200, array([22.94628865])), (2300, array([24.14308228])), (2400, array([25.38997665])), (2500, array([26.65224533])), (2600, array([27.9296472])), (2700, array([29.26490691])), (2800, array([30.57345122])), (2900, array([31.89630179])), (3000, array([33.2811809])), (3100, array([34.63333589])), (3200, array([36.05016039])), (3300, array([37.43023372])), (3400, array([38.87746378])), (3500, array([40.34008923])), (3600, array([41.81764985])), (3700, array([43.30966982])), (3800, array([44.81565878])), (3900, array([46.33511277]))]
##

x, y = [item[1][0] for item in data], [item[0] for item in data]

df = pd.DataFrame(data={'time': x, 'height': y})

您还可以通过添加以下行来检查数据是否按时间排序:

data = sorted(data, key=lambda x: x[1][0])

答案 1 :(得分:0)

您可以将数据结构解压缩到xy,然后绘制它们。

import matplotlib.pyplot as plt 
from numpy import array 

a = [(200, array([1.81101387])), (300, array([2.73324657])), (400, array([3.66660659])), (500, array([4.61826639])), (600, array([5.57527109])), (700, array([6.54331291])), (800, array([7.5340437])), (900, array([8.53877086])), (1000, array([9.54263164])), (1100, array([10.57365805])), (1200, array([11.61853761])), (1300, array([12.67720361])), (1400, array([13.74957748])), (1500, array([14.83556906])), (1600, array([15.93507691])), (1700, array([17.07408599])), (1800, array([18.20190138])), (1900, array([19.37230333])), (2000, array([20.52788386])), (2100, array([21.72908566])), (2200, array([22.94628865])), (2300, array([24.14308228])), (2400, array([25.38997665])), (2500, array([26.65224533])), (2600, array([27.9296472])), (2700, array([29.26490691])), (2800, array([30.57345122])), (2900, array([31.89630179])), (3000, array([33.2811809])), (3100, array([34.63333589])), (3200, array([36.05016039])), (3300, array([37.43023372])), (3400, array([38.87746378])), (3500, array([40.34008923])), (3600, array([41.81764985])), (3700, array([43.30966982])), (3800, array([44.81565878])), (3900, array([46.33511277]))]

x = [_[0] for _ in a]
y = [_[1][0] for _ in a]
# Here it could be also y = [_[1] for _ in a], but we are dealing here with the fact that you have plenty of one-element arrays

plt.plot(x, y)
plt.show()