如何使用Python在Latex中显示Plotly散点图的轴刻度? --- [Python、Plotly、Latex 表示]
我的问题简而言之可以从图中看出。黄色框中显示的数字 - 如何将它们转换为 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
我还尝试过:
- 尝试访问原始垂直轴编号,然后使用“ticktext”。然而,不幸的是我无法获得原始的垂直轴刻度 -->参见: Plotly Python 中的tickformat
- Ploty 是在 d3 上构建的,如果我们应用“tickformat”,那么格式化能力d3 的被调用。示例如下:
不过,到目前为止我还不知道一个解决方案。我需要 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?
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"$\betaquot;
tick_Coll["yaxis3"]["title"] = r"$\text{z}quot;
# apply changes
fig.update_layout(tick_Coll)
return fig
What I also tried:
- 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 - Ploty is build on d3 and if we apply "tickformat" then the formatting abilities of d3 are invoked. Examples for that are given in:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后我找到了解决方案。我要感谢 @RenaudLN。 Plotly Question中的帖子提醒我了解使用前缀和后缀的可能性。以下代码解决了我的问题
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