是否可以使用 Plotly Dash 创建线条颜色渐变的折线图(纯粹为了美观)?
我尝试使用类似的东西:
'line': {'color': 'linear-gradient(90deg, red, red 60%, white)' }
Run Code Online (Sandbox Code Playgroud)
绘图破折号中的整个图形代码示例:
dcc.Graph(
id='MORTGAGE_RATES',
figure={
'data': [
{ "x": MORTGAGE30US['date'],"y": MORTGAGE30US['value'],"mode": "lines","name": '30 YR', 'line': {'color': 'linear-gradient(90deg, red, red 60%, white)' }},
{ "x": MORTGAGE15US['date'],"y": MORTGAGE15US['value'],"mode": "lines","name": '15 YR',},
],
'layout': {
'title': 'MORTGAGE RATES',
"paper_bgcolor": "rgb(46, 54, 65)",
"plot_bgcolor": "rgb(46, 54, 65)",
'font': {
'color': "rgb(255,255,255)"
}
}
}
)
Run Code Online (Sandbox Code Playgroud) 正如您从屏幕截图中看到的,网格线直接从框的中间开始。
有什么办法可以用情节来做到这一点吗?

这是热图的设置:
colorscale= [[0, 'yellow'], [0.7, 'red'], [1, 'darkred']]
trace_activity = go.Heatmap(
z=activity_orgname['activity'],
x=activity_orgname['month'],
y=activity_orgname['org_name'].astype(str),
hoverongaps = False,colorscale=colorscale)
layout = go.Layout(title='Activity "' + ac_name + '" per Month:',
xaxis={'type':'category', "gridcolor": "rgba(157, 166, 0, 1)"},
yaxis={'type':'category', "gridcolor": "rgba(157, 166, 0, 1)"},
plot_bgcolor="white")
fig = go.Figure(data=[trace_activity],
layout=layout,
)
iplot(fig)
Run Code Online (Sandbox Code Playgroud) 我有一个时间序列的数据框图以及一个数值列表,我想在其中绘制垂直线。该图是使用 cufflinks 包创建的交互式图。这是一个包含 1000 个时间值的三个时间序列的示例,我想在 500 和 800 处绘制垂直线。我使用“axvlinee”的尝试是基于我在类似帖子中看到的建议:
import numpy as np
import pandas as pd
import cufflinks
np.random.seed(123)
X = np.random.randn(1000,3)
df=pd.DataFrame(X, columns=['a','b','c'])
fig=df.iplot(asFigure=True,xTitle='time',yTitle='values',title='Time Series Plot')
fig.axvline([500,800], linewidth=5,color="black", linestyle="--")
fig.show()
Run Code Online (Sandbox Code Playgroud)
错误消息指出“Figure”对象没有属性“axvline”。
我不确定这条消息是由于我对基本图缺乏了解还是源于使用 igraph 的限制。
我当前的代码使用 for 循环来填充绘图线图,通常会添加数百个趋势:
fig.add_trace(go.Scatter(
x=x_axis_data,
y=y_axis_data,
mode='lines',
legendgroup="Data Group 1",
name='Data',
line=dict(
color='royalblue',
dash ='dot',
width=3.5,
)
),row=1,col=j)
Run Code Online (Sandbox Code Playgroud)
添加所有跟踪后,我将使用以下设置(我理解这是默认设置):
fig.update_layout(showlegend=True)
Run Code Online (Sandbox Code Playgroud)
我的问题是,在情节图例中,当我想要显示以下任一内容时,我最终会为每个轨迹提供一个图例标题:
1) 仅唯一的迹线名称(无重复),或 2) 图例组名称(每个仅 1 次,每个趋势不添加一个)。
有没有办法在不添加虚拟痕迹等的情况下做到这一点?仅供参考,我已经查看了以下资源:https://plotly.com/python/legend/。如果我没有使用 for 循环使用一个Fig.add_trace 函数填充多个跟踪,那么这将按原样工作,但我无法让它用 for 循环填充的图例执行我想要的操作。谢谢。
我目前正在尝试开发一个曲面图来检查以下数据框的结果。我想在 x 轴上绘制噪声的增加值,在 y 轴上绘制 mu 的增加值,在 z 轴上绘制点估计值。查看 ggplot2 和 ggplotly 后,不清楚如何在表面或 3D 图中绘制这些列中的每一列。
df <- "mu noise0 noise1 noise2 noise3 noise4 noise5
1 1 0.000000 0.9549526 0.8908646 0.919630 1.034607
2 2 1.952901 1.9622004 2.0317115 1.919011 1.645479
3 3 2.997467 0.5292921 2.8592976 3.034377 3.014647
4 4 3.998339 4.0042379 3.9938346 4.013196 3.977212
5 5 5.001337 4.9939060 4.9917115 4.997186 5.009082
6 6 6.001987 5.9929932 5.9882173 6.015318 6.007156
7 7 6.997924 6.9962483 7.0118066 6.182577 7.009172
8 8 8.000022 7.9981131 8.0010066 8.005220 8.024569 …Run Code Online (Sandbox Code Playgroud) 我正在尝试用 Plotly 制作 2*7 的子图。
\n我想使用 foor 循环迭代我的数据,为子图中的每个位置制作不同的饼图。\n我面临 2 个问题,我不知道如何在迭代时给出位置。\n而且我也不'即使我调用“show”方法,我的身材上也没有任何东西。
\nimport plotly.graph_objs as go\nfrom plotly.subplots import make_subplots\nlabels = stock_by_portals_private.index\n\nspec=[[{'type':'domain'}, {'type':'domain'}, {'type':'domain'}, {'type':'domain'}, {'type':'domain'}, \n{'type':'domain'}, {'type':'domain'}],\n [{'type':'domain'}, {'type':'domain'}, {'type':'domain'}, {'type':'domain'}, {'type':'domain'}, \n{'type':'domain'}, {'type':'domain'}]]\n\nfig = make_subplots(rows=2, cols=7, specs=spec, print_grid=True)\ni = 1\nj = 1\nfor label in labels:\n private = stock_by_portals_private[stock_by_portals_private.index == label]['id']\n pro = stock_by_portals_pro[stock_by_portals_pro.index == label]['id']\n fig.add_trace(go.Pie(labels=['PRO', 'PRIVATE'], values=[pro , private ],\\\n name="R\xc3\xa9partition des annonceurs par portail: " + str(label)), 1,1)\n\nfig.update_traces(hoverinfo='label+percent+name', textinfo='none')\nfig = go.Figure(fig)\nfig.show()\nRun Code Online (Sandbox Code Playgroud)\n 如何在plotlyexpress中找到趋势线的系数?
例如,我使用下面的代码来绘制趋势线,但现在我想知道系数。
import plotly.express as px
px.scatter(df, x='x_data', y='y_data', trendline="ols")
Run Code Online (Sandbox Code Playgroud) 我对 R 还很陌生,我正在尝试组装一个 Flexdashboard,它从用户输入中获取 x 和 y 变量并返回这些值的图表。到目前为止,我可以在下面的代码中使用 ggplotly 生成所需的图表。
output$scatter <-renderPlotly({
cat('input$x=',input$x,'\n')
cat('input$y=',input$y,'\n')
p <- ggplot(Merged_data_frame_hcat, aes_string(x=input$x, y=input$y)) +
geom_point()+
theme_minimal(base_size = 14)
g <- ggplotly(p, source = 'source') %>%
layout(dragmode = 'lasso',
margin = list(l = 100),
font = list(family = 'Open Sans', size = 16))
})
Run Code Online (Sandbox Code Playgroud)
然而,我意识到使用 ggplotly,我的 x 轴的定义并不像我使用plot_ly 在仪表板外部绘制相同变量时那样定义。
有没有办法在 Flexdashboard 旁边使用plot_ly?到目前为止,我写了这个,但没有用。顺便说一句,我在这里使用 noquote 因为plot_ly 没有很好地接受作为字符串的输入名称
output$scatter <-renderPlotly({
cat('input$x=',input$x,'\n')
cat('input$y=',input$y,'\n')
if (length(input$y) == 2){
x1 = noquote(input$x)
y1 =noquote(input$y[1])
y2 = noquote(input$y[2])
plot_ly(Merged_data_frame_hcat)%>%
add_lines(x= ~x1,y =~y1, …Run Code Online (Sandbox Code Playgroud) 我有一个数据框,一列中有 4 个不同的值,我需要为每个值设置自定义颜色。
以下是样本数据
val,cluster
118910.000000,3
71209.000000,2
25674.666667,0
109267.666667,3
8.000000,1
Run Code Online (Sandbox Code Playgroud)
下面是代码。
fig = px.histogram(types, x="val",color='cluster')
fig.show()
Run Code Online (Sandbox Code Playgroud)
types 是给定数据中的数据框名称
当我想我得到默认颜色。相反,我需要得到
0:红色
2:蓝色
1:紫色
3:绿色
如何在 python 中为直方图设置自定义颜色
任何人都可以帮忙吗?
我创建了一个子图,老实说,在情节文档中并不清楚如何使这些标题更大。
\nhttps://plotly.com/python-api-reference/ generated/plotly.subplots.make_subplots.html
\n这是供参考的文档,他们谈论设置 x_axis 标题和 y_axis 标题。我做到了,但我似乎找不到修改它的方法。
\n\nfig = make_subplots(\n rows=6, cols=4,\n subplot_titles=subplot_titles,\n y_title=\'Distance (\xc3\x85)\',\n x_title=\'Resid ID\'\n)\n\npositions = [\n [1, 1], [1, 2], [1, 3], [1, 4],\n [2, 1], [2, 2], [2, 3], [2, 4],\n [3, 1], [3, 2], [3, 3], [3, 4],\n [4, 1], [4, 2], [4, 3], [4, 4],\n [5, 1], [5, 2], [5, 3], [5, 4],\n [6, 1], [6, 2], [6, 3], [6, 4],\n ]\n\n\nfor i in range(0, len(segids)):\n \n fig.add_trace(\n go.Scattergl(\n x=resid_titles,\n …Run Code Online (Sandbox Code Playgroud) plotly ×10
python ×8
pandas ×4
r ×2
cufflinks ×1
ggplot2 ×1
ggplotly ×1
heatmap ×1
histogram ×1
plotly-dash ×1
python-3.x ×1
shiny ×1
statsmodels ×1
surface ×1