为什么我无法使用px的图中包含绘图中的子图的绘图。

发布于 2025-02-06 12:45:55 字数 2458 浏览 1 评论 0原文

我一直在尝试使用将多个数字结合在一起的绘图来制作一个数字。为了做到这一点,我一直在尝试使用make_subplots函数,但是我发现很难以适当的格式添加图块。我目前可以制作单数图(如下所示):

但是,每当我尝试使用make_subplots结合这些单数图时,我都会以此为目的:

此图具有完全错误的子图,因为我需要四个子图中的每个数据都包含与四种方法有关的数据(a,b,c和d)。换句话说,我希望有四个子图看起来像我上面的奇异情节示例。

我已经以以下方式设置了代码:

for sequence in sequences:
    #process for making sequence profile is done here
    sequence_df = pd.DataFrame(sequence_profile)
    row_number=1
    grand_figure = make_subplots(rows=4, cols=1)
    #there are four groups per sequence, so the grand figure should have four subplots in total
    for group in sequence_df["group"].unique():
        figure_df_group = sequence_df[(sequence_df["group"]==group)] 
        figure_df_group.sort_values("sample", ascending=True, inplace=True)
        figure = px.line(figure_df_group, x = figure_df_group["sample"], y = figure_df_group["intensity"], color= figure_df_group["method"])
        figure.update_xaxes(title= "sample")
        figure.update_traces(mode='markers+lines')
        #note: the next line fails, since data must be extracted from the figure, hence why it is commented out 
        #grand_figure.append_trace(figure, row = row_number, col=1)
        figure.update_layout(title_text="{} Profile Plot".format(sequence))
        grand_figure.append_trace(figure.data[0], row = row_number, col=1)
        row_number+=1
        figure.write_image(os.path.join(output_directory+"{}_profile_plot_subplots_in_{}.jpg".format(sequence, group)))     
    grand_figure.write_image(os.path.join(output_directory+"grand_figure_{}_profile_plot_subplots.jpg".format(sequence)))

我尝试了以下说明(例如,在这里: valueError:无效的元素(s)为'数据'属性),但我无法像子图一样添加我的数字。起初,我似乎需要在plotly中使用图对象(GO)模块( https:// plotly。 com/python/subplots/),但我真的很想保留我当前奇异情节的格式/设计。我只希望这些情节以四人一组的形式结合在一起。但是,当我尝试像当前一样添加子图时,我需要使用图的数据属性,这会导致我的散点图的设计完全混乱。我如何改善这个问题的任何帮助都将是很棒的。

I have been trying to make a figure using plotly that combines multiple figures together. In order to do this, I have been trying to use the make_subplots function, but I have found it very difficult to have the plots added in such a way that they are properly formatted. I can currently make singular plots (as seen directly below):
single plot example

However, whenever I try to combine these singular plots using make_subplots, I end up with this:
multiple plot example

This figure has the subplots set up completely wrong, since I need each of the four subplots to contain data pertaining to the four methods (A, B, C, and D). In other words, I would like to have four subplots that look like my singular plot example above.

I have set up the code in the following way:

for sequence in sequences:
    #process for making sequence profile is done here
    sequence_df = pd.DataFrame(sequence_profile)
    row_number=1
    grand_figure = make_subplots(rows=4, cols=1)
    #there are four groups per sequence, so the grand figure should have four subplots in total
    for group in sequence_df["group"].unique():
        figure_df_group = sequence_df[(sequence_df["group"]==group)] 
        figure_df_group.sort_values("sample", ascending=True, inplace=True)
        figure = px.line(figure_df_group, x = figure_df_group["sample"], y = figure_df_group["intensity"], color= figure_df_group["method"])
        figure.update_xaxes(title= "sample")
        figure.update_traces(mode='markers+lines')
        #note: the next line fails, since data must be extracted from the figure, hence why it is commented out 
        #grand_figure.append_trace(figure, row = row_number, col=1)
        figure.update_layout(title_text="{} Profile Plot".format(sequence))
        grand_figure.append_trace(figure.data[0], row = row_number, col=1)
        row_number+=1
        figure.write_image(os.path.join(output_directory+"{}_profile_plot_subplots_in_{}.jpg".format(sequence, group)))     
    grand_figure.write_image(os.path.join(output_directory+"grand_figure_{}_profile_plot_subplots.jpg".format(sequence)))

I have tried following directions (like for example, here: ValueError: Invalid element(s) received for the 'data' property) but I was unable to get my figures added as is as subplots. At first it seemed like I needed to use the graph object (go) module in plotly (https://plotly.com/python/subplots/), but I would really like to keep the formatting/design of my current singular plot. I just want the plots to be conglomerated in groups of four. However, when I try to add the subplots like I currently do, I need to use the data property of the figure, which causes the design of my scatter plot to be completely messed up. Any help for how I can ameliorate this problem would be great.

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

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

发布评论

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

评论(1

面如桃花 2025-02-13 12:45:56

好的,所以我在这里找到了一个解决方案。而不是使用make_subplots函数,而只是将所有数字导出到.html文件(绘图将多个图保存到单个html ),然后将其转换为图像( html使用python )。这并不是我本来可以采用的方法,但确实有效。

Update

我发现Plotly Express提供了另一个解决方案,因为PX.Line对象具有facet的参数,该参数允许一个人在其图中设置多个子图。我的代码是这样设置的,并且与上面的代码不同,因为数据帧不需要根据其组在for loop中迭代:

sequence_df = pd.DataFrame(sequence_profile)        
figure = px.line(sequence_df, x = sequence_df["sample"], y = sequence_df["intensity"], color= sequence_df["method"], facet_col= sequence_df["group"])

尽管它仍然需要更格式化,但我的图现在看起来像这样,这就是出于我的目的而工作得多:

”

Ok, so I found a solution here. Rather than using the make_subplots function, I just instead exported all the figures onto an .html file (Plotly saving multiple plots into a single html) and then converted it into an image (HTML to IMAGE using Python). This isn't exactly the approach I would have preferred to have, but it does work.

UPDATE

I have found that plotly express offers another solution, as the px.line object has the parameter of facet that allows one to set up multiple subplots within their plot. My code is set up like this, and is different from the code above in that the dataframe does not need to be iterated in a for loop based on its groups:

sequence_df = pd.DataFrame(sequence_profile)        
figure = px.line(sequence_df, x = sequence_df["sample"], y = sequence_df["intensity"], color= sequence_df["method"], facet_col= sequence_df["group"])

Although it still needs more formatting, my plot now looks like this, which is works much better for my purposes:

faceted plot example

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