根据在 python 中使用plotly方法在y轴刻度中添加货币符号的答案,您可以通过以下方式将货币添加到Python Plotly刻度格式化程序:
fig.update_layout(yaxis_tickprefix = '$', yaxis_tickformat = ',.')
Run Code Online (Sandbox Code Playgroud)
但是,这显示负数而$-100不是-$100。
如何格式化刻度以包含 $ 并尊重负数?
layout(hovermode = "x unified")Plotly 提供了通过添加或显示可移动的水平或垂直“尖峰线”的选项layout(hovermode = "y unified")(请参阅文档)。例如:
library(plotly)
x <- seq(from = 0, to = 2*pi, length = 100)
y <- sin(x)
fig <- plot_ly(x = x, y = y, type = "scatter", mode = "markers")
fig <- fig %>% layout(hovermode = "x unified")
fig
Run Code Online (Sandbox Code Playgroud)
使用跟随光标的垂直线创建此图:
我尝试像这样修改布局:
fig <- fig %>% layout(hovermode = "x unified") %>% layout(hovermode = "y unified")
# or
fig <- fig %>% layout(hovermode = c("x unified", "y unified"))
# …Run Code Online (Sandbox Code Playgroud) 例如,我尝试在 python 中使用plotly 在同一个图表上绘制几条具有自己的 X 值的线。
x1 = [1, 3, 5, 7, 9]
y1 = np.random.random(5)
x2 = [2, 4, 6, 8, 10]
y2 = np.random.random(5)
Run Code Online (Sandbox Code Playgroud)
我想将其绘制在从 1 到 10 的一个 x 轴上
。StackOverFlow 上的所有答案(如下所示)都使用 pandas 数据框描述了解决方案,其中所有行(y1 和 y2)都具有与每个行相对应的相同 x 值数组线。然而,在我的例子中, y1 和 y2 具有不同的(尽管单位相同)对应的 x 值。
我该怎么做才能在情节中制作这样的情节?
使用时间线和plotly.express,我可以获得一个有效的甘特图:
import plotly.express as px
import pandas as pd
df = pd.DataFrame([
dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28'),
dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15'),
dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30')
])
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task")
fig.update_yaxes(autorange="reversed") # otherwise tasks are listed from the bottom up
fig.show()
Run Code Online (Sandbox Code Playgroud)
按照这里的建议,我尝试将其添加到子图中shared_xaxes = True
from plotly.subplots import make_subplots
fig_sub = make_subplots(rows=2, shared_xaxes=True)
fig_sub.append_trace(fig['data'][0], row=1, col=1)
fig_sub.append_trace(fig['data'][0], row=2, col=1)
fig_sub.show()
Run Code Online (Sandbox Code Playgroud)
但它将它视为 graph_objects 并且不显示甘特图。
有人有任何解决方法或建议吗?
我正在尝试使用绘图对象来可视化来自不同 pandas 数据帧的时间戳。成功添加跟踪作为 go.Scatter() 图后,我现在想添加 px.timeline() 图作为跟踪。这一般可以吗?
这是我的代码:
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
df=pd.read_json(file1.json)
df2=pd.read_json(file2.json)
df3=pd.read_json(file3.json)
fig = go.Figure(layout=layout)
fig.add_trace(
go.Scatter(x=df.column1, y=df.column2, name='name1', mode='markers')
)
fig.add_trace(
go.Scatter(x=df2.column1, y=df2.column2, name='name2', mode='markers')
)
fig.add_trace(
go.Scatter(x=df3.column1, y=df3.column2, name='name3', mode='markers')
)
#### add px trace ###
fig.update_traces(marker_size=10)
fig.show()
Run Code Online (Sandbox Code Playgroud)
到目前为止,这运作良好。现在,我想将 px.timeline() 图添加为我的图形的跟踪。我试过:
fig.add_trace(
px.timeline(df,x_start="column1",
x_end="column2", y='column3')
Run Code Online (Sandbox Code Playgroud)
这会引发值错误:
ValueError:
Invalid element(s) received for the 'data' property of [...]
Run Code Online (Sandbox Code Playgroud)
是否有解决方法可以将 px.-Plots 添加到 go.Figure() ?
我有一个 3D 装箱模型,它使用绘图来绘制输出图。我注意到绘制了 600+ 个项目,生成 html 文件需要很长时间,文件大小为 89M,这太疯狂了(我怀疑可能存在一些巨大的重复,或者由 \xe2\x80\x9cadd_trace\ 引起) xe2\x80\x9d 方法来绘制单个项目图)。为什么它会产生这么大的文件?如何将大小控制在可接受的水平(不超过 5M,因为我需要在我的网站中渲染它)。非常感谢您的帮助。
\n\n下面是我的完整代码(请跳过模型代码并从绘图代码中查看)
\nfrom py3dbp import Packer, Bin, Item, Painter\nimport time\nimport plotly.graph_objects as go\nfrom plotly.subplots import make_subplots\nimport plotly\nimport pandas as pd\n\nstart = time.time()\nimport numpy as np\n\n# -----------this part is about calculating the 3D bin packing problem to get x,y,z for each items of a bin/container--------------\n###library reference: https://github.com/jerry800416/3D-bin-packing\n\n# init packing function\npacker = Packer()\n# init bin\n# box = Bin(\'40HC-1\', (1203, 235, 259), 18000.0,0,0)\nbox = Bin(\'40HC-1\', (1202.4, 235, 269.7), …Run Code Online (Sandbox Code Playgroud) 我有两个共享 x 轴的子图,但它仅显示一个子图的 y 值,而不是两者。我希望悬停显示显示两个子图中的 y 值。
但我希望它也显示底部图表中的 y 值,即使我将鼠标悬停在顶部图表上,反之亦然。
这是我的代码:
title = 'Price over time'
err = 'Price'
fig = make_subplots(rows=2, cols=1,
vertical_spacing = 0.05,
shared_xaxes=True,
subplot_titles=(title,""))
# A
fig.add_trace(go.Scatter(x= A_error['CloseDate'],
y = A_error[err],
line_color = 'green',
marker_color = 'green',
mode = 'lines+markers',
showlegend = True,
name = "A",
stackgroup = 'one'),
row = 1,
col = 1,
secondary_y = False)
# B
fig.add_trace(go.Scatter(x= B_error['CloseDate'],
y = B_error[err],
line_color = 'blue',
mode = 'lines+markers',
showlegend = True,
name …Run Code Online (Sandbox Code Playgroud) 我有兴趣绘制一个实值函数f(x,y,z)=a,其中(x,y,z)是球体上的 3D 点,并且a是实数。我计算球体点的笛卡尔坐标如下,但我不知道如何可视化f每个点的值。
import plotly.graph_objects as go
import numpy as np
fig = go.Figure(layout=go.Layout(title=go.layout.Title(text=title), hovermode=False))
# Create mesh grid for spherical coordinates
phi, theta = np.mgrid[0.0:np.pi:100j, 0.0:2.0 * np.pi:100j]
# Get Cartesian mesh grid
x = np.sin(phi) * np.cos(theta)
y = np.sin(phi) * np.sin(theta)
z = np.cos(phi)
# Plot sphere surface
self.fig.add_surface(x=x, y=y, z=z, opacity=0.35)
fig.show()
Run Code Online (Sandbox Code Playgroud)
我会想象/期望/喜欢这样的可视化
f此外,我还以封闭形式计算了的梯度(即,对于每个(x,y,z)我计算 的 3D 维梯度f)。有没有一种方法可以绘制这个矢量场,类似于上图所示?
我这样创建一个 Plotly Figure 实例:
fig = go.Figure()
fig = make_subplots(rows=3, cols=1, shared_xaxes=True, row_width=[0.3, 0.3, 0.4])
Run Code Online (Sandbox Code Playgroud)
假设现在我不知道Figure 实例有多少行和列。我怎样才能获得这些值?例如,我期望这样的事情:
rows = fig.get_rows_num()
cols = fig.get_cols_num()
Run Code Online (Sandbox Code Playgroud)
我很感激任何帮助。
我有以下条形图。我想把它放在边界周围。有谁知道该怎么做?
data = {'Programming Languages': ['Python', 'C#', 'Java', 'Ruby', 'C++', 'C',
'JavaScript', 'PHP', 'TypeScript', 'Haskell', 'Closure', 'Kotlin'],
'Responses': [12,11,10,5,3,2,2,1,1,1,1,1]}
df = pd.DataFrame(data, columns = ['Programming Languages', 'Responses'])
df = df.sort_values(by='Responses', ascending=True)
fig = px.bar(df, x='Responses', y='Programming Languages', color_discrete_sequence=["black"])
fig.update_layout({'plot_bgcolor': 'rgba(0, 0, 0, 0)'},
font=dict(size=15))
fig.write_image("../images/pl2.pdf")
fig.show()
Run Code Online (Sandbox Code Playgroud)
不幸的是,我无法粘贴该图,因为它是 pdf 文件。