是否可以将 ggplot ly和 ggplot 与拼凑而成?
这将并排显示两个图
library(ggplot2)
library(plotly)
library(patchwork)
a <- ggplot(data.frame(1), aes(X1)) + geom_bar()
b <- ggplot(data.frame(1), aes(X1)) + geom_bar()
a + b
Run Code Online (Sandbox Code Playgroud)
但是,如果我们将其转换为 ggplotly,则会出错
b <- ggplotly(b)
a + b
Error: Can't add `b` to a ggplot object.
Run `rlang::last_error()` to see where the error occurred.
Run Code Online (Sandbox Code Playgroud)
有没有办法解决这个问题?
使用样本数据:
library(tidyverse)
library(plotly)
myplot <- diamonds %>% ggplot(aes(clarity, price)) +
geom_boxplot() +
facet_wrap(~ clarity, ncol = 8, scales = "free", strip.position = "bottom") +
theme(axis.ticks.x = element_blank(),
axis.text.x = element_blank(),
axis.title.x = element_blank())
ggplotly(myplot)
Run Code Online (Sandbox Code Playgroud)
返回类似:
与第一个和最后一个相比,内部刻面的缩放非常糟糕,并且有很多额外的填充。我试图从这些问题中找到解决方案:
R:facet_wrap 无法在 Shiny 应用程序中使用 ggplotly 正确呈现
通过反复试验,我使用panel.spacing.x = unit(-0.5, "line")了theme()它,它看起来更好一些,很多额外的填充消失了,但内部方面仍然明显更小。
同样作为一个额外的问题,但不是那么重要ggplotly(),当我将它们设置在底部时,条形标签是调用中的顶部。这里似乎是一个持续存在的问题,有没有人有一个hacky的解决方法?
编辑:在我的真实数据集中,我需要每个方面的 y 轴标签,因为它们的比例非常不同,所以我将它们保留在示例中,这就是为什么我需要facet_wrap. 我的真实数据集的屏幕截图以供解释:
我使用下面的代码在 plotly dash 上显示 x 和 y 值。但是我希望能够在“值”文本字段下方添加另一个文本字段。
文本字段将被称为“类别”,因此如果显示的 y 值是:5k,则类别 = 不贵,或者如果值为 20k,则类别 = 平均价格,如果值为 30k,则类别 = 太贵。
我将如何实现这一点?这是显示悬停在其上的值的运行代码
import json
from textwrap import dedent as d
import pandas as pd
import plotly.graph_objects as go
import numpy as np
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
from dash.dependencies import Input, Output
from jupyter_dash import JupyterDash
# app info
app = JupyterDash(__name__)
styles = {
'pre': {
'border': 'thin …Run Code Online (Sandbox Code Playgroud) 我有一个数据框
Date Category Sum
0 2019-06-03 "25M" 34
1 2019-06-03 "25M" 60
2 2019-06-03 "50M" 23
3 2019-06-04 "25M" 67
4 2019-06-05 "50M" -90
5 2019-06-05 "50M" 100
6 2019-06-06 "100M" 6
7 2019-06-07 "25M" -100
8 2019-06-08 "100M" 67
9 2019-06-09 "25M" 450
10 2019-06-10 "50M" 600
11 2019-06-11 "25M" -9
12 2019-07-12 "50M" 45
13 2019-07-13 "50M" 67
14 2019-07-14 "100M" 130
15 2019-07-14 "50M" 45
16 2019-07-15 "100M" 100
17 2019-07-16 "25M" -90
18 2019-07-17 "25M" 700 …Run Code Online (Sandbox Code Playgroud) 我知道跟踪(标记/线)有 hovertemplate/hover_text/ 选项,但我找不到形状这样的东西。有没有办法在形状上移动时弹出悬停文本?也许是一种解决方法?
例子:
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(
x=[1.5, 3],
y=[2.5, 2.5],
text=["Rectangle reference to the plot",
"Rectangle reference to the axes"],
mode="markers",
))
fig.add_shape(
# Rectangle reference to the plot
type="rect",
xref="paper",
yref="paper",
x0=0.25,
y0=0,
x1=0.5,
y1=0.5,
line=dict(
color="LightSeaGreen",
width=3,
),
fillcolor="PaleTurquoise",
)
Run Code Online (Sandbox Code Playgroud)
当我将鼠标悬停在这两点上时,我会得到一个包含信息的悬停模板。我怎样才能得到形状相似的东西?
正如这里所宣布的,Dash 的热重载应该在debug=True. 但是,这对我不起作用。我什至尝试明确设置dev_tools_hot_reload=True.
我从 IDE 中保存了代码文件,但此后在 Web 界面上看不到任何更改。
即使重新加载页面也没有区别。我必须停止服务器并重新运行脚本。
可能是什么问题?
编辑
我正在使用 VSCode 并从 windows powershell(在 VSCode 终端之外)运行脚本。
从 conda-forge 安装新版本不起作用。
我有一个看起来像这样的数据框:
我想要一个用于旧频率和一个用于新频率的条。目前我的图表如下所示:
这是代码的样子:
freq_df['date'] = pd.to_datetime(freq_df['date'])
freq_df['hour'] = freq_df['hour'].astype(str)
fig = px.bar(freq_df, x="hour", y="old freq",hover_name = "date",
animation_frame= freq_df.date.dt.day)
fig.update_layout(transition = {'duration': 2000})
Run Code Online (Sandbox Code Playgroud)
我如何添加另一个酒吧?
关于DF的说明:
它具有与特定日期中的每个小时相关的频率。
编辑:一种方法可能是创建一个类别列并添加旧的和新的频率并在另一个频率列中分配值。我该怎么做:p?
编辑:这是DF
,date,hour,old freq,new freq
43,2020-09-04,18,273,224.0
44,2020-09-04,19,183,183.0
45,2020-09-04,20,99,111.0
46,2020-09-04,21,130,83.0
47,2020-09-04,22,48,49.0
48,2020-09-04,23,16,16.0
49,2020-09-05,0,8,6.0
50,2020-09-05,1,10,10.0
51,2020-09-05,2,4,4.0
52,2020-09-05,3,7,7.0
53,2020-09-05,4,25,21.0
54,2020-09-05,5,114,53.0
55,2020-09-05,6,284,197.0
56,2020-09-05,7,343,316.0
57,2020-09-05,8,418,419.0
58,2020-09-05,9,436,433.0
59,2020-09-05,10,469,396.0
60,2020-09-05,11,486,300.0
61,2020-09-05,12,377,140.0
62,2020-09-05,13,552,103.0
63,2020-09-05,14,362,117.0
64,2020-09-05,15,512,93.0
65,2020-09-05,16,392,41.0
66,2020-09-05,17,268,31.0
67,2020-09-05,18,223,30.0
68,2020-09-05,19,165,24.0
69,2020-09-05,20,195,15.0
70,2020-09-05,21,90,
71,2020-09-05,22,46,1.0
72,2020-09-05,23,17,1.0
Run Code Online (Sandbox Code Playgroud) 我有一个应该正确显示的绘图对象,但由于某种原因,它在 DataBricks 中只显示为空白。对象类型为:
plotly.graph_objs._figure.Figure
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法来显示该图:
fig.show()
display(fig)
displayHTML(fig.to_html())
Run Code Online (Sandbox Code Playgroud)
我能想到的所有可能的解决方案都会导致同样的事情。谢谢!顺便说一句...使用 Plotly 4.9 版
我希望第 5 个子图位于第三行两列的中心。(我已经尝试通过添加domain参数来做到这一点)。这是重现它的代码-
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots
continent_df = pd.read_csv('https://raw.githubusercontent.com/vyaduvanshi/helper-files/master/continent.csv')
temp_cont_df = pd.pivot_table(continent_df, index='continent', aggfunc='last').reset_index()
fig = make_subplots(rows=3, cols=2, specs=[[{'type':'pie'},{'type':'pie'}],[{'type':'pie'},{'type':'pie'}],
[{'type':'pie'},{'type':'pie'}]])
fig.add_pie(labels=continent_df.continent, values=continent_df.new_cases, row=1,col=1)
fig.add_pie(labels=continent_df.continent, values=continent_df.new_deaths, row=1,col=2)
fig.add_pie(labels=continent_df.continent, values=continent_df.new_recovered, row=2,col=1)
fig.add_pie(labels=continent_df.continent, values=continent_df.new_tests, row=2,col=2)
fig.add_pie(labels=temp_cont_df.continent, values=temp_cont_df.active_cases, row=3,col=1,domain={'x':[0.25,0.75],'y':[0,0.33]})
Run Code Online (Sandbox Code Playgroud)
如果我不在参数中包含第 6 个图specs,则会引发错误。
我想用 plotly (python) 向现有图形添加箭袋。但是我能找到的唯一和平的文件要么只创建一个箭袋(这里)或一个全新的人物(那里)。
这是 plotly doc 的示例:
import plotly.figure_factory as ff
import numpy as np
x,y = np.meshgrid(np.arange(0, 2, .2), np.arange(0, 2, .2))
u = np.cos(x)*y
v = np.sin(x)*y
fig = ff.create_quiver(x, y, u, v)
fig.show()
Run Code Online (Sandbox Code Playgroud)
如果有人对我所做的情节有更好的理解,我会很感激一些解释!
非常感谢,
plotly ×10
python ×8
plotly-dash ×3
ggplot2 ×2
ggplotly ×2
r ×2
databricks ×1
hover ×1
pandas ×1
patchwork ×1