I have a jupyter notebook that has a plotly figure object. I want to be able to recreate the plotly fig object from the plot but I only have the output cell containing the plot from a previous run. I can not re-run the notebook to recreate the figure object. How do I extract the figure object from the output cell since all the data is already there.
first get cells from the current or other notebook.
import nbformat
path =r'c:my_jupyter_notebook.ipynb'
NB_VERSION = 4
with open(path) as f:
nb = nbformat.read(f, NB_VERSION)
markdown_cells = [
cell['source']
for cell in nb['cells'] # go through the cells
if cell['cell_type'] == 'markdown' and cell['source'] # skip things like 'code' cells, and empty markdown cells
]
out_cells=[]
for cell in nb['cells']:
if 'outputs' in cell:
out_cells.append(cell)
next get fig dict from the cell containing your plot. in my case cell index 1 has the plot
out_cells[1]['outputs'][0]['data']['application/vnd.plotly.v1+json'].keys()
d=out_cells[1]['outputs'][0]['data']['application/vnd.plotly.v1+json']
fig_cell_dict={key:value for key,value in d.items() if key != 'config'} # get only data and layout i.e ignore config key
finally convert the dict to fig object
fig=go.Figure (fig_cell_dict)
fig
the fig object has been recreated from a cell containing the plot
Related
I am attempting to output a set of graphs (as PDFs) which I have turned into a list. I want their ID to be in the title of the PDF name.
To create the list I am doing this:
All_Plots=dlply(Data, .(`ID`),function(x) Plots %+% x +facet_wrap(~ID))
Next, I want to get out of the PDFs out with their 'ID', which is the title of each plot
I was using this:
pdf(file="%03Plot.pdf", onefile=FALSE, paper="a4r", width=14)
All_Plots
dev.off()
But it will only give me numbers and not the IDs. I am wondering if I should use paste0?
pdf(file=paste0(Data$ID, "%03Plot.pdf"), onefile=FALSE, paper="a4r", width=14)
This gives me the first ID in the data frame but not the rest
I have below sample csv data.
date,Data Center,Customer,companyID,source,target,value
6/1/2021,dcA,customer1,companyID1,step1:open_list_view,exit,1
6/1/2021,dcB,customer2,companyID2,step1:open_list_view,exit,1
6/1/2021,dcC,customer3,companyID3,step1:open_list_view,exit,1
6/2/2021,dcD,customer4,companyID4,step1:open_list_view,exit,2
6/2/2021,dcE,customer5,companyID5,step1:open_list_view,step2:switch_display_option,1
.....
Now I click 'R' icon and then enable R
Next I import the csv data to powerBI and drag and drop 'source', 'target' and 'value' columns to "Visualization->Values" section.
Then I run below R script, and want to visualize the sankey chart in PowerBI
# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script:
# dataset <- data.frame(source, target, value)
# dataset <- unique(dataset)
# Paste or type your script code here:
library(networkD3)
node_names <- unique(c(dataset$source, dataset$targe))
nodes <- data.frame(name=node_names)
links <- data.frame(source = match(dataset$source, node_names) - 1,
target = match(dataset$target, node_names) - 1,
value = dataset$value)
sankeyNetwork(Links = links, Nodes = nodes,
Source = "source",
Target = "target",
Value = "value")
but i got 'No image was created'.
how to correct the code to make the sankey chart can visualized in powerBI?
every Python and R visuals is plot. it requires the script to be plotted. add R script at end of your code.
something like
plot(source, target, type="n",)
I have the following CSV document (3 columns, 1 header line, 10 data lines):
Artikelname,Anzahl Zeichen,Anzahl Fehler
Sport/Ski alpin,5459,42
People,2302,20
Nationale Politik,4012,43
Reportage ueber die Lebensmittelindustrie,11202,101
Wirtschaft,3192,22
Interview,2989,21
Sport/Tennis,1509,14
Filmkritik,2498,65
Regionalpolitik,3987,32
Mali-Reportage,10782,91
I now wish to plot the second column on the x-Axis and the third column on the y-Axis of a scatterplot in R.
getwd()
file <- "[filepath]"
data <- read.csv(file, skip = 1) # skip header line
data # print the data into the console
plot(data[2],data[3])
I believe it is because my data is not of the correct type yet, but I don't have any idea how to fix this.
First of all, remove skip=1. Your current code uses the first line of data as the column names instead of data. After reading the data correctly, you want
plot(data[,2],data[,3])
or perhaps more simply
plot(data[,2:3])
I want to make variable "list" containing filenames, variable "i" containing number of datafiles and plot it into single graph. Example of filenames: ConvAut.dat, ConvMoveAut.dat, CutAut.dat ...
Here is my try:
list = system("echo $(dir *.dat)")
plot for [1:i] i using 1:((2*i)+(column(2))) w steps tit i
I just found this piece of code but i dont know how to edit it.
I have version 4.6 patchlevel 0. Can you help me?
Thanks for the answers
list = system("dir /b *.dat") # Windows
# list = system("ls *.dat") # Unix
plot for [i=1:words(list)] word(list, i) using 1:((2*i)+column(2)) w steps title word(list, i)
The list is a string which contains all files, separated by white spaces. In the plot command you could also iterate over this file list with plot for [file in list] file ..., but then you don't have access to the file number. So I used words to get the number of files, and word(list, i) to get the i-th file name.
Note, that this works only if the file names don't contain white spaces.
Context: I am using R to do some data manipulation which I then export to Excel and create a barchart with.
Problem: Thus far it is relaitively easy to record an Excel VBA macro and then translate it into R code via the RDCOMClient package. However I am at a loss as how to interpret the VBA "with" function structure.
Question: I would like to translate the following Excel VBA code into R code (specifically using the RDCOMClient package):
' Activate barchart
ActiveSheet.ChartObjects("Chart 1").Activate
' Select the Male data column
ActiveChart.SeriesCollection(1).Select
' Change the colour of the Male bars in the barchart
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Solid
End With
Reproducible code: The following R code will set up the excel worksheet with a barchart
# Load package and helper functions - see http://www.omegahat.org/RDCOMClient
require(RDCOMClient)
source("http://www.omegahat.org/RDCOMClient/examples/excelUtils.R")
# Create Excel application
xls <- COMCreate("Excel.Application")
# Make Excel workbook visible to user
xls[["Visible"]] <- TRUE
# Add a worksheet to the workbook
wb = xls[["Workbooks"]]$Add(1)
# Add data.frame to worksheet (Hishest Qualification of Job Applicants by Sex)
df <- data.frame(Degree=c("BSc", "MSc", "PhD"), Male=c(322, 107, 39), Female=c(251, 128, 25))
exportDataFrame(df, at = wb$ActiveSheet()$Range("A1"))
# Add Chart
chart.display.range <- wb$ActiveSheet()$Range("E2:M20")
wb$ActiveSheet()$Range("A1:C4")$Select()
wb$ActiveSheet()$Shapes()$AddChart(Top = chart.display.range$Top(),
Left = chart.display.range$Left(),
Height = chart.display.range$Height(),
Width = chart.display.range$Width())$Select()
What I've done thus far: The first two lines of the VBA code are easy to translate into R:
# Activate chart
wb$ActiveSheet()$ChartObjects("Chart 1")$Activate()
# Select the Male data column
male <- wb$ActiveChart()$SeriesCollection(1)
male$Select()
And then for the with structure
# bar colour to be changed (this is a guess)
bar <- male$Selection()$Format()$Fill()
which results in the following error:
#Error in .COM(x, name, ...) :
# Cannot locate 0 name(s) Selection in COM object (status = -2147352570)
Which I'm interpreting as "Selection" should not be used? I'm not sure where to go from here but I think that once I fix the error above then I'd do something like the following:
bar[["Visible"]] = 1
bar[["ForeColor"]][["ObjectThemeColor"]] = 5
bar[["ForeColor"]][["TintAndShade"]] = 0
bar[["ForeColor"]][["Brightness"]] = 0
Thanks in advance!
P.S. I know that there may be an option to export an R plot into Excel but I'm more interested in trying to figure out how to interpret the "with" function structure.
P.P.S I'm using Windows 7 x64, x86_64-w64-mingw32/x64 (64-bit), R 3.0.1, RDCOMClient_0.93-0.1
Do
bar <- male$Format()$Fill()
Instead of
bar <- male$Selection()$Format()$Fill()