我试图将ggplot对象转换为plotly并在闪亮的应用程序中显示它.但我遇到一个错误"没有适用于'plotly_build'的方法应用于类"NULL"的对象
我能够成功地将ggplot对象返回到闪亮的应用程序,
output$plot1 <- renderplot({
gp <- ggplot(data = mtcars, aes(x = disp, y = cyl)) + geom_smooth(method = lm, formula = y~x) + geom_point() + theme_gdocs()
})
Run Code Online (Sandbox Code Playgroud)
但莫名其妙地无法转换它.
我的代码看起来像这样
output$plot2 <- renderplotly({
gp <- ggplot(data = mtcars, aes(x = disp, y = cyl)) + geom_smooth(method = lm, formula = y~x) + geom_point() + theme_gdocs()
ggplotly()
})
Run Code Online (Sandbox Code Playgroud) 我正在使用ggplotly来显示交互式时间序列图.x轴采用日期格式,但悬浮工具提示正在将日期格式转换为数字(附加截图).关于如何在工具提示中将日期显示为正确日期的任何想法?
下面是一小段代码:
output$ggplot <- renderPlotly({
plotbycity<-df_postgres %>% group_by(city, date, bedroooms) %>%
filter(city %in% input$checkGroup & bedroooms==input$radio) %>%
summarise(count=n(),rent=median(rent)) %>%
ungroup()
plotbycity$date<-as.Date(plotbycity$date)
# Error handling
plotbycity<-plotbycity[!is.na(plotbycity$city),]
if (is.null(plotbycity)) return(NULL)
#plotbycity<-ungroup(plotbycity)
#dat <- dat[c("rent", "bedroooms", "date", "City")]
#dat <- melt(dat,id.vars=c("date", "City", "bedroooms"),na.rm=TRUE) #
# draw the line plot using ggplot
gg <-ggplot(plotbycity, aes(x = date, y = rent, group = city, color = city,
text = paste('obs: ', count))) +
geom_line() +
ggtitle("Monthly Rents")
# #theme_hc(bgcolor = "darkunica") +
# #scale_fill_hc("darkunica")
p …Run Code Online (Sandbox Code Playgroud) 我正在尝试在Jupyter笔记本中创建一个Sankey图表,我的代码基于此处显示的第一个示例.
我最终得到了这个,我可以运行而不会出现任何错误:
import numpy as npy
import pandas as pd
import plotly as ply
ply.offline.init_notebook_mode(connected=True)
df = pd.read_csv('C:\\Users\\a245401\\Desktop\\Test.csv',sep=';')
print(df.head())
print(ply.__version__)
data_trace = dict(
type='sankey',
domain = dict(
x = [0,1],
y = [0,1]
),
orientation = "h",
valueformat = ".0f",
node = dict(
pad = 10,
thickness = 30,
line = dict(
color = "black",
width = 0.5
),
label = df['Node, Label'].dropna(axis=0, how='any'),
color = df['Color']
),
link = dict(
source = df['Source'].dropna(axis=0, how='any'),
target = df['Target'].dropna(axis=0, …Run Code Online (Sandbox Code Playgroud) 我正在创建一个简单的折线图,可以在Shiny中正确呈现.
我现在添加了一个selectInput,其名称为2个不同的度量,写在我的数据集中.我希望我的y变量能够相应地改变.
p <- plot_ly(data = LineChartData(), x= Calendar.Month, y = input$Measure, type = "line", group = Calendar.Year, col = Calendar.Year)
Run Code Online (Sandbox Code Playgroud)
不幸的是,图表只有一点呈现.它不是在输入$ Measure并在我的数据集中找到该字段.
我知道在使用ggplot时,我会将我的aes切换为aes_string.在情节上是否有类似的解决方案?
编辑:这是一些可重现的代码
这是ui.R文件
#ui.R
shinyUI(
fluidPage(
titlePanel("Inbound Intermediary Performance"),
sidebarLayout(
sidebarPanel(
h4("Parameters"),
br(),
selectInput("Measure", "Measure", c("Var1","Var2"))
),
mainPanel(
plotlyOutput("lineChart")
)
)
)
)
Run Code Online (Sandbox Code Playgroud)
server.R
#server.R
library(plotly)
library(shiny)
library(ggplot2)
#Create data
data <- data.frame(Month = c(1,2,3,4,5,6,7,8,9,10,11,12), Var1 = c(36,33,30,27,24,21,18,15,12,9,6,3), Var2 = c(4,8,12,16,20,24,28,32,36,40,44,48))
shinyServer(function(input, output) {
#Create plot
output$lineChart <- renderPlotly({
#using ggplot
p <- ggplot(data=data, aes_string(x='Month', y = …Run Code Online (Sandbox Code Playgroud) 当在Jupyter笔记本中使用Plotly绘制带有子图的离线iplot图时,在图中我获得输出:
This is the format of your plot grid:
[ (1,1) x1,y1 ] [ (1,2) x2,y2 ] [ (1,3) x3,y3 ] [ (1,4) x4,y4 ]
[ (2,1) x5,y5 ] [ (2,2) x6,y6 ] [ (2,3) x7,y7 ] [ (2,4) x8,y8 ]
[ (3,1) x9,y9 ] [ (3,2) x10,y10 ] [ (3,3) x11,y11 ] [ (3,4) x12,y12 ]
[ (4,1) x13,y13 ] [ (4,2) x14,y14 ] [ (4,3) x15,y15 ] [ (4,4) x16,y16 ]
Run Code Online (Sandbox Code Playgroud)
如何从输出中删除它?
示例代码:
import plotly as pl
import …Run Code Online (Sandbox Code Playgroud) 我试图使用"情节"功能,但它根本不适用于我的情况."ggplot"工作在2D的情况下,但是在添加一个轴时会出错.如何解决这个问题?
ggplot(data,aes(x=D1,y=D2,z=D3,color=Sample))+geom_point()
Run Code Online (Sandbox Code Playgroud)
如何添加一个轴并获得3D图?谢谢.
我已经构建了一个图表,我想将其嵌入到HTML文件中.如果我plotly在线使用,它可以按预期工作.但是,如果我使用OFFLINE离线图表工作(即它打开一个单独的HTML图表),但它没有嵌入HTML(nick.html),iframe即为空.
这是我的代码:
fig = dict(data=data, layout=layout)
plotly.tools.set_credentials_file(username='*****', api_key='*****')
aPlot = plotly.offline.plot(fig, config={"displayModeBar": False}, show_link=False,
filename='pandas-continuous-error-bars.html')
html_string = '''
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<style>body{ margin:0 100; background:whitesmoke; }</style>
</head>
<body>
<h1>Monthly Report</h1>
<!-- *** Section 1 *** --->
<h2></h2>
<iframe width="1000" height="550" frameborder="0" seamless="seamless" scrolling="no" \
src="''' + aPlot + '''.embed?width=800&height=550"></iframe>
<p> (Insights).</p>
</body>
</html>'''
f = open("C:/Users/nicholas\Desktop/nick.html",'w')
f.write(html_string)
f.close()
Run Code Online (Sandbox Code Playgroud)
任何人都知道它为什么不嵌入以及如何修复它?
我有大约 3 年的数据,按月和年组织。我想将这些数据按月和年绘制为条形图,以月为 x 轴,以年为迹线。问题是数据从 2018 年 9 月开始,因此要绘制的第一个月是 9 月,但我希望是 1 月。我尝试过这样做,但年份却按递减顺序排列,这也是我不希望的。这是一个代码示例。
import datetime
import pandas as pd
import plotly.graph_objects as go
import random
df = pd.DataFrame({})
numdays = 1000
base = datetime.datetime.today()
date_list = [base - datetime.timedelta(days=x) for x in range(numdays)]
price = [random.randint(1,10) for i in range(numdays)]
df['price'] = price
df.index = date_list
df = df.resample('MS').sum()
df['month'] = df.index.month_name()
df['month number'] = df.index.month
df['year'] = df.index.year
year_list = df['year'].unique().tolist()
fig = go.Figure()
for year in year_list:
df_aux …Run Code Online (Sandbox Code Playgroud) 我以前从未使用过plotly,我一直在尝试将示例图像代码导出为png。我已经安装了plotly和kaleido并在python 3上运行了代码,但没有任何反应。
#Here is the code:
import plotly.express as px
import numpy as np
# RGB Data as numpy array
img_rgb = np.array([[[255, 0, 0], [0, 255, 0], [0, 0, 255]],], dtype=np.uint8)
fig = px.imshow(img_rgb)
fig.show()
fig.write_image("fig.png")
Run Code Online (Sandbox Code Playgroud)
根据plotly文档,这应该可以解决问题,但我无法获得png图像。它没有显示任何错误,没有警告......这就像 python 陷入了无限循环。我什至无法阻止终端运行。
我正在使用 python 3.9.6 64 位
我尝试了最新版本(5.3.1)和旧版本(4.14.3),但我遇到了同样的问题。
python -m pip install plotly
kaleido是最新版本(0.2.1),但我也尝试过版本(0.2.0)。
python -m pip install -U kaleido
我可能做错了什么?
更新:我在另一台计算机上尝试了相同的过程,并且工作正常。知道为什么第一台计算机无法运行代码吗?