Plotting m u ltiple graphs IN TR OD U C TION TO DATA VISU AL IZATION IN P YTH ON Br y an Van de Ven Core De v eloper of Bokeh
Strategies Plo � ing man y graphs on common a x es Creating a x es w ithin a � g u re Creating s u bplots w ithin a � g u re INTRODUCTION TO DATA VISUALIZATION IN PYTHON
Graphs on common a x es import matplotlib.pyplot as plt plt.plot(t, temperature, 'r') # Appears on same axes plt.plot(t, dewpoint, 'b') plt.xlabel('Date') plt.title('Temperature & Dew Point') # Renders plot objects to screen plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON
Using a x es () plt.axes([0.05,0.05,0.425,0.9]) plt.plot(t, temperature, 'r') plt.xlabel('Date') plt.title('Temperature') plt.axes([0.525,0.05,0.425,0.9]) plt.plot(t, dewpoint, 'b') plt.xlabel('Date') plt.title('Dew Point') plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON
The a x es () command S y nta x: axes( [x_lo, y_lo, width, height] ) Units bet w een 0 and 1 (� g u re dimensions ) INTRODUCTION TO DATA VISUALIZATION IN PYTHON
Using s u bplot () INTRODUCTION TO DATA VISUALIZATION IN PYTHON
Using s u bplot () plt.subplot(2, 1, 1) plt.plot(t, temperature, 'r') plt.xlabel('Date') plt.title('Temperature') plt.subplot(2, 1, 2) plt.plot(t, dewpoint, 'b') plt.xlabel('Date') plt.title('Dew Point') plt.tight_layout() plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON
The s u bplot () command S y nta x: subplot(nrows, ncols, nsubplot) S u bplot ordering : Ro w-w ise from top le � Inde x ed from 1 INTRODUCTION TO DATA VISUALIZATION IN PYTHON
Let ' s practice ! IN TR OD U C TION TO DATA VISU AL IZATION IN P YTH ON
C u stomi z ing a x es IN TR OD U C TION TO DATA VISU AL IZATION IN P YTH ON Br y an Van de Ven Core De v eloper of Bokeh
Controlling a x is e x tents axis([xmin, xmax, ymin, ymax]) sets a x is e x tents Control o v er indi v id u al a x is e x tents xlim([xmin, xmax]) ylim([ymin, ymax]) Can u se t u ples , lists for e x tents e . g ., xlim((-2, 3)) w orks e . g ., xlim([-2, 3]) w orks also INTRODUCTION TO DATA VISUALIZATION IN PYTHON
GDP o v er time import matplotlib.pyplot as plt plt.plot(yr, gdp) plt.xlabel('Year') plt.ylabel('Billions of Dollars') plt.title('US Gross Domestic Product') plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON
Using x lim () plt.plot(yr, gdp) plt.xlabel('Year') plt.ylabel('Billions of Dollars') plt.title('US Gross Domestic Product') plt.xlim((1947, 1957)) plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON
Using x lim () and y lim () plt.plot(yr, gdp) plt.xlabel('Year') plt.ylabel('Billions of Dollars') plt.title('US Gross Domestic Product') plt.xlim((1947, 1957)) plt.ylim((0, 1000)) plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON
Using a x is () plt.plot(yr, gdp) plt.xlabel('Year') plt.ylabel('Billions of Dollars') plt.title('US Gross Domestic Product') plt.axis((1947, 1957, 0, 600)) plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON
Other a x is () options In v ocation Res u lt axis('off') t u rns o � a x is lines , labels axis('equal') eq u al scaling on x and y a x es axis('square') forces sq u are plot axis('tight') sets xlim , ylim to sho w all data INTRODUCTION TO DATA VISUALIZATION IN PYTHON
Using a x is (' eq u al ') INTRODUCTION TO DATA VISUALIZATION IN PYTHON
Using a x is (' eq u al ') plt.subplot(2, 1, 1) plt.plot(x, y, 'red') plt.title('default axis') plt.subplot(2, 1, 2) plt.plot(x, y, 'red') plt.axis('equal') plt.title('axis equal') plt.tight_layout() plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON
Let ' s practice ! IN TR OD U C TION TO DATA VISU AL IZATION IN P YTH ON
Legends , annotations , and st y les IN TR OD U C TION TO DATA VISU AL IZATION IN P YTH ON Br y an Van de Ven Core De v eloper of Bokeh
Legends Legend pro v ide labels for o v erlaid points and c u r v es INTRODUCTION TO DATA VISUALIZATION IN PYTHON
Using legend () import matplotlib.pyplot as plt plt.scatter(setosa_len, setosa_wid, marker='o', color='red', label='setosa') plt.scatter(versicolor_len, versicolor_wid, marker='o', color='green', label='versicolor') plt.scatter(virginica_len, virginica_wid, marker='o', color='blue', label='virginica') plt.legend(loc='upper right') plt.title('Iris data') plt.xlabel('sepal length (cm)') plt.ylabel('sepal width (cm)') plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON
Legend locations string code string code 'upper left' 'upper right' 2 1 'center left' 'center right' 6 7 'lower left' 'lower right' 3 4 'upper center' 'right' 9 5 'center' 'best' 10 0 'lower center' 8 INTRODUCTION TO DATA VISUALIZATION IN PYTHON
Plot annotations Te x t labels and arro w s u sing annotate() method Fle x ible speci � cation of coordinates Ke yw ord arrowprops : dict of arro w properties width color etc . INTRODUCTION TO DATA VISUALIZATION IN PYTHON
Using annotate () for te x t plt.annotate('setosa', xy=(5.0, 3.5)) plt.annotate('virginica', xy=(7.25, 3.5)) plt.annotate('versicolor', xy=(5.0, 2.0)) plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON
Options for annotate () option description s te x t of label xy coordinates to annotate xytext coordinates of label arrowprops controls dra w ing of arro w INTRODUCTION TO DATA VISUALIZATION IN PYTHON
Using annotate () for arro w s INTRODUCTION TO DATA VISUALIZATION IN PYTHON
Using annotate () for arro w s plt.annotate('setosa', xy=(5.0, 3.5), xytext=(4.25, 4.0), arrowprops={'color':'red'}) plt.annotate('virginica', xy=(7.2, 3.6), xytext=(6.5, 4.0), arrowprops={'color':'blue'}) plt.annotate('versicolor', xy=(5.05, 1.95), xytext=(5.5, 1.75), arrowprops={'color':'green'}) plt.show() INTRODUCTION TO DATA VISUALIZATION IN PYTHON
Working w ith plot st y les St y le sheets in matplotlib Defa u lts for lines , points , backgro u nds , etc . S w itch st y les globall y w ith plt.style.use() plt.style.available : list of st y les INTRODUCTION TO DATA VISUALIZATION IN PYTHON
ggplot st y le import matplotlib.pyplot as plt plt.style.use('ggplot') INTRODUCTION TO DATA VISUALIZATION IN PYTHON
fi v ethirt y eight st y le import matplotlib.pyplot as plt plt.style.use('fivethirtyeight') INTRODUCTION TO DATA VISUALIZATION IN PYTHON
Let ' s practice ! IN TR OD U C TION TO DATA VISU AL IZATION IN P YTH ON
Recommend
More recommend