Docker, Tensorflow, GUI, matplotlib

Lijo Jose
Analytics Vidhya
Published in
2 min readFeb 22, 2020

In the previous post, we discussed about using tensorflow on a dockerized container. With that, we can work on tensorflow environment. But, how will we do visualization of results? Copy the data to local computer and view it separetely? We can do that.. but, is there a better way? Yes. We can enable docker with GUI capabilities and display contents from docker itself.

First enable docker to use the display. We will do this by ‘xhost’ command as below

xhost +

Next, we need to run the docker with display settings.

docker run -it --gpus=all --name <name> -v <algo folder>:/algo -v <data folder>:/data --net=host --env="DISPLAY" --volume="$HOME/.Xauthority:/root/.Xauthority:rw" tensorflow/tensorflow:1.15.2-gpu-py3 /bin/bash

Inside the docker container, install python3-tk.

apt install -y python3-tk

Install matplotlib using pip

pip install matplotlib

For easy editing in python, we can have ipython. Installing ipython can be done as below

pip install ipython

The below code is taken from matplotlib website.

import numpy as np
import matplotlib.pyplot as plt


N = 5
menMeans = (20, 35, 30, 35, 27)
womenMeans = (25, 32, 34, 20, 25)
menStd = (2, 3, 4, 1, 2)
womenStd = (3, 5, 2, 3, 3)
ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars: can also be len(x) sequence

p1 = plt.bar(ind, menMeans, width, yerr=menStd)
p2 = plt.bar(ind, womenMeans, width,
bottom=menMeans, yerr=womenStd)

plt.ylabel('Scores')
plt.title('Scores by group and gender')
plt.xticks(ind, ('G1', 'G2', 'G3', 'G4', 'G5'))
plt.yticks(np.arange(0, 81, 10))
plt.legend((p1[0], p2[0]), ('Men', 'Women'))

plt.show()

Copy the above code and paste it in ipython environment and run it. You will be able to see the display on your host machine.

--

--