我正在尝试使用plotly_relayout来获取一个图的x轴缩放限制,并将它们应用到Shiny中的另一个图。到目前为止,我可以从“plot1”(x轴限制)获取相关的plotly_relayout数据,将其转换(从数字到日期),并在绘制“plot2”之前将其提供,但它实际上并没有设置缩放“plot2”上的范围。
在大多数情况下,当我尝试放大“plot1”时,我的 RStudio 就会崩溃。只有在 RStudio 不崩溃的少数情况下,我才会看到“plot2”中的“coord_cartesian”没有达到预期的效果(在放大plot1之后)。
我也很好奇,考虑到下面的代码,我的 RStudio 持续崩溃是否是正常的,或者我是否需要考虑重新重建 RStudio。任何关于如何实现这种效果的想法将不胜感激!
library(ggplot2)
library(plotly)
library(shinydashboard)
library(shinyWidgets)
#Data frame with dates and bogus data
a=data.frame(Date=seq.Date(as.Date("2000-01-01"),
as.Date("2000-12-31"),
"day"),
value=rnorm(366)
)
#Simple dashboard with two plots
ui <- dashboardPage(
dashboardHeader(title="Sample App"),
dashboardSidebar(
),
dashboardBody(
plotlyOutput("plot1"),
plotlyOutput("plot2")
)
)
server <- function(input, output, session){
#Create a reactive list, set zoomX1 and zoomX2 as NULL
reactiveList <- reactiveValues(zoomX1=NULL,zoomX2=NULL)
#Create a reactive function to update the reactive list every time the plotly_relayout changes
relayout_data <- reactive({ …Run Code Online (Sandbox Code Playgroud) 我正在尝试将趋势线添加到绘制的条形图中plotly
代码:
import plotly.express as px
fig = px.bar(count, x="date", y="count",trendline="ols")
fig.update_layout(
xaxis_title="Date",
yaxis_title = "Count"
)
fig.show()
Run Code Online (Sandbox Code Playgroud)
错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-129-8b01de219d3c> in <module>
----> 1 fig = px.bar(count, x="date", y="count",trendline="ols")
2
3 fig.update_layout(
4 xaxis_title="Date",
5 yaxis_title = "Count"
TypeError: bar() got an unexpected keyword argument 'trendline'
Run Code Online (Sandbox Code Playgroud)
这是数据
如何成功地将趋势线添加到该图中?
我刚刚开始学习Plotly,它在Jupyter Notebook. 但是当我运行相同的脚本时.py它会在浏览器中打开一个窗口,但随后显示
\n\n\n无法连接\n Firefox 无法\xe2\x80\x99 建立与位于 127.0.0.1:5875 的服务器的连接。\n 并且未显示图表或图形。
\n
有人知道如何做到这一点吗?基本上我想显示来自csv在网络浏览器中显示数据。
我有以下熊猫数据框:
>>> print(df.head())
X Y Z R G B
0 -846.160 -1983.148 243.229 22 24 19
1 -846.161 -1983.148 243.229 31 37 28
2 -846.157 -1983.148 243.231 20 21 18
3 -846.160 -1983.148 243.230 21 25 18
4 -846.159 -1983.147 243.233 38 48 34
Run Code Online (Sandbox Code Playgroud)
我将其中的数据绘制成 3D 散点图,如下所示:
import plotly.express as px
fig = px.scatter_3d(df, x='X', y='Y', z='Z')
fig.update_traces(marker=dict(size=4), selector=dict(mode='markers'))
fig.show()
Run Code Online (Sandbox Code Playgroud)
情节如下所示。
如您所见,图中的每个标记都是蓝色的。是否有任何选项,如何使用 DataFrame 中的我的R, G,B列df来更改图中每个标记的颜色?
TLDR:我想用三个字母缩写而不是每个月的数字来标记框架滑块。
我创建了一个条形图,显示 40 年来每个月的平均积雪深度。我从 NOAA 提取数据,然后使用 lubridate 按年份和月份进行分组。这是代码:
snow_depth <- govy_data$snwd %>%
replace_na(list(snwd = 0)) %>%
mutate(month_char = month(date, label = TRUE, abbr = TRUE)) %>%
group_by(year = year(date), month = month(date), month_char) %>%
summarise(avg_depth = mean(snwd))
Run Code Online (Sandbox Code Playgroud)
mutate 函数在数据框中创建一个列 (month_char),其中包含每个月份的三个字母缩写。此列的类是有序因子。
下面的代码显示了我如何创建图表/动画:
snow_plot <- snow_depth %>% plot_ly(
x = ~year,
y = ~avg_depth,
color = ~avg_temp,
frame = ~month,
text = ~paste('<i>Month</i>: ', month_char,
'<br><b>Avg. Depth</b>: ', avg_depth,
'<br><b>Avg. Temp</b>: ', avg_temp),
hoverinfo = 'text',
type = 'bar'
)
snow_plot
Run Code Online (Sandbox Code Playgroud)
此代码生成一个动画效果良好的绘图,如下所示: …
我使用plotly 在一个带有颜色渐变的闪亮应用程序中渲染3D 散点图。虽然基本散点图本身没有问题,但我似乎无法更改颜色渐变的颜色,即使对于此处提供的示例也是如此: https: //plotly.com/r/3d-scatter-plots/。(参见代码片段)。
我尝试了标准颜色和十六进制的多种颜色组合。
library(plotly)
fig <- plot_ly(mtcars, x = ~wt, y = ~hp, z = ~qsec,
marker = list(color = ~mpg, colorscale = c('#FFE1A1', '#683531'), showscale = TRUE))
fig <- fig %>% add_markers()
fig <- fig %>% layout(scene = list(xaxis = list(title = 'Weight'),
yaxis = list(title = 'Gross horsepower'),
zaxis = list(title = '1/4 mile time')),
annotations = list(
x = 1.13,
y = 1.05,
text = 'Miles/(US) gallon',
xref = 'paper',
yref = 'paper', …Run Code Online (Sandbox Code Playgroud) 我想换行文本,就像它是一个长句子一样,添加换行符,使字体具有更大的尺寸,例如,像这里
我有以下数据可以从https://gofile.io/d/8djNCU下载:
这是我的代码:
import plotly.express as px
import pandas as pd
df = pd.read_excel("/Users/stuff/Desktop/test.xlsx")
g1 = df.Group1
g2 = df.Group2
g3 = df.Group3
g4 = df.Group4
my_values = df.Values
df = pd.DataFrame(
dict(group1=g1, group2=g2, group3=g3, group4=g4, my_values=my_values)
)
fig = px.sunburst(df, path=['group1', 'group2', 'group3', 'group4'], values='my_values')
fig.show()
Run Code Online (Sandbox Code Playgroud)
文本可以换行吗?我希望长文本尽可能大并用断线分割,因为我想使用图像进行演示。
我正在使用滑块来显示热图动画,并希望重新标记滑块上出现的刻度线。
我的动画中有 50 帧,热图中的每个矩阵的尺寸为 7×7,其中七行/列对应于列表,labels=['A','B','C','D '、'D'、'F'、'G']。此外,在我的应用程序中,每个帧对应于 5 秒的窗口,因此,例如,第 3 帧对应于 15 秒。这是到目前为止我的简单示例,我模仿https://plotly.com/python/sliders/上的文档
import numpy as np
import plotly.graph_objs as go
N = 50
M = np.random.random((N, 7, 7))
labels=['A','B','C','D','D','F','G']
window_length=5
fig = go.Figure()
for step in range(N):
fig.add_trace(
go.Heatmap(x=labels,y=labels,z=M[step]))
fig.data[1].visible = True
# Create and add slider
steps = []
for i in range(len(fig.data)):
step = dict(
method="update",
args=[{"visible": [False] * len(fig.data)},
{"title": "Time: " + str(window_length*i)+' sec'}],)
step["args"][0]["visible"][i] = True # Toggle i'th trace to "visible"
steps.append(step)
sliders …Run Code Online (Sandbox Code Playgroud) 我用来plotly express绘制boxplot如下图所示:
px.box(data_frame=df,
y="price",
x="products",
points="all")
Run Code Online (Sandbox Code Playgroud)
然而,产品的盒盆以相同的颜色显示。它们是四种产品。我想用不同的颜色为每个颜色着色,使用附加参数color_discrete_sequence不起作用。
plotly ×10
python ×7
r ×3
pandas ×2
python-3.x ×2
shiny ×2
bar-chart ×1
events ×1
networkx ×1
plotly-dash ×1
r-plotly ×1
regression ×1
scatter3d ×1