英文:
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:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('sensor_data_1.csv', header=None)
dfe = df.drop_duplicates()
sid_time = dfe[0].value_counts().index.to_list()
sid_sen = dfe[2].value_counts().index.to_list()
sid_par = dfe[3].value_counts().index.to_list()
sensor_gdata = {}
for i, s in enumerate(sid_time):
sensor_gdata[i] = dfe.groupby([0]).get_group(s).drop duplicates()
df_merged = pd.concat([sensor_gdata[n] for n in sensor_gdata], sort=False)
dfplot = df_merged.pivot(columns=2, values=[0, 3])
ax = dfplot.plot(kind='bar', stacked=True)
ax.set_xticklabels(df_merged[0], rotation=45)
plt.show()
To integrate both sensor values in one timestamp, you can modify your code as follows:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('sensor_data_1.csv', header=None)
dfe = df.drop_duplicates()
# Group by timestamp and sensor ID
grouped = dfe.groupby([0, 2])
# Pivot the data to create separate columns for Temp and Humidity
df_pivot = grouped[3].unstack().reset_index()
# Rename the columns for clarity
df_pivot.columns = ['Timestamp', 'Temp', 'Hum']
# Plot the data
df_pivot.plot(x='Timestamp', kind='bar', stacked=True, title="Sensor Data")
plt.xticks(rotation=45)
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
29-03-2023 18:08,2,Con,0
29-03-2023 18:08,2,Temp,27.2
29-03-2023 18:08,2,hum,44.4
29-03-2023 18:08,2,Con,0
29-03-2023 18:08,2,Temp,27.2
29-03-2023 18:08,2,hum,44.3
29-03-2023 18:08,2,Con,0
29-03-2023 18:08,2,Temp,27.2
29-03-2023 18:08,2,hum,44.4
29-03-2023 18:09,2,Con,0
29-03-2023 18:09,2,Temp,27.2
29-03-2023 18:09,2,hum,44.4
29-03-2023 18:09,3,Con,0
29-03-2023 18:09,3,Temp,27.2
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:
import pandas as pd
import matplotlib.pyplot as mpyplt
df = pd.read_csv('sensor_data_1.csv', header=None)
dfe = df.drop_duplicates()
sid_time = dfe[0].value_counts().index.to_list()
sid_sen = dfe[2].value_counts().index.to_list()
sid_par = dfe[3].value_counts().index.to_list()
sid_time,sid_sen, sid_par
sensor_gdata = {}
for i, s in enumerate(sid_time):
sensor_gdata[i] = dfe.groupby([0]).get_group(s).drop_duplicates()
print(sensor_gdata[i])
df_merged = pd.concat([sensor_gdata[n] for n in sensor_gdata], sort=False)
dfplot = df_merged.pivot(columns=2, values=[0,3])
ax = dfplot.plot(kind='bar',stacked=True)
ax.set_xticklabels(df_merged[0], rotation=45)
mpyplt.show()
How can I integrate both sensor values in one timestamp?
答案1
得分: 0
基于数据框架的操作如下:
根据时间戳、ID和项目删除重复的行,并创建一个透视表,以便为每对ID/测量类型设置不同的颜色。
df.drop_duplicates(["timestamp","id","item"],inplace = True)
df = df.pivot(index = 'timestamp',columns=['id','item']).droplevel(0,axis =1)
新的数据框架如下:
id 2 3
item Con Temp hum Con Temp hum
timestamp
29-03-2023 18:08 0.0 27.2 44.4 NaN NaN NaN
29-03-2023 18:09 0.0 27.2 44.4 0.0 27.2 44.4
绘制这个数据框架:
ax = df.plot.bar(width = 1.5)
ax.set_xticklabels(ax.get_xticklabels(), rotation=45, ha = 'right')
ax.set_xlabel('')
条形图中的间隙/空格是由于val = 0或NaN引起的。
英文:
Based on the dataframe
timestamp id item val
0 29-03-2023 18:08 2 Con 0.0
1 29-03-2023 18:08 2 Temp 27.2
2 29-03-2023 18:08 2 hum 44.4
3 29-03-2023 18:08 2 Con 0.0
4 29-03-2023 18:08 2 Temp 27.2
5 29-03-2023 18:08 2 hum 44.3
6 29-03-2023 18:08 2 Con 0.0
7 29-03-2023 18:08 2 Temp 27.2
8 29-03-2023 18:08 2 hum 44.4
9 29-03-2023 18:09 2 Con 0.0
10 29-03-2023 18:09 2 Temp 27.2
11 29-03-2023 18:09 2 hum 44.4
12 29-03-2023 18:09 3 Con 0.0
13 29-03-2023 18:09 3 Temp 27.2
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
df.drop_duplicates(["timestamp","id","item"],inplace = True)
df = df.pivot(index = 'timestamp',columns=['id','item']).droplevel(0,axis =1)
and the new dataframe becomes
id 2 3
item Con Temp hum Con Temp hum
timestamp
29-03-2023 18:08 0.0 27.2 44.4 NaN NaN NaN
29-03-2023 18:09 0.0 27.2 44.4 0.0 27.2 44.4
Plot this dataframe
ax = df.plot.bar(width = 1.5)
ax.set_xticklabels(ax.get_xticklabels(), rotation=45, ha = 'right')
ax.set_xlabel('')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论