使用Pandas从csv文件中读取面积图的输入数据

2024-06-02 05:30:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用pandas读取一个包含两列值的csv文件。我想使用csv文件的两列作为面积图。每一列都应该由一个区域表示。我试过了,但不幸的是,到目前为止我还没有成功。以下是我目前掌握的代码:

import pandas as pd
from matplotlib import pyplot as plt
%matplotlib inline


inputValues = pd.read_csv("C:/Users/wi9632/Desktop/electricalPowerTotal_2.csv") 
first_column = inputValues.iloc[:, 0]
power_values = inputValues
#power_values = first_column

'''
power_values = [[120000, 12000]
,[120000, 116321.1908]
,[120000, 118113.0328]
,[120000, 114600]
,[93000, 107353.9204]
,[93000, 107587.8386]
,[12000, 117104.2044]
,[12000, 118264.4594]
,[12000, 117945.2063]
,[12000, 119603.5571]
,[12000, 114533.1649]
,[12000, 115572.16]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12838.06104]
,[12000, 12083.09783]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 89786.61005]
,[12000, 77881.9384]
,[12000, 92682.10154]
,[12000, 87809.53293]
,[12000, 86967.55995]
,[12000, 66165.492]
,[12000, 70745.2192]
,[14700, 65919.1703]
,[14700, 60105.36791]
,[14700, 71163.80286]
,[14700, 41359.14048]
,[17400, 86868.4165]
,[17400, 17369.06999]
,[22800, 24852.58464]
,[28200, 32362.0731]
,[30900, 26377.3125]
,[39000, 31773.85124]
,[39000, 43314.7621]
,[39000, 48467.08134]
,[39000, 61814.72004]
,[39000, 47418.45997]
,[41700, 58048.10651]
,[47100, 50910.68421]
,[47100, 88949.26413]
,[47100, 46397.97818]
,[55200, 54805.75834]
,[57900, 40667.24158]
,[63300, 72125.98178]
,[66000, 62627.80501]
,[66000, 60919.18474]
,[66000, 59630.61079]
,[74100, 67263.35234]
,[74100, 56496.24679]
,[79500, 69745.10363]
,[82200, 87378.8584]
,[84900, 95942.32954]
,[87600, 12000]
,[87600, 12176.49049]
,[95700, 48300.76603]
,[95700, 83070.30318]
,[95700, 100654.9642]
,[103800, 105354.238]
,[106500, 110412.9039]
,[109200, 105819.7575]
,[106500, 92972.19054]
,[106500, 94560.84184]
,[106500, 120000]
,[111900, 117985.9067]
,[109200, 12000]
,[111900, 12000]
,[114600, 12000]
,[106500, 12000]
,[101100, 12000]
,[98400, 12000]
,[93000, 12000]
,[93000, 13746.58733]
,[93000, 12000]
,[93000, 12000]
,[93000, 12000]
,[93000, 12000]
,[87600, 12000]
,[87600, 12000]
,[84900, 12000]
,[82200, 12000]
,[76800, 12000]
,[71400, 12000]
,[68700, 12000]
,[68700, 12000]
,[68700, 12000]
,[66000, 12000]
,[63300, 12000]
,[60600, 12000]
,[60600, 42243.72896]
,[57900, 54527.61265]
,[52500, 70358.99518]
,[49800, 78872.43535]
,[47100, 94230.28391]
,[49800, 105183.383]
,[52500, 111166.1544]
,[49800, 111937.2309]
,[49800, 115286.2033]
,[49800, 117300]
,[49800, 119554.2029]
,[44400, 119916.9913]
,[47100, 120000]
,[44400, 120000]
,[44400, 120000]
,[44400, 120000]
,[44400, 120000]
,[47100, 120000]
,[49800, 120000]
,[49800, 120000]
,[47100, 120000]
,[47100, 120000]
,[47100, 120000]
,[55200, 120000]
,[57900, 12000]
,[57900, 12000]
,[55200, 12000]
,[55200, 12000]
,[55200, 12000]
,[60600, 12000]
,[55200, 12000]
,[49800, 12000]
,[52500, 12000]
,[55200, 15962.01285]
,[52500, 23219.24871]
,[60600, 33698.20201]
,[60600, 119930.2521]
,[63300, 120000]
,[60600, 119709.8892]
,[63300, 119621.9221]
,[66000, 120000]
,[66000, 117300]
,[71400, 120000]
,[68700, 120000]
,[71400, 119836.1707]
,[60600, 117300]
,[57900, 119537.0779]
,[60600, 120000]
,[63300, 12000]
,[60600, 12000]
,[55200, 12000]
,[57900, 12000]
,[55200, 12000]
,[52500, 18663.70438]
,[47100, 28437.57919]
,[41700, 70171.49386]
,[39000, 100169.5002]
,[39000, 114429.5489]
,[39000, 112166.1558]
,[39000, 120000]
,[36300, 12000]
,[30900, 12000]
,[30900, 12000]
,[28200, 12000]
,[30900, 12000]
,[30900, 12000]
,[28200, 12000]
,[25500, 12835.81588]
,[28200, 35718.64701]
,[33600, 97075.89193]
,[36300, 111813.347]
,[39000, 117276.4379]
,[41700, 12000]
,[44400, 12000]
,[44400, 12000]
,[44400, 12000]
,[47100, 12000]
,[44400, 12000]
,[44400, 12000]
,[47100, 12000]
,[44400, 12000]
,[47100, 12000]
,[47100, 12000]
,[49800, 12000]
,[47100, 12000]
,[57900, 12000]
,[57900, 12000]
,[57900, 12000]
,[63300, 12000]
,[63300, 12000]
,[63300, 12000]
,[63300, 12000]
,[66000, 12000]
,[63300, 12000]
,[68700, 12000]
,[68700, 12000]
,[74100, 12000]
,[79500, 12000]
,[79500, 12000]
,[84900, 12000]
,[84900, 12000]
,[82200, 12000]
,[82200, 12000]
,[82200, 12000]
,[76800, 12000]
,[79500, 12000]
,[82200, 12000]
,[82200, 12000]
,[84900, 12000]
,[87600, 12000]
,[87600, 12000]
,[79500, 12000]
,[71400, 12000]
,[71400, 12000]
,[71400, 12000]
,[71400, 12000]
,[63300, 12000]
,[66000, 12000]
,[63300, 12000]
,[60600, 12000]
,[60600, 12000]
,[63300, 12000]
,[63300, 12000]
,[63300, 12154.35357]
,[66000, 12536.75926]
,[66000, 12903.98697]
,[55200, 13053.31214]
,[55200, 13542.66462]
,[55200, 13650.02032]
,[52500, 14112.16008]
,[41700, 14044.3751]
,[41700, 14061.56448]
,[44400, 50555.92643]
,[41700, 67285.26901]
,[41700, 29504.2061]
,[39000, 23825.30113]
,[33600, 29774.46277]
,[33600, 79602.37682]
,[30900, 41151.24172]
,[30900, 65128.04827]
,[30900, 19498.52188]
,[33600, 30631.10012]
,[33600, 18007.39888]
,[36300, 12000]
,[33600, 114692.7977]
,[30900, 49522.07339]
,[33600, 93953.58247]
,[36300, 74269.44322]
,[36300, 52006.01276]
,[30900, 12000]
,[36300, 12000]
,[36300, 12000]
,[36300, 12000]
,[33600, 12000]
,[33600, 12000]
,[36300, 12000]
,[36300, 85105.38741]
,[36300, 30555.82867]
,[36300, 66040.11056]
,[39000, 45224.85527]
,[36300, 28136.72986]
,[36300, 12186.79167]
,[33600, 12000]
,[33600, 12000]
,[30900, 12000]
,[25500, 12000]
,[25500, 12000]
,[25500, 12000]
,[28200, 76832.66527]
,[28200, 43609.02258]
,[28200, 67022.7672]
,[25500, 73925.53359]
,[25500, 53049.44227]
,[20100, 53800.767]
,[20100, 48240.78759]
,[20100, 38755.82412]
,[20100, 55760.30678]
,[20100, 60444.88024]
,[20100, 59739.8675]
,[20100, 47365.30315]
,[17400, 47365.30315]]
'''


wind_data = pd.DataFrame(power_values, index=range(0, 289), columns=columns)
fig = plt.figure(linewidth=1, figsize=(7, 5))
ax = wind_data.plot.area(ax=plt.gca(), color="green, red", stacked=False,  )
ax.set_facecolor("white")
ax.set_xlabel("Time of day", fontsize = 14, labelpad=8)
ax.set_ylabel("Power in kW", fontsize = 14,labelpad=8)
ax.set_xlim(0, 288)
ax.set_ylim(0, 300000)



plt.xticks(wind_data.index, labels=[f'{h:02d}:00' for h in wind_data.index], rotation=90)
plt.grid(axis='y', alpha=.4)
plt.tight_layout()
xticks = ['00:00', '01:00', '02:00' , '03:00', '04:00' , '05:00' , '06:00' , '07:00' , '08:00' , 
          '09:00' , '10:00' , '11:00' , '12:00' , '13:00' , '14:00' , '15:00' , '16:00' 
          , '17:00', '18:00', '19:00' , '20:00' , '21:00', '22:00' , '23:00' , '24:00'  ]
xvals = [0, 1*12, 2*12, 3*12, 4*12, 5*12, 6*12, 7*12, 8*12, 9*12, 10*12, 11*12, 12*12, 13*12, 14*12, 15*12, 16*12
        , 17*12, 18*12, 19*12, 20*12, 21*12, 22*12, 23*12, 24*12] 
ax.set(xticks=xvals, xticklabels=xticks)
ax.tick_params(axis='both', which='major', labelsize=14)
ax.legend(loc='center left', bbox_to_anchor=(0.03, 1.15), fontsize = 14, ncol=3)
plt.savefig('Wind_Assignment_NoScore.png', edgecolor='black', dpi=300, bbox_inches='tight')
plt.show()

在文件“electricalPowerTotal_2.csv”中,您可以在power_值数组中看到两列值。我用“power\u values=inputValues”和 “功率值=第一列”,但仅显示空图表。有人能帮我吗

以下是文件“electricalPowerTotal_2.csv”的内容

Conventional control;Optimal control
120;12
120;116.3
120;118.1
120;114.6
93;107.4
93;107.6
12;117.1
12;118.3
12;117.9
12;119.6
12;114.5
12;115.6
12;12
12;12
12;12
12;12
12;12
12;12
12;12
12;12
12;12
12;12
12;12
12;12
12;12.8
12;12.1
12;12
12;12
12;12
12;12
12;12
12;12
12;12
12;12
12;12
12;12
12;89.8
12;77.9
12;92.7
12;87.8
12;87
12;66.2
12;70.7
14.7;65.9
14.7;60.1
14.7;71.2
14.7;41.4
17.4;86.9
17.4;17.4
22.8;24.9
28.2;32.4
30.9;26.4
39;31.8
39;43.3
39;48.5
39;61.8
39;47.4
41.7;58
47.1;50.9
47.1;88.9
47.1;46.4
55.2;54.8
57.9;40.7
63.3;72.1
66;62.6
66;60.9
66;59.6
74.1;67.3
74.1;56.5
79.5;69.7
82.2;87.4
84.9;95.9
87.6;12
87.6;12.2
95.7;48.3
95.7;83.1
95.7;100.7
103.8;105.4
106.5;110.4
109.2;105.8
106.5;93
106.5;94.6
106.5;120
111.9;118
109.2;12
111.9;12
114.6;12
106.5;12
101.1;12
98.4;12
93;12
93;13.7
93;12
93;12
93;12
93;12
87.6;12
87.6;12
84.9;12
82.2;12
76.8;12
71.4;12
68.7;12
68.7;12
68.7;12
66;12
63.3;12
60.6;12
60.6;42.2
57.9;54.5
52.5;70.4
49.8;78.9
47.1;94.2
49.8;105.2
52.5;111.2
49.8;111.9
49.8;115.3
49.8;117.3
49.8;119.6
44.4;119.9
47.1;120
44.4;120
44.4;120
44.4;120
44.4;120
47.1;120
49.8;120
49.8;120
47.1;120
47.1;120
47.1;120
55.2;120
57.9;12
57.9;12
55.2;12
55.2;12
55.2;12
60.6;12
55.2;12
49.8;12
52.5;12
55.2;16
52.5;23.2
60.6;33.7
60.6;119.9
63.3;120
60.6;119.7
63.3;119.6
66;120
66;117.3
71.4;120
68.7;120
71.4;119.8
60.6;117.3
57.9;119.5
60.6;120
63.3;12
60.6;12
55.2;12
57.9;12
55.2;12
52.5;18.7
47.1;28.4
41.7;70.2
39;100.2
39;114.4
39;112.2
39;120
36.3;12
30.9;12
30.9;12
28.2;12
30.9;12
30.9;12
28.2;12
25.5;12.8
28.2;35.7
33.6;97.1
36.3;111.8
39;117.3
41.7;12
44.4;12
44.4;12
44.4;12
47.1;12
44.4;12
44.4;12
47.1;12
44.4;12
47.1;12
47.1;12
49.8;12
47.1;12
57.9;12
57.9;12
57.9;12
63.3;12
63.3;12
63.3;12
63.3;12
66;12
63.3;12
68.7;12
68.7;12
74.1;12
79.5;12
79.5;12
84.9;12
84.9;12
82.2;12
82.2;12
82.2;12
76.8;12
79.5;12
82.2;12
82.2;12
84.9;12
87.6;12
87.6;12
79.5;12
71.4;12
71.4;12
71.4;12
71.4;12
63.3;12
66;12
63.3;12
60.6;12
60.6;12
63.3;12
63.3;12
63.3;12.2
66;12.5
66;12.9
55.2;13.1
55.2;13.5
55.2;13.7
52.5;14.1
41.7;14
41.7;14.1
44.4;50.6
41.7;67.3
41.7;29.5
39;23.8
33.6;29.8
33.6;79.6
30.9;41.2
30.9;65.1
30.9;19.5
33.6;30.6
33.6;18
36.3;12
33.6;114.7
30.9;49.5
33.6;94
36.3;74.3
36.3;52
30.9;12
36.3;12
36.3;12
36.3;12
33.6;12
33.6;12
36.3;12
36.3;85.1
36.3;30.6
36.3;66
39;45.2
36.3;28.1
36.3;12.2
33.6;12
33.6;12
30.9;12
25.5;12
25.5;12
25.5;12
28.2;76.8
28.2;43.6
28.2;67
25.5;73.9
25.5;53
20.1;53.8
20.1;48.2
20.1;38.8
20.1;55.8
20.1;60.4
20.1;59.7
20.1;47.4
17.4;47.4

Tags: 文件csvdataindexpltaxpdvalues