Graph using Sage - dot

G = DiGraph(A, format='weighted_adjacency_matrix')
G.relabel([1..10],inplace=True)
H = G.plot(edge_labels=False, graph_border=True)
H.show()
Would it be possible to highlight a certain route through this graph? i.e make some lines a different colour, say, red?

Is this what you are looking for ?
sage: g = graphs.PetersenGraph()
sage: g.show(edge_colors={'red':[(0,1),(1,6),(6,9),(9,4)] })
The documentation of all available options for graph plots can be found on this page:
http://www.sagemath.org/doc/reference/plotting/sage/graphs/graph_plot.html
Nathann

Related

Have two (or more) node label sets in Julia GraphPlot maybe using Compose?

Here is a minimal working code from Julia Discourse:
using LightGraphs
using GraphPlot
using Colors
g = graphfamous("karate")
membership = [1,1,1,1,1,1,1,1,2,1,1,1,1,1,2,2,1,1,2,1,2,1,2,1,1,1,1,2,1,1,2,1,1,1]
nodelabels = 1:34
nodecolor = [colorant"lightgrey", colorant"orange"]
nodefillc = nodecolor[membership]
colors = [colorant"lightgray" for i in 1:78]
colors[42] = colorant"orange"
gplot(g, nodefillc=nodefillc, layout=circular_layout, edgestrokec=colors, nodelabel=nodelabels)
Which produces:
I succeed to have node labels, from 1 to 34, however, I need to display another type of labels for some specific nodes. e.g., the weight of some nodes. That is, I need, for instance, the weight of node 19 is 100 and the weight of node 1 is 0.001.
Is there a way to display such data? I could node find a relevant keyword in GraphPlot (only nodelabel only accepts a Vector) and I could not find another Julia package that could do it for plotting graphs.
EDIT thanks to #Dan Getz, before posting on SE, I had the same idea as he suggested: try to label the nodes with a string of the format "$i\n $weight"
However, the result is highly unsatisfying as you can see in this picture of one of my actual graphs. Node 12 in Orange, separated from its weight 177.0 with \n is not really nice to read!
EDIT thanks to #Przemyslaw Szufel maybe my question could be resolved with Compose (that I actually already use) which is a graphic backend for GraphPlot. Unfortunately it is a bit undocumented despite I and other people asking about it!
You could use GraphMakie.jl, which is also compatible with (Light)Graphs.jl and possibly a bit more flexible than GraphPlot.jl.
using Graphs, GraphMakie, Colors
g = smallgraph(:karate)
membership = [1,1,1,1,1,1,1,1,2,1,1,1,1,1,2,2,1,1,2,1,2,1,2,1,1,1,1,2,1,1,2,1,1,1]
nodelabels = repr.(collect(1:34))
nodecolor = [colorant"lightgrey", colorant"orange"]
nodefillc = nodecolor[membership]
colors = [colorant"lightgray" for i in 1:78]
colors[42] = colorant"orange"
fig = Figure(resolution=(500,500))
ax = Axis(fig[1,1])
pos = Shell()(g) # = circular layout
graphplot!(ax, g,
layout=_->pos,
edge_color=colors,
node_color=nodefillc,
node_size=30,
nlabels=nodelabels,
nlabels_align=(:center, :center)
)
hidedecorations!(ax)
hidespines!(ax)
# add additional annotation to node 17
weightOffset = Point2(0, 0.045)
text!(ax, "0.001", position=pos[17] - weightOffset, space=:data, align=(:center, :top), fontsize=10)
display(fig)

How can I plot bar chart in Julia?

plot(Giorni,Fatturato, label="line")
scatter!( Giorni,Fatturato, label="points")
ylabel!("Fatturato [Dollari]")
xlabel!("Tempo (Giorni)")
title!("Fatturato Giornaliero")
With this I obtain a normal graph, how can i plot a bar chart?
I guess you are looking for Plots.bar function. The code below produces the following plot
using Plots
giorni = collect(1:10)
fatturato = rand(10)
p = bar(giorni,fatturato)
ylabel!("Fatturato [Dollari]")
xlabel!("Tempo (Giorni)")
title!("Fatturato Giornaliero")
savefig(p,"barplot.png")
You can obtain barchart via bar! or histogram! depending what exactly you want to achieve. For an example:
using Plots
giorni = rand(50) .+ (0.8:0.8:40)
fatturato = giorni .+ rand(50)
plot(giorni,fatturato, label="line")
scatter!( giorni,fatturato, label="points")
ylabel!("Fatturato [Dollari]")
xlabel!("Tempo (Giorni)")
title!("Fatturato Giornaliero")
histogram!(giorni,label="giorni",bins=20)
histogram!(fatturato,label="fatturato",bins=20, orientation=:horizontal)
It is not clear however what you exactly have in mind so this is just an example.

Can I assign specific values from a ColorGradient to a marker?

I apologize for this super basic question, but I am not experienced in plotting, and a lot of the documentation for Julia plotting assumes more knowledge than I have!
I am creating a scatter plot, using Plots, where each marker is plotted based on spatial position, and I want to scale the color by magnitude of value that each marker holds. I created a color gradient as such:
C(g::ColorGradient) = RGB[g[z] for z = LinRange(0,1,M)]
g = :inferno
cgrad(g,[0.01,0.99]) |> C
M is related to the number of markers, this way I create a suitable scale of colors based on the number of markers I have.
I assumed I was creating some kind of structure that would assign a color from this gradient based off a value ranging from 0.01 to 0.99. However, I guess I don't understand what the structure C is. When I assign color = C(v), where v is between 0 and 1.00, I get an error saying that C does not accept type Float64.
Is there a way I can assign a marker some color from this gradient based off its value? I have all of the values for each location stored in another array.
UPDATE: I have also tried indexing into C. I turned my values into Int64 ranging from 1-99, and tried to set color=C[v], but C also does not take Type Int64.
UPDATE 2: Ok, so i realized my issue was I did not understand the |> functionality, So i rewrote the code to look like:
C(g::ColorGradient) = RGB[g[z] for z = LinRange(0,1,M)]
g = :inferno
myGrad = (cgrad(g,[0.00,1.00]) |> C)
and now I can index into my color gradiant! However I still am having an issue setting the color equal to the value stored in the myGradient array.
for i = 1:M
X,Y = find_coords(i,pd)
colors = myGrad[c_index[i]]
outline = rand(Float64,3)
plt = plot!(X,Y,colors, markerstrokecolor = outline)
end
When I type myGrad[c_index[i]] into REPL it plots a color. However I am getting an error from the above code which states
"Cannot convert RGB{Float64} to series data for plotting"
If i change the plot line as follows I get a slightly different error:
plt = plot!(X,Y,markercolor = colors, markerstrokecolor = outline)
ERROR: LoadError: MethodError: no method matching plot_color(::Float64)
So for some reason I cant store this color, as a color variable for my plot.
There are a few different issues at play here. Firstly, if you want to create a scatter plot, you should probably use scatter. It also doesn't seem necessary to plot things in a loop here, although it's hard to tell as your code isn't a minimum working example (MWE), as it relies on things defined somewhere else in your code.
Here's an example of how this might work:
using Plots
# Create a discrete color gradient with 20 points
my_colors = [cgrad(:inferno, [0.01, 0.99])[z] for z ∈ range(0.0, 1.0, length = 20)]
# Draw some random data points
x, y = sort(rand(100)), rand(100)
# Assign a color between 1 and 20 on the color grid to each point
z = sort(rand(1:20, 100))
# Plot
scatter(x, y, color = my_colors[z], markerstrokecolor = "white", label = "",
markersize = [10 for _ ∈ 1:100])
gives:

How can you make a stacked area / line chart in Julia with Plots.jl?

I would like to create a stacked area chart, similar to this for example, in Julia using Plots.
I know / suppose that you can do this if you directly use the Gadfly or PyPlot backends in Julia, but I was wondering if there was a recipe for this. If not, how can you contribute to the Plots Recipes? Would be a useful addition.
There's a recipe for something similar in
https://docs.juliaplots.org/latest/examples/pgfplots/#portfolio-composition-maps
For some reason the thumbnail looks broken now though (but the code works).
The exact plot in the matlab example can be produced by
plot(cumsum(Y, dims = 2)[:,end:-1:1], fill = 0, lc = :black)
As a recipe that would look like
#userplot AreaChart
#recipe function f(a::AreaChart)
fillto --> 0
linecolor --> :black
seriestype --> :path
cumsum(a.args[1], dims = 2)[:,end:-1:1]
end
If you want to contribute a recipe to Plots you can open a pull request on Plots, or, eg. on StatsPlots - there's a good description of contributing here: https://docs.juliaplots.org/latest/contributing/
It's a bit of reading, but very generally useful as an introduction to contributing to Julia packages.
You can read this thread in the Julia discourse forum where the question is developed in deep.
One solution posted there using Plots is :
# a simple "recipe" for Plots.jl to get stacked area plots
# usage: stackedarea(xvector, datamatrix, plotsoptions)
#recipe function f(pc::StackedArea)
x, y = pc.args
n = length(x)
y = cumsum(y, dims=2)
seriestype := :shape
# create a filled polygon for each item
for c=1:size(y,2)
sx = vcat(x, reverse(x))
sy = vcat(y[:,c], c==1 ? zeros(n) : reverse(y[:,c-1]))
#series (sx, sy)
end
end
a = [1,1,1,1.5,2,3]
b = [0.5,0.6,0.4,0.3,0.3,0.2]
c = [2,1.8,2.2,3.3,2.5,1.8]
sNames = ["a","b","c"]
x = [2001,2002,2003,2004,2005,2006]
plotly()
stackedarea(x, [a b c], labels=reshape(sNames, (1,3)))
(by user NiclasMattsson)
Other ways presented there include using the VegaLite.jl package.

How to create a barplot with several variable

I am very new to R and I am trying to create a barplot with my infiltration on the y axis and burn on the x axis comparing the grazing treatment (example of the data set is below)
Infiltration Grazing Burn
3301.145496 G S
8165.771889 U S
9937.833576 G L
11576.5892 U L
32739.07643 G N
25923.84328 U N
25942.3 G C
So far I have produced the code so that it reads the table
#reads the basic data in
ana<-read.table("D:\\Dave.txt",head=T)
attach(ana)
head(ana)
dim(ana)
However I do not understand how to write the code to produce a barplot.
I have attached an image of how I produced the graph on excel
excel graph
Also I do not have the ggplot, how do I format it as "barplot(....."
With the sample dataset available, an example could be:
barplot(as.matrix(mtcars[,10:11]), beside = TRUE)
With your dataset and assuming 'ana$burn' is coded as a factor:
barplot(xtabs(ana$Infiltration ~ ana$Burn + ana$Grazing),beside = TRUE)
The beside = TRUE gives your barplot side by side, if you want to stack, put beside = FALSE (just look at the two, see what you prefer).
If 'ana$Burn' and 'ana$Grazing' not coded as factor, you need to add this before:
ana$Burn = as.factor(ana$Burn)
ana$Grazing = as.factor(ana$Grazing)

Resources