Visualization Tools

From Self-Organization Wiki
Jump to: navigation, search

Box plot

A box plot is a good way to depict results where measurements have been taken over another parameter, for example the fitness of several independent runs of an evolutionary algorithm over the number of generations.

Box plots with R

The statistic software R can be installed quickly, e.g., on Debian/Ubuntu Linux by "apt-get install r-base". The data must be given in a tab-seperated file, where each column comprises the measurements for one box.

x <- read.table("datafilename.tsv")
postscript("outputfilename.ps")
boxplot(x)
dev.off()

The postscript command redirects the output to a postscript file. The last command flushes the output and closes the file.

Data manipulation with R

Sometimes it is necessary to postprocess data before plotting it.

x <- read.table("datafilename.tsv")

storing every nth column with n=3

y <- x[seq(1,ncol(x),3)]

storing every nth line with n=3, starting from first.

z <- x[(seq(1,nrow(x),3)), ]

Example script that selects every 6th column for data and plots it into a pdf file

y <- read.table("rboxplot.txt", col.names = seq(1,500,5), check.names=F)
z <- y[(seq(1,nrow(y),6)), ]
a <- z[seq(1,ncol(z),3)]
pdf("boxplot_fitness.pdf")
boxplot(a, xlab="Generation", ylab="Fitness")
dev.off()

Box plots with Python

This code snippet below shows how to load data from a text file (one-dimensional array), convert in float values and generate a Box plot within the converted data. Note: Keep your text- and python file in the same path.

import matplotlib.pyplot as plt

f = open("my_file.txt", "r")
data = []
for line in f:
   data.append(float(line))
f.close()

plt.boxplot(data)
plt.xlabel('My_x_values')
plt.ylabel('My_y_values')
plt.title('My Box plot')
plt.show()

If you want to load two Box plots in one plot, use the following snippet:

import matplotlib.pyplot as plt

f1 = open("my_file1.txt", "r")
data1 = []
for line in f1:
    data1.append(float(line))
f1.close()

f2 = open("my_file2.txt", "r")
data2 = []
for line in f2:
    data2.append(float(line))
f2.close()

data_to_plot = [data1, data2]

fig, ax = plt.subplots()
ax.boxplot(data_to_plot)

plt.xlabel('My_x_values')
plt.ylabel('My_y_values')
plt.title('My Box plots')
plt.show()

Hint: Works analogously with several files for Box plots.