A33 Dot Matrix - cimat/data-visualization-patterns GitHub Wiki

#A 3.3: DOT MATRIX

Description

The dot matrix is a one-dimensional representation form for discrete quantitative data. Instead of merging the discrete values into a continuous representation such as a bar or pie chart, it retains the “countability” of the data by employing a series of proxy elements. The special view on quantifiable information the dot matrix offers to the user (he can literally count the amount of data displayed) is the result of a trade-off with the display’s accuracy. Thus, this representation pattern is only useful when an appropriate scale ratio is applied(Behrens, 2008).

Required Data

Use the dot matrix for the display of discrete quantitative values. This means that since there are only “full” elements in a matrix, no fractions or floating-point numbers can be displayed. In case you use the dot matrix for variables with continuous values, you have to put up with an approximation of your data in the representation. The conversion factor of the matrix (the amount represented by a single element) basically determines how accurate the matrix display will be (Behrens, 2008).

Usage

Create a two-dimensional matrix of elements that act as containers for your data. As for the elements’ graphical properties such as color, hue or opacity, choose unobtrusive values since the appearance of an empty container should be clearly distinguishable from a filled one. Calculate an appropriate scale for the matrix: Depending on the maximum value of your data, the number of matrix elements and the desired resolution of the visualization, assign a fixed quantitative value to each matrix element.

To display a data item in the matrix, fill the number of container elements corresponding to the data value. Make sure that you maintain a consistent “reading direction” in which to fill the matrix, for instance from left to right and from top to bottom.

Note that you can only display one dataset per matrix. If your data consists of more than one variable, use different color hues or saturation values to distinguish between the different attributes (Behrens, 2008).

Rationale

Traditional chart variations are useful to visualize proportions or continuous values. But for the task of counting things and displaying their total number (or, in more professional terms, displaying discrete values), these diagrams lack certain abilities. As Austrian sociologist Otto Neurath pointed out in his studies on visual education tools in the mid-1920s, traditional diagram forms such as bar or pie charts are not the most appropriate form to display such countable quantities. Instead, he proposed the design of symbols that represent a certain amount of a variable: For instance, the pictogram of a man stands for 10,000 unemployed workers in a social statistics chart. Being able to count the magnitude of a variable, Neurath concluded, significantly increases the experience for the observer (Behrens, 2008).

Related Patterns

  • A 3.1 Simple Bar Chart
  • A 3.2 Multiset Bar Chart
  • A 3.5 Isometric Bar Chart
  • A 4.2 Ring Chart

PYTHON IMPLEMENTATION

Data Set

from datos import data
d=data('mtcars')
d.head()

Dependences

  • Matplotlib
  • Seaborn
  • Pyqtgraph
  • Pandas

Code Example

Matplotlib

import matplotlib.patches as p
import matplotlib.pyplot as plt
import pandas
import math
from datos import data

fig=plt.figure(1, figsize=(4,4))
ax = plt.subplot(111, aspect='equal')
colors=['#FF9999', 'lightskyblue','#66FF66' ]
d=data('mtcars')
ps = pandas.Series([i for i in d.gear])
counts = ps.value_counts()

x=0.05
y=0.9
m=0
cols=1
for i in counts:
        for j in range(i):
                p1 = p.Circle((x, y), 0.05, fc=colors[m],
edgecolor='white')
                x=x+0.1
                ax.add_patch(p1)
                cols=cols+1
                if (cols>10):
                        cols=1
                        x=0.05
                        y=y-0.1
        m=m+1
x=0.05
y=0.3
m=0
for i in counts.index:
        p2 = p.Circle((x, y), 0.05, fc=colors[m], edgecolor='white')
        ax.add_patch(p2)
        plt.text(x+0.17, y-0.03, str(int(i))+" gear", ha="center",
family='sans-serif', size=11, color='b' )
        #y=y-0.13
        x=x+0.35
        m=m+1

ax.axis('off')
plt.title("Dot Matrix by Number of Gears", color='b', family='sans-
serif', size=14)
plt.show()

Seaborn

import matplotlib.patches as p
import matplotlib.pyplot as plt
import pandas
import seaborn as sns
from datos import data

sns.set(style="white")
#colors=sns.color_palette("muted", n_colors=9)
colors = sns.husl_palette(10)

fig=plt.figure(1, figsize=(4,4))
ax = plt.subplot(111, aspect='equal')
d=data('mtcars')
ps = pandas.Series([i for i in d.gear])
counts = ps.value_counts()

x=0.05
y=0.9
m=0
cols=1
for i in counts:
        for j in range(i):
                p1 = p.Circle((x, y), 0.05, fc=colors[m],
edgecolor='white')
                x=x+0.1
                ax.add_patch(p1)
                cols=cols+1
                if (cols>10):
                        cols=1
                        x=0.05
                        y=y-0.1
        m=m+4
x=0.05
y=0.3
m=0
for i in counts.index:
        p2 = p.Circle((x, y), 0.05, fc=colors[m], edgecolor='white')
        ax.add_patch(p2)
        plt.text(x+0.17, y-0.03, str(int(i))+" gear", ha="center",
family='sans-serif', size=11, color='b' )
        x=x+0.35
        m=m+4

ax.axis('off')
plt.title("Dot Matrix by Number of Gears", color='b', family='sans-
serif', size=14)
plt.show()

Pyqtgraph

import pyqtgraph as pg
from  PyQt4  import  QtGui
import pandas
from datos import data

win = pg.GraphicsWindow("Dot Matrix ")
win.resize(300,300)
v = win.addViewBox()
v.setAspectLocked()
text = pg.TextItem("Dot Matrix by Number of Gears ",
anchor=(-0.1,22.5), color='w')
v.addItem(text)
d=data('mtcars')
ps = pandas.Series([i for i in d.gear])
counts = ps.value_counts()
position=[ (-0.5,15), (-2,15), (-3.5,15)]
colours = [QtGui.QColor('springgreen'), QtGui.QColor('lightskyblue'),
QtGui.QColor('lightcoral')]

x=0.0
y=1.0
m=0
cols=1

for i in counts:
        for j in range(i):
                ellipse = QtGui.QGraphicsEllipseItem(x,y,0.05,0.05)
                ellipse.setBrush(colours[m])
                v.addItem(ellipse)
                x=x+0.05
                cols=cols+1
                if (cols>10):
                        cols=1
                        x=0.0
                        y=y-0.05
        m=m+1

x=0
y=0.7
m=0
for i in counts.index:
        ellipse = QtGui.QGraphicsEllipseItem(x,y,0.05,0.05)
        ellipse.setBrush(colours[m])
        v.addItem(ellipse)
        x=x+0.15
        m=m+1
j=0
for x in counts.index:
    text = pg.TextItem(str(int(x))+" gear", anchor=(position[j]),
color='w')
    v.addItem(text)
    j+=1

if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore,
'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

References

R IMPLEMENTATION

Data Set

Dependences

  • lattice
  • ggplot2

Code Example

Graphics

x <- mtcars 
x$cyl <- factor(x$cyl)
x$color[x$cyl==4] <- "red"
x$color[x$cyl==6] <- "blue"
x$color[x$cyl==8] <- "darkgreen"    
dotchart(x$mpg,labels=row.names(mtcars),ylim=c(0, 1.00), pch=16,
         main="Gas Milage for Car Models\ngrouped by cylinder",
         xlab="Miles per Gallon", gcolor="black", color=x$color)
legend("bottomright", legend = c("4", "6", "8"),
       col = c("red","blue","darkgreen"), pch = 16, title="Cylindres")

Lattice

library(lattice)
dotplot(rownames(mtcars) ~ mpg, data=mtcars, groups=mtcars$cyl, pch=16, main="Gas Milage for Car Models\ngrouped by cylinder",     xlab="Miles per Gallon", auto.key=list(space="top", columns=3, title="Cylindres", cex.title=1))

ggplot2

library(ggplot2)
g<-ggplot(mtcars, aes(x = mpg, fill = factor(cyl)))
g+geom_dotplot(stackgroups = TRUE, binwidth = 1.5, binpositions = "all", method="histodot")

References

⚠️ **GitHub.com Fallback** ⚠️