A41 Simple Pie Chart - cimat/data-visualization-patterns GitHub Wiki
A pie chart is a circular object divided into multiple polar segments. It displays the relative magnitude of several quantitative values compared to each other, or, in other words, the distribution of several values that belong to the same dataset. The full circle represents the total magnitude of this dataset, equal to 100 percent, while each segment stands for the magnitude of one particular variable. Segment area, arc length and arc angle of each segment are proportional to the value the segment represents. The segments of a pie chart are usually labeled with percentage numbers rather than total values (although they can feature both for the sake of understanding)(Behrens, 2008).
Use a pie chart to display a set of categorical items. Each item has a magnitude expressed as a proportional value against the total size of the dataset: The sum of all items’ magnitudes is 100 percent(Behrens, 2008).
Draw a circle. Divide it into slice-shaped segments between the center and the circle arc, one segment per data item. Assign one specific data item to each segment, and adjust the size of the segments by making each arc (or each arc angle) proportional to the percentage value of the respective item from your dataset. Label the chart segments accordingly (Behrens, 2008).
Pie charts give the reader a quick idea of the proportional distribution of data. The association between data and representation is evident: The bigger the piece, the larger the data chunk compared to the other ones. As pie charts belong to the most common infographics used in popular media (e.g. to display election results) and build upon a strong metaphor (the cake cut into pieces) they are a reliable way of communicating information when several data items add to a whole (Behrens, 2008).
- A 3.4 Stacked Bar Chart
- A 4.2 Ring Chart
- A 5.1 Sankey Diagram
from datos import data
d=data('mtcars')
t1 = d.pivot_table( values = 'carb',index=['cyl'], columns = ['gear'],
aggfunc =len)
- Matplotlib
- Seaborn
- Pyqtgraph
- Pandas
import matplotlib.pyplot as plt
from datos import data
import pandas
colors = ['lightcoral', 'lightskyblue','yellowgreen']
d=data('mtcars')
ps = pandas.Series([i for i in d.cyl])
c = ps.value_counts()
plt.pie(c, labels=c.index, colors=colors, autopct='%1.1f%%',
shadow=True, startangle=0)
plt.axis('equal')
plt.title('Car Distribution by Cylindres', size=18)
plt.show()
import matplotlib.pyplot as plt
import seaborn as sns
from datos import data
import pandas
sns.set(style="white")
d=data('mtcars')
colors = sns.husl_palette(3)
d=data('mtcars')
ps = pandas.Series([i for i in d.cyl])
c = ps.value_counts()
plt.pie(c, labels=c.index, colors=colors, autopct='%1.1f%%',
shadow=True, startangle=0)
plt.title('Car Distribution by Cylindres', size=18)
plt.show()
import pyqtgraph as pg
import random
import numpy
from PyQt4 import QtGui
from datos import data
win = pg.GraphicsWindow("Car Distribution by Cylindres")
view = win.addViewBox()
view.setAspectLocked()
d=data('mtcars')
t1 = d.pivot_table( values = 'carb',index=['gear'], columns = ['cyl'],
aggfunc = len)
colours = [QtGui.QColor('springgreen'), QtGui.QColor('lightskyblue'),
QtGui.QColor('lightcoral')]
r=[0,0,0]
r[0]= sum(t1[4])/len(d)
r[1]=sum(t1[6]) /len (d)
x=t1[8]
x = x[~numpy.isnan(x)]
r[2]= sum(x) / len(d)
count1=0
labels=['','','']
labels[0]='4 Cyl \n'+str(r[0]*100)+ '%'
labels[1]='6 Cyl \n'+str(r[1]*100) + '%'
labels[2]='8 Cyl \n'+str(r[2]*100) +'%'
position=[ (-1,6), (-5,4), (-5,10)
set_angle = 0
for x in r:
angle = x*360*16
ellipse = QtGui.QGraphicsEllipseItem(0,0,100,100)
ellipse.setPos(0,0)
ellipse.setStartAngle(set_angle)
ellipse.setSpanAngle(angle)
ellipse.setBrush(colours[count1])
set_angle = set_angle + angle
ellipse.setPen(pg.mkPen(5))
view.addItem(ellipse)
count1 +=1
j=0
for x in position:
text = pg.TextItem(labels[j], anchor=(position[j]), color=(0,0,0))
view.addItem(text)
j+=1
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore,
'PYQT_VERSION'):
QtGui.QApplication.instance().exec
table(mtcars$cyl)
##
## 4 6 8
## 11 7 14
- lattice
- ggplot2
t<-table(mtcars$cyl)
colores<-c("pink","green","skyblue")
pct <- round(t/sum(t) * 100)
lbl<-paste(c("4 Cyl","6 Cyl","8 Cyl"), pct, sep =" ")
lbl<-paste(lbl, "%", sep =" ")
pie(x=t,labels=lbl, col = colores, radius = 1, main="Proportion Cylindres in a Car Distribution", cex=1)
library(latticeExtra)
panel.piechart <- function(x, y, labels = as.character(y),
edges = 200, radius = 0.8, clockwise = FALSE,
init.angle = if(clockwise) 90 else 0,
density = NULL, angle = 45,
col = superpose.polygon$col,
border = superpose.polygon$border,
lty = superpose.polygon$lty, ...)
{
stopifnot(require("gridBase"))
superpose.polygon <- trellis.par.get("superpose.polygon")
opar <- par(no.readonly = TRUE)
on.exit(par(opar))
if (panel.number() > 1) par(new = TRUE)
par(fig = gridFIG(), omi = c(0, 0, 0, 0), mai = c(0, 0, 0, 0))
pie(as.numeric(x), labels = labels, edges = edges, radius = radius,
clockwise = clockwise, init.angle = init.angle, angle = angle,
density = density, col = col, border = border, lty = lty)
}
piechart <- function(x, data = NULL, panel = "panel.piechart", ...)
{
ocall <- sys.call(sys.parent())
ocall[[1]] <- quote(piechart)
ccall <- match.call()
ccall$data <- data
ccall$panel <- panel
ccall$default.scales <- list(draw = FALSE)
ccall[[1]] <- quote(lattice::barchart)
ans <- eval.parent(ccall)
ans$call <- ocall
ans
}
t<-table(mtcars$cyl)
colores<-c("pink","green","skyblue")
pct <- round(t/sum(t) * 100)
lbl<-paste(c("4 Cyl","6 Cyl","8 Cyl"), pct, sep =" ")
lbl<-paste(lbl, "%", sep =" ")
par(new = TRUE)
piechart(t, labels=lbl, main="Proportion Cylindres ",col = colores, xlab="")
library(ggplot2)
t<-table(mtcars$cyl)
x<-as.data.frame(t)
colnames(x)<-c("Cylindres", "Frequency")
bp <- ggplot(x, aes(x ="",y=Frequency, fill = Cylindres)) +
geom_bar(width = 1, stat = "identity") +labs (title="Proportion Cylindres in a Car Distribution")
pie <-bp+coord_polar("y", start=0)
pie + geom_text(aes(y = Frequency/3 + c(0, cumsum(Frequency)[-length(Frequency)]),
label = paste(round(Frequency/sum(Frequency) * 100), " %")), size=5)