How to have a legend for colored edges in Julia plots? - colors

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]
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, edgelabel=1:78)
Which produces
I would like to add a legend on top left (or top right) so that I can easily tell what are the meaning of the two types of edges. Additionally, if there is also a way to legend what are the two types of nodes that would be a great plus!
For instance, grey edges would be named: "regular edges" and green edges would be "backup edges". Similarly, grey nodes would be "regular nodes" and orange nodes would be "backup nodes"!
I could not find a keyword in gplot for what I am searching!

Related

Separating the coordinates of two or more polygons

I have a list of coordinates that represents two (or potentially more) different polygon corner point coordinates (x,y). How can I separate this list into two (or more) separate lists of separate polygon corner point coordinates? That is, the list above includes coordinates for the red and yellow polygon in the image below. How can I separate between the two?
These polygons do not overlap/intersect.
Example:
Separating this
coordslist _of_two_polygons= [(634.196912844544, 573.5518673990564), (634.72, 596.06), (634.04, 612.57), (628.14, 636.73), (619.82, 651.76), (614.85, 661.02), (612.03, 660.08), (613.91, 653.64), (619.01, 639.95), (621.7, 631.76), (623.98, 622.64), (626.53, 608.54), (624.52, 587.34), (624.92, 585.99), (623.5164081989366, 571.5658544699229), (621.28, 571.15), (622.6759134800419, 565.013087688352), (638.1176399986085, 565.552256750043), (637.67, 570.49), (635.37, 573.77), (634.196912844544, 573.5518673990564), (605.9745784899422, 564.4299376691641), (605.05, 565.73), (602.1, 572.04), (598.34, 579.28), (594.99, 583.18), (590.02, 581.3), (593.24, 577.27), (596.5165952802347, 563.3459954757998), (598.53, 564.17), (605.9745784899422, 564.4299376691641), (634.196912844544, 573.5518673990564)]
into this
coords_polygon1 = [(..),..,(..)]
coords_polygon1 = [(..),..,(..)]
A visual example to explain a bit: (not exactly the coordinates above, but the logic is the same)

How to scale a barplot in tikz without scaling the text?

I have a barplot in latex in tikz, that I want to scale (reduce width, increase length), but without the text having the same effect.
\begin{figure}
\centering
\resizebox{0.8\textwidth}{1.3\textwidth}{
\begin{tikzpicture}
\pgfplotsset{every tick label/.append style={font=\tiny}}
\begin{axis}[ xmajorgrids=true,
xbar, xmin=0,
xlabel={Gini Coefficient, OECD Countries \%},
symbolic y coords={{Australia},{Austria},{Belgium},{Canada},{Chile},{Costa Rica},{Czech Republic},{Denmark},{Estonia},{Finland},{France},{Germany},{Greece},{Hungary},{Iceland},{Ireland},{Israel},{Italy},{Japan},{Korea},{Latvia},{Lithuania},{Luxembourg},{Mexico},{Netherlands},{New Zealand},{Norway},{Poland},{Portugal},{Slovak Republic},{Slovenia},{South Africa},{Spain},{Sweden},{Switzerland},{Turkey},{United Kingdom},{United States}},
ytick=data,
nodes near coords, nodes near coords align={horizontal},
ytick=data,
]
\addplot[fill=blue!90,draw=black!70,tickwidth = 0pt,bar width=4pt,label style={font=\small}, tick label style={font=\small}] coordinates {(0.33,{Australia}) (0.284,{Austria}) (0.266,{Belgium}) (0.307,{Canada}) (0.454,{Chile}) (0.48,{Costa Rica}) (0.253,{Czech Republic}) (0.263,{Denmark}) (0.314,{Estonia}) (0.266,{Finland}) (0.291,{France}) (0.294,{Germany}) (0.333,{Greece}) (0.288,{Hungary}) (0.255,{Iceland}) (0.297,{Ireland}) (0.344,{Israel}) (0.328,{Italy}) (0.339,{Japan}) (0.355,{Korea}) (0.346,{Latvia}) (0.378,{Lithuania}) (0.304,{Luxembourg}) (0.458,{Mexico}) (0.285,{Netherlands}) (0.349,{New Zealand}) (0.262,{Norway}) (0.284,{Poland}) (0.331,{Portugal}) (0.241,{Slovak Republic}) (0.244,{Slovenia}) (0.62,{South Africa}) (0.341,{Spain}) (0.282,{Sweden}) (0.296,{Switzerland}) (0.404,{Turkey}) (0.351,{United Kingdom}) (0.391,{United States})};
\end{axis}
\end{tikzpicture}
}
\caption{\small{Gini coefficient, OECD countries, 2017 or latest available, source: \url{https://data.oecd.org/inequality/income-inequality.htm}}}
\label{fig:giniOECD}
\end{figure}
I would like the bar charts a bit further apart to each other, the blank areas on top and at the end to be removed, the markers (text within the chart that show the values) to be smaller and all the text to be at normal scale, not skewed like that.
Never ever use \resizebox (or \scalebox) for things that contain text.
to set the height and the width of a pgfplot, the height and width keywords can be used
don't put formatting instructions in arguments of macros like \caption. If you want the caption in another font size, you can adjust it with the help of the caption package
\documentclass{article}
\usepackage{pgfplots}
\usepackage{hyperref}
\usepackage[font=small]{caption}
\begin{document}
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\pgfplotsset{%
width=.8\textwidth,
height=1.5\textwidth
}
\begin{axis}[
xmajorgrids=true,
xbar,
xmin=0,
xlabel={Gini Coefficient, OECD Countries \%},
symbolic y coords={{Australia}, {Austria}, {Belgium}, {Canada}, {Chile}, {Costa Rica}, {Czech Republic}, {Denmark}, {Estonia}, {Finland}, {France}, {Germany}, {Greece}, {Hungary}, {Iceland}, {Ireland}, {Israel}, {Italy}, {Japan}, {Korea}, {Latvia}, {Lithuania}, {Luxembourg}, {Mexico}, {Netherlands}, {New Zealand}, {Norway}, {Poland}, {Portugal}, {Slovak Republic}, {Slovenia}, {South Africa}, {Spain}, {Sweden}, {Switzerland}, {Turkey}, {United Kingdom}, {United States}},
ytick=data,
nodes near coords,
nodes near coords align={horizontal},
enlarge y limits=0.02,
]
\addplot[fill=blue!90,draw=black!70,tickwidth = 0pt,bar width=4pt,label style={font=\small}, tick label style={font=\small}] coordinates {(0.33,{Australia}) (0.284,{Austria}) (0.266,{Belgium}) (0.307,{Canada}) (0.454,{Chile}) (0.48,{Costa Rica}) (0.253,{Czech Republic}) (0.263,{Denmark}) (0.314,{Estonia}) (0.266,{Finland}) (0.291,{France}) (0.294,{Germany}) (0.333,{Greece}) (0.288,{Hungary}) (0.255,{Iceland}) (0.297,{Ireland}) (0.344,{Israel}) (0.328,{Italy}) (0.339,{Japan}) (0.355,{Korea}) (0.346,{Latvia}) (0.378,{Lithuania}) (0.304,{Luxembourg}) (0.458,{Mexico}) (0.285,{Netherlands}) (0.349,{New Zealand}) (0.262,{Norway}) (0.284,{Poland}) (0.331,{Portugal}) (0.241,{Slovak Republic}) (0.244,{Slovenia}) (0.62,{South Africa}) (0.341,{Spain}) (0.282,{Sweden}) (0.296,{Switzerland}) (0.404,{Turkey}) (0.351,{United Kingdom}) (0.391,{United States})};
\end{axis}
\end{tikzpicture}
\caption{Gini coefficient, OECD countries, 2017 or latest available, source: \url{https://data.oecd.org/inequality/income-inequality.htm}}
\label{fig:giniOECD}
\end{figure}
\end{document}

Change color and legend of plotLearnerPrediction ggplot2 object

I've been producing a number of nice plots with the plotLearnerPrediction function in the mlr package for R. They look like this. From looking into the source code of the plotLearnerPrediction function it looks like the color surfaces are made with geom_tile.
A plot can for example be made by:
library(mlr)
data(iris)
#make a learner
lrn <- "classif.qda"
#make a task
my.task <- makeClassifTask(data = iris, target = "Species")
#make plot
plotLearnerPrediction(learner = lrn, task = my.task)
Now I wish to change the colors, using another red, blue and green tone to match those of some other plots that I've made for a project. for this I tried scale_fill_continuous and scale_fill_manual without any luck (Error: Discrete value supplied to continuous scale) I also wish to change the legend title and the labels for each legend entry (Which I tried giving appropriate parameters to the above scale_fill's). There's a lot of info out there on how to set the geom_tile colours when producing the plot, but I haven't found any info on how to do this post-production (i.e. in somebody else's plot object). Any help would be much appreciated.
When you look into the source code you see how the plot is generated and then you can see which scale has to be overwritten or set.
In this example it's fairly easy:
g = plotLearnerPrediction(learner = lrn, task = my.task)
library(ggplot2)
g + scale_fill_manual(values = c(setosa = "yellow", versicolor = "blue", virginica = "red"))

how to control transparency of ppp density plot

I am trying to use the layered methods to overlay few spatstat spatial objects. All these objects are for the same window. I have an im layer (density) from a ppp. I want to make this layer a bit transparent in order to have a better visibility of the other objects in the layered object.
How can I control the transparency of this density plot (im)? Is there something like alpha or transparency parameter for the plot.im ?
UPDATE:
library(spatstat)
pipes=simplenet
plot(pipes)
point_net = as.ppp(runifpoint(10, win = Window(pipes)))
point_surface = density(point_net)
plot(point_surface)
layers= layered(point_surface, point_net, pipes)
plot(layers)
Here , I have plotted 3 layers. As you can see the density plot has very dark blues and reds. Yes, I can plot lines and points with different colours to make them visible, but it would nice to do simple stacked line, point plots and add a little bit of transparency to the density (im) plots.
The purpose is just to avoid complex customized plot colours and to explain to colleagues.
thank you.
First the commands from the original post:
library(spatstat)
pipes=simplenet
point_net = as.ppp(runifpoint(10, win = Window(pipes)))
point_surface = density(point_net)
layers= layered(point_surface, point_net, pipes)
plot(layers)
You need to provide a different colourmap to plot.im. There are two
ways you can do this:
Plot each layer individually using add = TRUE for subsequent
layers and provide the colour map when you plot the im object.
Pass a list of plot arguments when you plot the layered object you
have created above.
I find the first option easier for illustration, so I will do that
first. The default colourmap of spatstat is the 29th Kovesi colour
sequence (?Kovesi for more details on these sequences):
def_col <- Kovesi$values[[29]]
head(def_col)
#> [1] "#000C7D" "#000D7E" "#000D80" "#000E81" "#000E83" "#000E85"
To add transparency you can use to.transparent with your choice of
fraction for more/less transparency:
def_col_trans <- to.transparent(def_col, fraction = 0.7)
head(def_col_trans)
#> [1] "#000C7DB3" "#000D7EB3" "#000D80B3" "#000E81B3" "#000E83B3" "#000E85B3"
Now you just need to use this as your colourmap:
plot(point_surface, col = def_col_trans)
plot(point_net, add = TRUE)
plot(pipes, add = TRUE)
To do it with the layered object you have to make a list of plot
argument lists (containing NULL if you don't have additional
arguments):
layer_args <- list(list(col = def_col_trans),
list(NULL),
list(NULL))
plot(layers, plotargs = layer_args)

Formatted String from List Psychopy

My task is a variation of a multiple object tracking task. There are 7 circles on the screen. It randomly selects 3 circles to change the color (red, green, blue) briefly to indicate to the participant to track these circles. After the color change, all the circles will change to the same color and the circles will move for a period of time. When the circles stop moving, a response prompt will appear, where the participant is to select one of the three colored circles ('select the red/green/blue circle'). I am having difficulty inserting which color circle to select into the formatted string. I keep getting the error message: unsupported operand type(s) for %: 'TextStim' and 'list'
I'm not sure if I need to or how to convert these lists, so any help would be much appreciated!
n_targets = 7 #seven locations
circles = [] #setting up the circle stimuli
for i in range(n_targets):
tmp = visual.Circle(win,radius = 27,units = 'pix',edges = 32,fillColor='white',lineColor = 'black',lineWidth = 1, pos=(posx[i],posy[i]))
circles.append(tmp)
cols = ['blue','red','green'] #3 colors the circles will change to
targets = random.sample(circles,3) #randomly select 3 of the 7 circles
TrialTarget = random.sample(targets, 1) #select 1 of the 3 circles to be the target for the trial
#code for movement would go here (skipping since it is not relevant)
#at end of trial, response prompt appears and ask user to select target and is where error occurs
ResponsePrompt = visual.TextStim(win, text = "Select the %s circle") %TrialTarget
In this line, you are trying to create a formatted string from a TextStim object and a Circle stimulus object rather than a string object and another string object:
ResponsePrompt = visual.TextStim(win, text = "Select the %s circle") %TrialTarget
i.e. ResponsePrompt is clearly a visual.TextStim, as you are creating it as one, and I think TrialTarget is a visual.Circle stimulus, as you randomly sample it from a list of Circles.
I'm guessing that you actually want to incorporate the colour label into the prompt text. So to fix both problems (the type incompatibility and the formatting syntax), you need to actually get one of the elements of cols, called say trialColour, and use something like this:
ResponsePrompt = visual.TextStim(win, text = "Select the %s circle" % trialColour)
i.e. here trialColour is actually a string, and the formatting operation is brought inside the brackets so it applies directly to the text string "Select the %s circle"
That should hopefully fix your immediate problem. You might also want to investigate using random.shuffle() to shuffle lists in place instead of random.sample().

Resources