如何使用CSV列标题作为情节标签

发布于 2025-02-13 05:40:49 字数 2315 浏览 1 评论 0原文

我正在尝试使用三个列标题(“ node_pot_gauche_i”,“ node_lisse_center_i”,“ node_pot_droite_i”)作为标签,同时在我的后制作Python脚本中绘制ANSYS APDL分析结果。

我目前要做的是,当我在循环中绘制时,我会手动添加标签:

# Parcours de tous les fichiers csv
            for i in range(len(model[idx]['asb_path'])):

                # Parcours a l'envers des indices pour positionner les graphs
                j = len(model[idx]['asb_path'])-i-1

                # Lecture du fichier csv possédant des headers
                potiso = pd.read_csv(model[idx]['potiso_path'][i], index_col=0)
                asb = pd.read_csv(model[idx]['asb_path'][i], index_col=0)

                # Plot des résultats sur un subplot de la figure créer en haut
                axs[j].plot(potiso, label='NODE_POT_ISO')
                axs[j].plot(asb['NODE_POT_GAUCHE_'+str(i+1)], label='NODE_POT_GAUCHE_'+str(i+1))
                axs[j].plot(asb['NODE_POT_DROITE_'+str(i+1)], label='NODE_POT_DROITE_'+str(i+1))
                axs[j].set_title(os.path.basename(model[idx]['asb_path'][i]))
                axs[j].set_xlabel('time(s)')
                axs[j].set_ylabel('displacement(m)')
                axs[j].legend()
                axs[j].grid(True)

            # Enregistrement au format .png
            plt.savefig(fig_path)

我想要的是自动使用数据框中的列标题,并在绘图时自动使用它们作为标签更改CSV组织,而不会打扰我的Python后过程脚本。

我似乎找不到我的问题的答案,我尝试了不同的事情,例如让它空白:

# Parcours de tous les fichiers csv
            for i in range(len(model[idx]['asb_path'])):

                # Parcours a l'envers des indices pour positionner les graphs
                j = len(model[idx]['asb_path'])-i-1
                # Lecture du fichier csv possédant des headers
                asb = pd.read_csv(model[idx]['asb_path'][i], index_col=0)

                # Plot des résultats sur un subplot de la figure créer en haut
                axs[j].plot(asb)
                axs[j].set_title(os.path.basename(model[idx]['asb_path'][i]))
                axs[j].set_xlabel('time(s)')
                axs[j].set_ylabel('displacement(m)')
                axs[j].legend()
                axs[j].grid(True)

            plt.savefig(fig_path)

但是我遇到了以下错误:

没有发现传奇的唱片公司的艺术家。请注意,标签以下划线开头的艺术家在没有参数的情况下被忽略。

必须有一种方法来提取列的名称,然后将它们作为我的情节标签输入输入。 在此先感谢您的任何帮助!

I'm trying to use the three columns headers ("NODE_POT_GAUCHE_i", "NODE_LISSE_CENTER_i", "NODE_POT_DROITE_i") as labels while plotting results from an Ansys APDL analysis in my post-process python script.

What I currently do, is that when I'm plotting in my loop, I add the label manually :

# Parcours de tous les fichiers csv
            for i in range(len(model[idx]['asb_path'])):

                # Parcours a l'envers des indices pour positionner les graphs
                j = len(model[idx]['asb_path'])-i-1

                # Lecture du fichier csv possédant des headers
                potiso = pd.read_csv(model[idx]['potiso_path'][i], index_col=0)
                asb = pd.read_csv(model[idx]['asb_path'][i], index_col=0)

                # Plot des résultats sur un subplot de la figure créer en haut
                axs[j].plot(potiso, label='NODE_POT_ISO')
                axs[j].plot(asb['NODE_POT_GAUCHE_'+str(i+1)], label='NODE_POT_GAUCHE_'+str(i+1))
                axs[j].plot(asb['NODE_POT_DROITE_'+str(i+1)], label='NODE_POT_DROITE_'+str(i+1))
                axs[j].set_title(os.path.basename(model[idx]['asb_path'][i]))
                axs[j].set_xlabel('time(s)')
                axs[j].set_ylabel('displacement(m)')
                axs[j].legend()
                axs[j].grid(True)

            # Enregistrement au format .png
            plt.savefig(fig_path)

What I would like, is to automatically use the column headers from the data frame and use them as labels automatically while plotting, allowing me to change csv organization without disturbing my python post-process script.

I can't seem to find any answer to my question, I've tried different things such as letting it blank :

# Parcours de tous les fichiers csv
            for i in range(len(model[idx]['asb_path'])):

                # Parcours a l'envers des indices pour positionner les graphs
                j = len(model[idx]['asb_path'])-i-1
                # Lecture du fichier csv possédant des headers
                asb = pd.read_csv(model[idx]['asb_path'][i], index_col=0)

                # Plot des résultats sur un subplot de la figure créer en haut
                axs[j].plot(asb)
                axs[j].set_title(os.path.basename(model[idx]['asb_path'][i]))
                axs[j].set_xlabel('time(s)')
                axs[j].set_ylabel('displacement(m)')
                axs[j].legend()
                axs[j].grid(True)

            plt.savefig(fig_path)

But I'm getting the following error :

No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when legend() is called with no argument.

There must be a way to extract the column's name and then input them as a label entry for my plot.
Thanks in advance for any help !

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

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

发布评论

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

评论(1

自找没趣 2025-02-20 05:40:49

有人在其他地方回答了我,这使我在这里分享答案:

# Parcours de tous les fichiers csv
            for i in range(len(model[idx]['asb_path'])):

                # Parcours a l'envers des indices pour positionner les graphs
                j = len(model[idx]['asb_path'])-i-1
                # Lecture du fichier csv possédant des headers
                asb = pd.read_csv(model[idx]['asb_path'][i], index_col=0)

                # Plot des résultats sur un subplot de la figure créer en haut
                axs[j].plot(asb, label = list(asb.columns))
                axs[j].set_title(os.path.basename(model[idx]['asb_path'][i]))
                axs[j].set_xlabel('time(s)')
                axs[j].set_ylabel('displacement(m)')
                axs[j].legend()
                axs[j].grid(True)

            plt.savefig(fig_path)

重要的部分是:

axs[j].plot(asb, label = list(asb.columns))

或:

axs[j].plot(asb, label = asb.columns)

它基本上使用了数据框架对象的列属性。您还可以循环浏览每一列并自己构建列表。 (在这里提供更多信息: https:// www。 geeksforgeeks.org/how-to-get-column-names-names-in-pandas-dataframe/

i由于我不是在使用asb.columns而无法使用它作为列表,这就是为什么它以前没有工作的原因。


编辑:小心,如果只有一列是绘制:

axs[j].plot(asb, label = list(asb.columns))

将返回['header_name']。

和:

axs[j].plot(asb, label = asb.columns)

将返回索引。

因此,如果只有一列,则应使用:

axs[j].plot(asb, label = asb.columns[0])

Someone answered me elsewhere, which leads me to share the answer here :

# Parcours de tous les fichiers csv
            for i in range(len(model[idx]['asb_path'])):

                # Parcours a l'envers des indices pour positionner les graphs
                j = len(model[idx]['asb_path'])-i-1
                # Lecture du fichier csv possédant des headers
                asb = pd.read_csv(model[idx]['asb_path'][i], index_col=0)

                # Plot des résultats sur un subplot de la figure créer en haut
                axs[j].plot(asb, label = list(asb.columns))
                axs[j].set_title(os.path.basename(model[idx]['asb_path'][i]))
                axs[j].set_xlabel('time(s)')
                axs[j].set_ylabel('displacement(m)')
                axs[j].legend()
                axs[j].grid(True)

            plt.savefig(fig_path)

The important part is :

axs[j].plot(asb, label = list(asb.columns))

or :

axs[j].plot(asb, label = asb.columns)

It basically uses the columns attribute of the data frame object. You could as well loop through every column and build the list by yourself. (more informations here : https://www.geeksforgeeks.org/how-to-get-column-names-in-pandas-dataframe/)

I failed at using asb.columns before cause I wasn't using it as a list, that's why it wasn't working before.


EDIT: Be careful, if there is only one column being plotted :

axs[j].plot(asb, label = list(asb.columns))

will return ['HEADER_NAME'].

and :

axs[j].plot(asb, label = asb.columns)

will return an index.

So if there is only one column you should be using :

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