如何使用Python在Latex中显示Plotly散点图的轴刻度? --- [Python、Plotly、Latex 表示]

发布于 2025-01-08 23:48:50 字数 2522 浏览 4 评论 0原文

我的问题简而言之可以从图中看出。黄色框中显示的数字 - 如何将它们转换为 Latex 表示形式?

长篇故事或描述:

你可以看到,x轴(水平轴)已经在Latex中显示了。此外,作为 x 标题的 β、垂直轴标题 (x,y,z) 和标签都是使用 Latex 显示的。

乳胶标签: 只需看一下以下代码中的参数 name

fig.append_trace(
    go.Scatter(
                x= mPVs,
                y= dat[:,2],
                name =r"$\text{z}$",
                    ),
    
        row=3, col=1)

Axes Titles: 顺便说一句,我不喜欢 pep8 风格 - 我更喜欢 start_Next_And_Next -风格。我认为这种类型的编码对眼睛来说不那么费力。

def ticks_Modi(fig, ticks):
    
    # get the ticks to Latex representation
    ticks_Latex = [rf"${i:.3f}$" for i in ticks]
    
    # generate empty dicts
    tick_Coll = {
    "yaxis1": {},
    "yaxis2": {},
    "yaxis3": {},
    }
    
    # horizontal axis -> get as latex
    tick_Sett= {
    "tickmode": 'array',
    "tickvals": np.round(ticks, decimals = 3),
    "ticktext":  ticks_Latex
    }
    
    # vertical axes --> Latex, but HOW?
    tick_Sett_Y = {
    # "tickmode": 'array',
    # "tickvals": np.round(ticks, decimals = 3),
        
    # Plotly uses D3 -> also d3 formats doesent work here
    "tickformat": r"$"+".0%"+r"$"
    }
    
    
    tick_Coll["xaxis1"] = tick_Sett
    tick_Coll["yaxis1"] = tick_Sett_Y
    
    # get the axis title to be in latex
    tick_Coll["yaxis1"]["title"] = r"$\text{x}$"
    
    tick_Coll["xaxis2"] = tick_Sett
    tick_Coll["yaxis2"]["title"] = r"$\text{y}$"
    
    tick_Coll["xaxis3"] = tick_Sett
    tick_Coll["xaxis3"]["title"]= r"$\beta$"
    tick_Coll["yaxis3"]["title"] = r"$\text{z}$"
         
    # apply changes
    fig.update_layout(tick_Coll)
    
    return fig

我还尝试过:

  1. 尝试访问原始垂直轴编号,然后使用“ticktext”。然而,不幸的是我无法获得原始的垂直轴刻度 -->参见: Plotly Python 中的tickformat
  2. Ploty 是在 d3 上构建的,如果我们应用“tickformat”,那么格式化能力d3 的被调用。示例如下:
  1. Plotly 文档
  2. d3.format 链接
  3. d3 格式化 GitHub

不过,到目前为止我还不知道一个解决方案。我需要 Latex 的原因是我想将这些图嵌入到我的论文中。

感谢所有愿意花时间解决我的问题的人。

My question in short can be seen in the figure. The numbers which are shown in the yellow box - how do I get them into Latex representation? Question can be seen here

Long story or description:

You can see that, the x-axis (horizontal axis) is already displayed in Latex. Also, the \beta as the x-title, the vertical axis titles (x,y,z) and the labels are displayed using Latex.

Labels in Latex:
Just have a look on the argument name in the following code

fig.append_trace(
    go.Scatter(
                x= mPVs,
                y= dat[:,2],
                name =r"$\text{z}
quot;,
                    ),
    
        row=3, col=1)

Axes Titles: By the way, I do not like the pep8 style - I prefer start_Next_And_Next -style. I consider this type of coding to be less strenuous on the eyes.

def ticks_Modi(fig, ticks):
    
    # get the ticks to Latex representation
    ticks_Latex = [rf"${i:.3f}
quot; for i in ticks]
    
    # generate empty dicts
    tick_Coll = {
    "yaxis1": {},
    "yaxis2": {},
    "yaxis3": {},
    }
    
    # horizontal axis -> get as latex
    tick_Sett= {
    "tickmode": 'array',
    "tickvals": np.round(ticks, decimals = 3),
    "ticktext":  ticks_Latex
    }
    
    # vertical axes --> Latex, but HOW?
    tick_Sett_Y = {
    # "tickmode": 'array',
    # "tickvals": np.round(ticks, decimals = 3),
        
    # Plotly uses D3 -> also d3 formats doesent work here
    "tickformat": r"
quot;+".0%"+r"
quot;
    }
    
    
    tick_Coll["xaxis1"] = tick_Sett
    tick_Coll["yaxis1"] = tick_Sett_Y
    
    # get the axis title to be in latex
    tick_Coll["yaxis1"]["title"] = r"$\text{x}
quot;
    
    tick_Coll["xaxis2"] = tick_Sett
    tick_Coll["yaxis2"]["title"] = r"$\text{y}
quot;
    
    tick_Coll["xaxis3"] = tick_Sett
    tick_Coll["xaxis3"]["title"]= r"$\beta
quot;
    tick_Coll["yaxis3"]["title"] = r"$\text{z}
quot;
         
    # apply changes
    fig.update_layout(tick_Coll)
    
    return fig

What I also tried:

  1. Try to access the original vertical axes numbers and then use "ticktext". However, unfortunately I was not able to get the original vertical axes ticks --> see also:
    tickformat in Plotly Python
  2. Ploty is build on d3 and if we apply "tickformat" then the formatting abilities of d3 are invoked. Examples for that are given in:
  1. Plotly's Documentation
  2. d3.format link
  3. d3 formatting GitHub

Still, up to now I do not know a solution. The reason, why I need Latex is that I want to embed these plots in my thesis.

Thanks to everyone, who is willing to spend his time on my issue.

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

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

发布评论

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

评论(1

番薯 2025-01-15 23:48:50

最后我找到了解决方案。我要感谢 @RenaudLNPlotly Question中的帖子提醒我了解使用前缀和后缀的可能性。以下代码解决了我的问题

tick_Sett_Y = {
            "tickprefix": r"$" ,
            "ticksuffix": r"$",
        }

tick_Coll["yaxis1"] = tick_Sett_Y.copy()

# apply changes
fig.update_layout(tick_Coll)

finally I found a solution. I want to thank @RenaudLN. The post in Plotly Question reminded me of the possibility to use prefixes and suffixes. The following code solved my issue

tick_Sett_Y = {
            "tickprefix": r"
quot; ,
            "ticksuffix": r"
quot;,
        }

tick_Coll["yaxis1"] = tick_Sett_Y.copy()

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