我从绘图文档中复制了一个简单的代码,但没有看到输出。输出空间中有一个大空白而不是绘图。
import plotly.graph_objects as go
fig = go.Figure( go.Scatter(x=[1,2,3], y=[1,3,2] ) )
fig.show()
Run Code Online (Sandbox Code Playgroud)
情节版本是4.4.1。
然而,这个和所有其他绘图在我的本地笔记本中运行良好。有任何想法吗?
我正在使用plotlyexpress时间轴函数绘制甘特图。我在日期字段的悬停文本呈现方面遇到问题。
plotly version: 4.5.4
Run Code Online (Sandbox Code Playgroud)
此处给出的原始示例https://plotly.com/python/gantt/,“完成”字段以正确的格式呈现为%Y-%m-%d。
plotly version: 4.5.4
Run Code Online (Sandbox Code Playgroud)
我尝试使用以下代码自定义悬停文本字段。
import plotly.express as px
import pandas as pd
df = pd.DataFrame([
dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28', Resource="Alex")
])
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task", color="Resource")
fig.update_yaxes(autorange="reversed")
fig.show()
Run Code Online (Sandbox Code Playgroud)
现在,显示的FinishStart字段是和的差值(Finish以毫秒为单位)。在上面的示例中,“完成”值显示为5011200000。
Finish我需要在悬停文本中显示原始值。在这种情况下2009-02-28。
我只能通过在数据框中创建 Finish 列的副本并将其用于悬停文本来解决此问题。
有没有办法在不重复的情况下获得正确的列渲染?
当将 subplot_title 添加到我的子图中时,我的标题与我的轴重叠。我可以像 matplotlib 那样改变子图标题的位置吗ax.set_title('title', y=1.5)?
到目前为止,这是我的代码:
from plotly.subplots import make_subplots
categories = ['key', 'acousticness', 'danceability', 'energy', 'loudness',
'speechiness', 'tempo','key']
fig = make_subplots(rows=1, cols=2, specs=[[{"type":"polar"}, {"type":"polar"}]],
subplot_titles=('Clustering Into 8 Playlists', 'Clustering Into 11 Playlists'))
fig.add_trace(go.Scatterpolar(
r=x,
theta=categories,
fill='toself',
name='Cluster 1',
visible='legendonly'
), row=1, col=1)
fig.add_trace(go.Scatterpolar(
r=y,
theta=categories,
fill='toself',
name='Cluster 2',
visible='legendonly'
), row=1, col=2)
fig.update_layout(height=600, width=1400, title_text='Radar Plot of All Clusters (Fig.4)')
fig.show()
Run Code Online (Sandbox Code Playgroud) 我正在尝试用我的数据制作水平直方图,但出现此错误:
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
Run Code Online (Sandbox Code Playgroud)
当我使用时,我总是得到一个漂亮的直方图
px.histogram(df, x="columnName")
Run Code Online (Sandbox Code Playgroud)
但当我尝试将其更改为水平时,它不起作用。
px.histogram(df, y="columnName") or px.histogram(df, x="columnName", orientation='h') don't work.
Run Code Online (Sandbox Code Playgroud)
我没有 NoneType 的数据,我什至尝试过,px.histogram(y=np.random.randn(500))但它仍然不起作用。
我尝试使用go.Figure(data=[go.Histogram(y=df['columnName'])])它确实给了我一个水平历史记录,但随后我无法根据不同的列更改颜色。
任何帮助将不胜感激,提前致谢:)
语境
我目前正在创建一个数据可视化网络应用程序。我正在使用 Plotly 创建时间序列。我对传说有疑问。
我希望图例保留在底部,因为数据的标签很长,因此会干扰数据的整体尺寸。我选择的方向是水平的。
但当我添加更多数据时,图例会向上移动而不是保持在固定位置,以便可以向下添加新数据。相反,随着新数据添加到图表中,整个图例都会上升。
我的代码如下:
plotly_fig = px.line(data_frame=dataframe_cols1,x=dataframe_cols1.index,y=Columns_select1,
width=780, height=730) # Get data from the dataframe with selected columns, choose the x axis as the index of the dataframe, y axis is the data that will be multiselected
# Legend settings
plotly_fig.update_layout(showlegend=True)
plotly_fig.update_layout(legend=dict(
orientation = "h",
yanchor="bottom",
y=-.50,
xanchor="right",
x=1
))
Run Code Online (Sandbox Code Playgroud)
问题
添加新数据时如何防止图例向上移动?换句话说,如何将图例固定在其位置以防止其与图表“混合”?
是否可以为烛台定义自定义颜色?我们希望根据我们自己的业务规则为它们着色,而不是 Plotly 默认应用的“增加”和“减少”规则。
我正在尝试在 Plotly 中制作一个带有虚线和标记线的折线图。在本例中,我希望代表“加拿大”的线是虚线。我已经弄清楚如何更改宽度和颜色,但不知道如何使线条虚线。我原来的 DF 更大,所以 go.scatter 对我来说不是一个解决方案。谢谢您的帮助!
import plotly.express as px
# data
df = px.data.gapminder()
df = df[df['country'].isin(['Canada', 'USA', 'Norway', 'Sweden', 'Germany'])]
# plotly
fig = px.line(df, x='year', y='lifeExp',
color='country')
# color "Canada" black and change the width of the line
fig.update_traces(patch={"line": {"color": "black", "width": 4}},#
selector={"legendgroup": "Canada"})
#i tried to extend the code with dash and mode, but do not work:
#fig.update_traces(patch={"line": {"color": "black", "width": 4, "dash": 'dot', "mode": 'lines+markers'}},
plotly.io.write_image(fig, file='testline.png', format='png')
Run Code Online (Sandbox Code Playgroud)
目前情况:我正在编写一个 rMarkdown 文档,它的工作完全符合预期。当编织到 HTML 时,10 个绘图块中的每一个都会生成一个交互式绘图图表,并且它们工作得非常好。当编织为 PDF 或 DOCX 时,会显示每个代码块,但每个绘图不会(如预期),因为这些文件类型无法显示 HTML 内容。
目标:在编织到 HTML 时获取要渲染的绘图,但在编织到 PDF 或 DOCX 时让 ggplots 在其位置渲染(而不是在 HTML 下回显、评估或渲染),无需手动调整回显和/或评估每次针织运行中每个图的参数。
尝试过:
当前非首选选项:
保持原样,代码显示在所有三个输出中,但绘图仅显示在 HTML 输出中(PDF 和 DOCX 的报告不完整)。
为每个绘图块编写一个额外的 ggplot2 块,并将所有 echo 和 eval 设置为 true,从而为 HTML 输出中的 10 个变体中的每一个生成一个交互式绘图和一个静态绘图(总共 20 个绘图,非常重复)以及 PDF DOCX 输出回显并评估两个代码块(重复),但仅渲染 ggplot(大量不合适的未渲染绘图代码)。
为每个绘图块编写一个额外的 ggplot2 块,并手动调整每次 knit 运行的 echo 和 eval 参数(很容易错过某些内容并犯错误)。
需要:一个优雅的解决方案,可以在编写 HTML 时自动回显和评估代码块 A 并忽略代码块 B,同时在编写 PDF …
我的要求是将按钮放在直方图选项卡页面内,而不是主页上。我修改了参考代码来实现这一点。下面是修改后的代码。
从 app.layout 中注释掉并将选项卡内的 dbc.Button 移动为第一列(回调修改):
@app.callback(
Output("tab-content", "children"),
[Input("tabs", "active_tab"), Input("store", "data")],
)
def render_tab_content(active_tab, data):
"""
This callback takes the 'active_tab' property as input, as well as the
stored graphs, and renders the tab content depending on what the value of
'active_tab' is.
"""
if active_tab and data is not None:
if active_tab == "scatter":
return dcc.Graph(figure=data["scatter"])
elif active_tab == "histogram":
return dbc.Row(
[
#Modified to add the Button inside the Tab
dbc.Col(dbc.Button(
"Regenerate graphs",
color="primary", …Run Code Online (Sandbox Code Playgroud) 我有2个数据框:
df1
SeqTech NMDS1 NMDS2 NMDS3 C1 C2 C3
AM.AD.1 Sanger -1.2408789 0.39893503 -0.036690753 -1.0330785 -0.009904179 -0.06261568
AM.AD.2 Sanger -0.9050894 0.55943858 -0.121985899 -1.0330785 -0.009904179 -0.06261568
AM.F10.T1 Sanger -0.9059108 0.09466239 -0.033827792 -1.0330785 -0.009904179 -0.06261568
AM.F10.T2 Sanger -0.8511172 0.21396548 -0.061612450 -1.0330785 -0.009904179 -0.06261568
DA.AD.1 Sanger -1.1390353 0.05166118 0.306245704 -1.0330785 -0.009904179 -0.06261568
DA.AD.1T Sanger -1.2072895 0.06963215 0.241758582 -1.0330785 -0.009904179 -0.06261568
DA.AD.2 Sanger -1.1279367 -0.18692443 -0.092967153 -1.0330785 -0.009904179 -0.06261568
DA.AD.3 Sanger -1.3517083 -0.03651835 0.008165075 -1.0330785 -0.009904179 -0.06261568
DA.AD.3T Sanger -1.2616186 -0.06099534 -0.016942073 -1.0330785 -0.009904179 …Run Code Online (Sandbox Code Playgroud) plotly ×10
python ×7
plotly-dash ×2
r ×2
3d ×1
histogram ×1
matplotlib ×1
pandas ×1
pdf ×1
plotly.js ×1
r-markdown ×1
subplot ×1