Python – 使用不同的x标签绘制多列数据随时间戳的变化

huangapple go评论107阅读模式
英文:

Python - Plot multiple columns against time stamp with different xlabels

问题

I'll only provide a translation for the code-related parts. Here is the code you provided:

  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. df = pd.read_csv('sensor_data_1.csv', header=None)
  4. dfe = df.drop_duplicates()
  5. sid_time = dfe[0].value_counts().index.to_list()
  6. sid_sen = dfe[2].value_counts().index.to_list()
  7. sid_par = dfe[3].value_counts().index.to_list()
  8. sensor_gdata = {}
  9. for i, s in enumerate(sid_time):
  10. sensor_gdata[i] = dfe.groupby([0]).get_group(s).drop duplicates()
  11. df_merged = pd.concat([sensor_gdata[n] for n in sensor_gdata], sort=False)
  12. dfplot = df_merged.pivot(columns=2, values=[0, 3])
  13. ax = dfplot.plot(kind='bar', stacked=True)
  14. ax.set_xticklabels(df_merged[0], rotation=45)
  15. plt.show()

To integrate both sensor values in one timestamp, you can modify your code as follows:

  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. df = pd.read_csv('sensor_data_1.csv', header=None)
  4. dfe = df.drop_duplicates()
  5. # Group by timestamp and sensor ID
  6. grouped = dfe.groupby([0, 2])
  7. # Pivot the data to create separate columns for Temp and Humidity
  8. df_pivot = grouped[3].unstack().reset_index()
  9. # Rename the columns for clarity
  10. df_pivot.columns = ['Timestamp', 'Temp', 'Hum']
  11. # Plot the data
  12. df_pivot.plot(x='Timestamp', kind='bar', stacked=True, title="Sensor Data")
  13. plt.xticks(rotation=45)
  14. plt.show()

This modified code should help you create a plot with both Temp and Humidity values for each timestamp, grouped by sensor ID.

英文:

My dataframe is

  1. 29-03-2023 18:08,2,Con,0
  2. 29-03-2023 18:08,2,Temp,27.2
  3. 29-03-2023 18:08,2,hum,44.4
  4. 29-03-2023 18:08,2,Con,0
  5. 29-03-2023 18:08,2,Temp,27.2
  6. 29-03-2023 18:08,2,hum,44.3
  7. 29-03-2023 18:08,2,Con,0
  8. 29-03-2023 18:08,2,Temp,27.2
  9. 29-03-2023 18:08,2,hum,44.4
  10. 29-03-2023 18:09,2,Con,0
  11. 29-03-2023 18:09,2,Temp,27.2
  12. 29-03-2023 18:09,2,hum,44.4
  13. 29-03-2023 18:09,3,Con,0
  14. 29-03-2023 18:09,3,Temp,27.2
  15. 29-03-2023 18:09,3,hum,44.4

I want to plot a graph for each sensor's con, temp and hum values for each id[2,3] for each time stamp.

I am expecting 6 values (3 of sensor 2 + 3 of sensor 3) for each time stamp in different colors.
My code does not produce the required results:

  1. import pandas as pd
  2. import matplotlib.pyplot as mpyplt
  3. df = pd.read_csv('sensor_data_1.csv', header=None)
  4. dfe = df.drop_duplicates()
  5. sid_time = dfe[0].value_counts().index.to_list()
  6. sid_sen = dfe[2].value_counts().index.to_list()
  7. sid_par = dfe[3].value_counts().index.to_list()
  8. sid_time,sid_sen, sid_par
  9. sensor_gdata = {}
  10. for i, s in enumerate(sid_time):
  11. sensor_gdata[i] = dfe.groupby([0]).get_group(s).drop_duplicates()
  12. print(sensor_gdata[i])
  13. df_merged = pd.concat([sensor_gdata[n] for n in sensor_gdata], sort=False)
  14. dfplot = df_merged.pivot(columns=2, values=[0,3])
  15. ax = dfplot.plot(kind='bar',stacked=True)
  16. ax.set_xticklabels(df_merged[0], rotation=45)
  17. mpyplt.show()

How can I integrate both sensor values in one timestamp?

Python – 使用不同的x标签绘制多列数据随时间戳的变化

Python – 使用不同的x标签绘制多列数据随时间戳的变化

答案1

得分: 0

基于数据框架的操作如下:

  1. 根据时间戳ID和项目删除重复的行并创建一个透视表以便为每对ID/测量类型设置不同的颜色
  2. df.drop_duplicates(["timestamp","id","item"],inplace = True)
  3. df = df.pivot(index = 'timestamp',columns=['id','item']).droplevel(0,axis =1)

新的数据框架如下:

  1. id 2 3
  2. item Con Temp hum Con Temp hum
  3. timestamp
  4. 29-03-2023 18:08 0.0 27.2 44.4 NaN NaN NaN
  5. 29-03-2023 18:09 0.0 27.2 44.4 0.0 27.2 44.4

绘制这个数据框架:

  1. ax = df.plot.bar(width = 1.5)
  2. ax.set_xticklabels(ax.get_xticklabels(), rotation=45, ha = 'right')
  3. ax.set_xlabel('')

条形图中的间隙/空格是由于val = 0或NaN引起的。

英文:

Based on the dataframe

  1. timestamp id item val
  2. 0 29-03-2023 18:08 2 Con 0.0
  3. 1 29-03-2023 18:08 2 Temp 27.2
  4. 2 29-03-2023 18:08 2 hum 44.4
  5. 3 29-03-2023 18:08 2 Con 0.0
  6. 4 29-03-2023 18:08 2 Temp 27.2
  7. 5 29-03-2023 18:08 2 hum 44.3
  8. 6 29-03-2023 18:08 2 Con 0.0
  9. 7 29-03-2023 18:08 2 Temp 27.2
  10. 8 29-03-2023 18:08 2 hum 44.4
  11. 9 29-03-2023 18:09 2 Con 0.0
  12. 10 29-03-2023 18:09 2 Temp 27.2
  13. 11 29-03-2023 18:09 2 hum 44.4
  14. 12 29-03-2023 18:09 3 Con 0.0
  15. 13 29-03-2023 18:09 3 Temp 27.2
  16. 14 29-03-2023 18:09 3 hum 44.4

I drop the duplicated rows that have the same timestamp/id/item, and make a pivot table in order to set different colours for each pair of id/type of measurement

  1. df.drop_duplicates(["timestamp","id","item"],inplace = True)
  2. df = df.pivot(index = 'timestamp',columns=['id','item']).droplevel(0,axis =1)

and the new dataframe becomes

  1. id 2 3
  2. item Con Temp hum Con Temp hum
  3. timestamp
  4. 29-03-2023 18:08 0.0 27.2 44.4 NaN NaN NaN
  5. 29-03-2023 18:09 0.0 27.2 44.4 0.0 27.2 44.4

Plot this dataframe

  1. ax = df.plot.bar(width = 1.5)
  2. ax.set_xticklabels(ax.get_xticklabels(), rotation=45, ha = 'right')
  3. ax.set_xlabel('')

The gaps/spaces between bars result from val = 0 or NaN.
Python – 使用不同的x标签绘制多列数据随时间戳的变化

huangapple
  • 本文由 发表于 2023年3月31日 18:18:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75897383.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定