如何将关键名称作为我的子图的标题?

发布于 2025-02-10 11:12:39 字数 2390 浏览 0 评论 0原文

我设法在文件夹中绘制所有CSV文件,并在一个窗口中绘制图形。但是到目前为止,只有一件小事。这些地块没有标题。当我使用“信标”列来决定应该绘制哪些数据时,我也希望将信标名称作为图的标题。

我的数据帧看起来像这样:

Beacon  Average  Gradient
Date                                
2017-06-10    A18   4.5566   1.11280
2017-06-10    B18   5.6694  -0.41125
2017-06-15    A18   4.5528   1.11350
2017-06-15    B18   5.6663  -0.41105
2017-06-15    C17   3.7307  -0.72965
          ...      ...       ...
2017-08-16   SB01   3.8360  -0.10900
2017-08-16   SB02   3.7130  -0.13750
2017-08-16   SB03   3.5610   0.13300
2017-08-16   SB04   3.9790   0.08100
2017-08-16   SB05   3.7230  -0.25600

我的脚本看起来像这样:

import matplotlib.pyplot as plt
import pandas as pd
import glob
import os
import numpy as np
## chose directory
path =r'C:\soc3\data\sb_measurements'
globbed = glob.glob(path + "/*.csv")


data = [] 

## Select and modify all CSV files in directory
for csv in globbed:
    df = pd.read_csv(csv, header=0, skiprows=(0), delimiter = ',', names = ['Beacon', 'Go', 'Return', 'Average'])
    df['Filename'] = os.path.basename(csv)
    data.append(df)
   
for index in data:
    index['Gradient'] = (np.gradient(index['Average']))

    
df = pd.concat(data, axis=0, ignore_index=True)
df = df.loc[:, df.columns.intersection(['Beacon', 'Average','Filename','Gradient'])]   
df = df.dropna()
df = df.replace('.csv','', regex=True)
df['Filename'] = df['Filename'].replace('SB','20', regex=True)
df['Filename'] = df['Filename'].astype('|S')
df['Filename'] = pd.to_datetime(df['Filename'], format='%Y%m%d')
df.rename(columns = {'Filename':'Date'}, inplace = True)
df.set_index('Date', inplace=True)
counted_sets = df['Beacon'].nunique()
df = df.dropna()


grouped = df.groupby('Beacon')

ncols= 7
nrows = 4

fig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize = (90,30) , constrained_layout = True)

for (key, ax) in zip(grouped.groups.keys(), axes.flatten()):
    grouped.get_group(key).plot(ax=ax)
# =============================================================================
#     plt.savefig("output.jpg") 
# =============================================================================


ax.legend()
plt.show()

print(df)

这些图看起来像这样:

“

我希望有人能帮助我。

I managed to plot all the CSV files in a folder and plot the graphs in one window. But there is just one small thing missing so far. The plots have no title. And as I use the 'Beacon' column to decide which data should be plotted I would also like to have the Beacon name as title of the plot.

my data frame looks like this:

Beacon  Average  Gradient
Date                                
2017-06-10    A18   4.5566   1.11280
2017-06-10    B18   5.6694  -0.41125
2017-06-15    A18   4.5528   1.11350
2017-06-15    B18   5.6663  -0.41105
2017-06-15    C17   3.7307  -0.72965
          ...      ...       ...
2017-08-16   SB01   3.8360  -0.10900
2017-08-16   SB02   3.7130  -0.13750
2017-08-16   SB03   3.5610   0.13300
2017-08-16   SB04   3.9790   0.08100
2017-08-16   SB05   3.7230  -0.25600

My script looks like this:

import matplotlib.pyplot as plt
import pandas as pd
import glob
import os
import numpy as np
## chose directory
path =r'C:\soc3\data\sb_measurements'
globbed = glob.glob(path + "/*.csv")


data = [] 

## Select and modify all CSV files in directory
for csv in globbed:
    df = pd.read_csv(csv, header=0, skiprows=(0), delimiter = ',', names = ['Beacon', 'Go', 'Return', 'Average'])
    df['Filename'] = os.path.basename(csv)
    data.append(df)
   
for index in data:
    index['Gradient'] = (np.gradient(index['Average']))

    
df = pd.concat(data, axis=0, ignore_index=True)
df = df.loc[:, df.columns.intersection(['Beacon', 'Average','Filename','Gradient'])]   
df = df.dropna()
df = df.replace('.csv','', regex=True)
df['Filename'] = df['Filename'].replace('SB','20', regex=True)
df['Filename'] = df['Filename'].astype('|S')
df['Filename'] = pd.to_datetime(df['Filename'], format='%Y%m%d')
df.rename(columns = {'Filename':'Date'}, inplace = True)
df.set_index('Date', inplace=True)
counted_sets = df['Beacon'].nunique()
df = df.dropna()


grouped = df.groupby('Beacon')

ncols= 7
nrows = 4

fig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize = (90,30) , constrained_layout = True)

for (key, ax) in zip(grouped.groups.keys(), axes.flatten()):
    grouped.get_group(key).plot(ax=ax)
# =============================================================================
#     plt.savefig("output.jpg") 
# =============================================================================


ax.legend()
plt.show()

print(df)

And these plots look like this:

Plots

I hope someone can help me out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等数载,海棠开 2025-02-17 11:12:39

我要说的是,提供熊猫图函数。这样:grouped.get_group(key).plot(ax = ax,title = key)

I would say it is enough to provide the key to the title parameter of panda's plot function. Like this: grouped.get_group(key).plot(ax=ax, title=key)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文