Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Python

This document is a comprehensive Python 3 cheat sheet covering various data types, operations, control structures, and functions. It includes information on base types, container types, boolean logic, and file operations, along with examples of syntax and usage. The cheat sheet serves as a quick reference for Python programming concepts and practices.

Uploaded by

ayoubtaoussi2022
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Python

This document is a comprehensive Python 3 cheat sheet covering various data types, operations, control structures, and functions. It includes information on base types, container types, boolean logic, and file operations, along with examples of syntax and usage. The cheat sheet serves as a quick reference for Python programming concepts and practices.

Uploaded by

ayoubtaoussi2022
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

©2012-2015 - Laurent Pointal Mémento v2.0.

6
License Creative Commons Attribution 4 Python 3 Cheat Sheet Latest version on :
https://perso.limsi.fr/pointal/python:memento
integer, float, boolean, string, bytes Base Types ◾ ordered sequences, fast index access, repeatable values Container Types
int 783 0 -192 0b010 0o642 0xF3 list [1,5,9] ["x",11,8.9] ["mot"] []
zero binary octal hexa tuple (1,5,9) 11,"y",7.4 ("mot",) ()
float 9.23 0.0 -1.7e-6
-6 Non modifiable values (immutables) ☝ expression with only comas →tuple
bool True False ×10
""
str bytes (ordered sequences of chars / bytes)
str "One\nTwo" Multiline string: b""
escaped new line """X\tY\tZ ◾ key containers, no a priori order, fast key access, each key is unique
'I\'m' 1\t2\t3""" dictionary dict {"key":"value"} dict(a=3,b=4,k="v") {}
escaped ' escaped tab (key/value associations) {1:"one",3:"three",2:"two",3.14:"π"}
bytes b"toto\xfe\775" collection set {"key1","key2"} {1,9,3,0} set()
hexadecimal octal ☝ immutables ☝ keys=hashable values (base types, immutables…) frozenset immutable set empty

for variables, functions, Identifiers type(expression) Conversions


modules, classes… names
int("15") → 15
int("3f",16) → 63 nd
can specify integer number base in 2 parameter
a…zA…Z_ followed by a…zA…Z_0…9
◽ diacritics allowed but should be avoided int(15.56) → 15 truncate decimal part
◽ language keywords forbidden float("-11.24e8") → -1124000000.0
◽ lower/UPPER case discrimination round(15.56,1)→ 15.6 rounding to 1 decimal (0 decimal → integer number)
☺ a toto x7 y_max BigOne bool(x) False for null x, empty container x , None or False x ; True for other x
☹ 8y and for str(x)→ "…" representation string of x for display (cf. formatting on the back)
chr(64)→'@' ord('@')→64 code ↔ char
= Variables assignment
repr(x)→ "…" literal representation string of x
☝ assignment ⇔ binding of a name with a value
1) evaluation of right side expression value bytes([72,9,64]) → b'H\t@'
2) assignment in order with left side names list("abc") → ['a','b','c']
x=1.2+8+sin(y) dict([(3,"three"),(1,"one")]) → {1:'one',3:'three'}
a=b=c=0 assignment to same value set(["one","two"]) → {'one','two'}
y,z,r=9.2,-7.6,0 multiple assignments separator str and sequence of str → assembled str
a,b=b,a values swap ':'.join(['toto','12','pswd']) → 'toto:12:pswd'
a,*b=seq unpacking of sequence in str splitted on whitespaces → list of str
*a,b=seq item and list "words with spaces".split() → ['words','with','spaces']
and str splitted on separator str → list of str
x+=3 increment ⇔ x=x+3 *=
x-=2 decrement ⇔ x=x-2 /= "1,4,8,2".split(",") → ['1','4','8','2']
x=None « undefined » constant value %= sequence of one type → list of another type (via list comprehension)
del x remove name x … [int(x) for x in ('1','29','-3')] → [1,29,-3]
for lists, tuples, strings, bytes… Sequence Containers Indexing
negative index -5 -4 -3 -2 -1 Items count Individual access to items via lst[index]
positive index 0 1 2 3 4 len(lst)→5 lst[0]→10 ⇒ first one lst[1]→20
lst=[10, 20, 30, 40, 50] lst[-1]→50 ⇒ last one lst[-2]→40
positive slice 0 1 2 3 4 5 ☝ index from 0
On mutable sequences (list), remove with
negative slice -5 -4 -3 -2 -1 (here from 0 to 4)
del lst[3] and modify with assignment
lst[4]=25
Access to sub-sequences via lst[start slice:end slice:step]
lst[:-1]→[10,20,30,40] lst[::-1]→[50,40,30,20,10] lst[1:3]→[20,30] lst[:3]→[10,20,30]
lst[1:-1]→[20,30,40] lst[::-2]→[50,30,10] lst[-3:-1]→[30,40] lst[3:]→[40,50]
lst[::2]→[10,30,50] lst[:]→[10,20,30,40,50] shallow copy of sequence
Missing slice indication → from start / up to end.
On mutable sequences (list), remove with del lst[3:5] and modify with assignment lst[1:4]=[15,25]

Boolean Logic Statements Blocks Modules/Names Imports


module truc⇔file truc.py
Comparisons : < > <= >= == != from monmod import nom1,nom2 as fct
(boolean results) ≤ ≥ = ≠ parent statement: →direct access to names, renaming with as
a and b logical and both simulta- statement block 1… import monmod →access via monmod.nom1 …
indentation !

-neously ⁝ ☝ modules and packages searched in python path (cf sys.path)


a or b logical or one or other parent statement: statement block executed only
or both Conditional Statement
statement block2… if a condition is true
☝ pitfall : and and or return value of a or ⁝ yes no yes
of b (under shortcut evaluation). if logical condition: ? ?
⇒ ensure that a and b are booleans. no
not a logical not
next statement after block 1 statements block
True Can go with several elif, elif... and only one
True and False constants ☝ configure editor to insert 4 spaces in if age<=18:
False final else. Only the block of first true
place of an indentation tab. state="Kid"
condition is executed. elif age>65:
☝ floating numbers… approximated values Maths
angles in radians ☝ with a var x: state="Retired"
if bool(x)==True: ⇔ if x: else:
Operators: + - * / // % ** from math import sin,pi… state="Active"
if bool(x)==False: ⇔ if not x:
Priority (…) × ÷ ab sin(pi/4)→0.707…
integer ÷ ÷ remainder cos(2*pi/3)→-0.4999… Exceptions on Errors
Signaling an error:
@ → matrix × python3.5+numpy sqrt(81)→9.0 √ raise ExcClass(…) error
(1+5.3)*2→12.6 log(e**2)→2.0 Errors processing: normal processing
abs(-3.2)→3.2 ceil(12.5)→13 try: raise X() errorraise
processing processing
round(3.57,1)→3.6 floor(12.5)→12 normal procesising block
pow(4,3)→64.0 modules math, statistics, random, except Exception as e: ☝ finally block for final processing
☝ usual order of operations decimal, fractions, numpy, etc. (cf. doc) error processing block in all cases.
statements block executed as long as Conditional Loop Statement statements block executed for each Iterative Loop Statement
condition is true item of a container or iterator
☝ beware of infinite loops!

yes next
while logical condition: ? Loop Control for var in sequence: …
no finish
statements block break immediate exit statements block
continue next iteration
s = 0 initializations before the loop ☝ else block for normal Go over sequence's values
i = 1 condition with a least one variable value (here i) loop exit. s = "Some text" initializations before the loop
Algo: cnt = 0

☝ good habit : don't modify loop variable


while i <= 100: i=100 loop variable, assignment managed by for statement
s = s + i**2
i = i + 1 ☝ make condition variable change ! s= ∑ i 2 for c in s:
if c == "e": Algo: count
print("sum:",s) i=1 cnt = cnt + 1 number of e
print("found",cnt,"'e'") in the string.
print("v=",3,"cm :",x,",",y+4) Display loop on dict/set ⇔ loop on keys sequences
use slices to loop on a subset of a sequence

items to display : literal values, variables, expressions Go over sequence's index


print options: ◽ modify item at index
◽ sep=" " items separator, default space ◽ access items around index (before / after)
lst = [11,18,9,12,23,4,17]
◽ end="\n" end of print, default new line lost = []
◽ file=sys.stdout print to file, default standard output for idx in range(len(lst)): Algo: limit values greater
val = lst[idx] than 15, memorizing
s = input("Instructions:") Input
if val > 15: of lost values.
☝ input always returns a string, convert it to required type lost.append(val)
(cf. boxed Conversions on the other side). lst[idx] = 15
print("modif:",lst,"-lost:",lost)
len(c)→ items count Generic Operations on Containers Go simultaneously over sequence's index and values:
min(c) max(c) sum(c) Note: For dictionaries and sets, these for idx,val in enumerate(lst):
sorted(c)→ list sorted copy operations use keys.
val in c → boolean, membership operator in (absence not in) range([start,] end [,step]) Integer Sequences
enumerate(c)→ iterator on (index, value) ☝ start default 0, end not included in sequence, step signed, default 1
zip(c1,c2…)→ iterator on tuples containing ci items at same index
range(5)→ 0 1 2 3 4 range(2,12,3)→ 2 5 8 11
all(c)→ True if all c items evaluated to true, else False range(3,8)→ 3 4 5 6 7 range(20,5,-5)→ 20 15 10
any(c)→ True if at least one item of c evaluated true, else False range(len(seq))→ sequence of index of values in seq
Specific to ordered sequences containers (lists, tuples, strings, bytes…) ☝ range provides an immutable sequence of int constructed as needed
reversed(c)→ inversed iterator c*5→ duplicate c+c2→ concatenate
c.index(val)→ position c.count(val)→ events count function name (identifier) Function Definition
import copy named parameters
copy.copy(c)→ shallow copy of container def fct(x,y,z):
copy.deepcopy(c)→ deep copy of container fct
"""documentation"""
☝ modify original list Operations on Lists # statements block, res computation, etc.
lst.append(val) add item at end return res result value of the call, if no computed
result to return: return None
lst.extend(seq) add sequence of items at end ☝ parameters and all
lst.insert(idx,val) insert item at index variables of this block exist only in the block and during the function
lst.remove(val) remove first item with value val call (think of a “black box”)
lst.pop([idx])→value remove & return item at index idx (default last) Advanced: def fct(x,y,z,*args,a=3,b=5,**kwargs):
lst.sort() lst.reverse() sort / reverse liste in place *args variable positional arguments (→tuple), default values,
**kwargs variable named arguments (→dict)
Operations on Dictionaries Operations on Sets
d[key]=value d.clear() Operators: r = fct(3,i+2,2*i) Function Call
| → union (vertical bar char) storage/use of one argument per
d[key]→ value del d[key] & → intersection returned value parameter
d.update(d2) update/add - ^ → difference/symmetric diff. ☝ this is the use of function fct() fct
associations Advanced:
d.keys() < <= > >= → inclusion relations name with parentheses *sequence
d.values() →iterable views on Operators also exist as methods. which does the call **dict
d.items() keys/values/associations
d.pop(key[,default])→ value s.update(s2) s.copy()
s.add(key) s.remove(key) s.startswith(prefix[,start[,end]]) Operations on Strings
d.popitem()→ (key,value)
d.get(key[,default])→ value s.discard(key) s.clear() s.endswith(suffix[,start[,end]]) s.strip([chars])
d.setdefault(key[,default])→value s.pop() s.count(sub[,start[,end]]) s.partition(sep)→ (before,sep,after)
s.index(sub[,start[,end]]) s.find(sub[,start[,end]])
storing data on disk, and reading it back Files s.is…() tests on chars categories (ex. s.isalpha())
f = open("file.txt","w",encoding="utf8") s.upper() s.lower() s.title() s.swapcase()
s.casefold() s.capitalize() s.center([width,fill])
file variable name of file
opening mode encoding of s.ljust([width,fill]) s.rjust([width,fill]) s.zfill([width])
for operations ◽ 'r' read
on disk chars for text s.encode(encoding) s.split([sep]) s.join(seq)
◽ 'w' write
(+path…) files:
◽ 'a' append utf8 ascii formating directives values to format Formatting
cf. modules os, os.path and pathlib ◽ …'+' 'x' 'b' 't' latin1 …
"modele{} {} {}".format(x,y,r) str
writing ☝ read empty string if end of file reading "{selection:formatting!conversion}"
f.write("coucou") f.read([n]) → next chars
f.writelines(list of lines) if n not specified, read up to end ! ◽ Selection : "{:+2.3f}".format(45.72793)
f.readlines([n]) → list of next lines 2 →'+45.728'
Examples

f.readline() → next line nom "{1:>10s}".format(8,"toto")


☝ text mode t by default (read/write str), possible binary 0.nom →' toto'
4[key] "{x!r}".format(x="I'm")
mode b (read/write bytes). Convert from/to required type ! 0[2]
f.close() ☝ dont forget to close the file after use ! →'"I\'m"'
◽ Formatting :
f.flush() write cache f.truncate([size]) resize fill char alignment sign mini width.precision~maxwidth type
reading/writing progress sequentially in the file, modifiable with:
<>^= + - space 0 at start for filling with 0
f.tell()→position f.seek(position[,origin]) integer: b binary, c char, d decimal (default), o octal, x or X hexa…
Very common: opening with a guarded block with open(…) as f: float: e or E exponential, f or F fixed point, g or G appropriate (default),
(automatic closing) and reading loop on lines for line in f : string: s … % percent
of a text file: # processing ofline ◽ Conversion : s (readable text) or r (literal representation)
LEARN DATA SCIENCE ONLINE
Start Learning For Free - www.dataquest.io

Data Science Cheat Sheet


NumPy

KEY IMPORTS
We’ll use shorthand in this cheat sheet Import these to start
arr - A numpy Array object import numpy as np

I M P O RT I N G/ E X P O RT I N G arr.T - Transposes arr (rows become columns and S C A L A R M AT H


np.loadtxt('file.txt') - From a text file vice versa) np.add(arr,1) - Add 1 to each array element
np.genfromtxt('file.csv',delimiter=',') arr.reshape(3,4) - Reshapes arr to 3 rows, 4 np.subtract(arr,2) - Subtract 2 from each array
- From a CSV file columns without changing data element
np.savetxt('file.txt',arr,delimiter=' ') arr.resize((5,6)) - Changes arr shape to 5x6 np.multiply(arr,3) - Multiply each array
- Writes to a text file and fills new values with 0 element by 3
np.savetxt('file.csv',arr,delimiter=',') np.divide(arr,4) - Divide each array element by
- Writes to a CSV file A D D I N G/ R E M OV I N G E L E M E N TS 4 (returns np.nan for division by zero)
np.append(arr,values) - Appends values to end np.power(arr,5) - Raise each array element to
C R E AT I N G A R R AYS of arr the 5th power
np.array([1,2,3]) - One dimensional array np.insert(arr,2,values) - Inserts values into
np.array([(1,2,3),(4,5,6)]) - Two dimensional arr before index 2 V E C TO R M AT H
array np.delete(arr,3,axis=0) - Deletes row on index np.add(arr1,arr2) - Elementwise add arr2 to
np.zeros(3) - 1D array of length 3 all values 0 3 of arr arr1
np.ones((3,4)) - 3x4 array with all values 1 np.delete(arr,4,axis=1) - Deletes column on np.subtract(arr1,arr2) - Elementwise subtract
np.eye(5) - 5x5 array of 0 with 1 on diagonal index 4 of arr arr2 from arr1
(Identity matrix) np.multiply(arr1,arr2) - Elementwise multiply
np.linspace(0,100,6) - Array of 6 evenly divided C O M B I N I N G/S P L I T T I N G arr1 by arr2
values from 0 to 100 np.concatenate((arr1,arr2),axis=0) - Adds np.divide(arr1,arr2) - Elementwise divide arr1
np.arange(0,10,3) - Array of values from 0 to less arr2 as rows to the end of arr1 by arr2
than 10 with step 3 (eg [0,3,6,9]) np.concatenate((arr1,arr2),axis=1) - Adds np.power(arr1,arr2) - Elementwise raise arr1
np.full((2,3),8) - 2x3 array with all values 8 arr2 as columns to end of arr1 raised to the power of arr2
np.random.rand(4,5) - 4x5 array of random floats np.split(arr,3) - Splits arr into 3 sub-arrays np.array_equal(arr1,arr2) - Returns True if the
between 0-1 np.hsplit(arr,5) - Splits arr horizontally on the arrays have the same elements and shape
np.random.rand(6,7)*100 - 6x7 array of random 5th index np.sqrt(arr) - Square root of each element in the
floats between 0-100 array
np.random.randint(5,size=(2,3)) - 2x3 array I N D E X I N G/S L I C I N G/S U B S E T T I N G np.sin(arr) - Sine of each element in the array
with random ints between 0-4 arr[5] - Returns the element at index 5 np.log(arr) - Natural log of each element in the
arr[2,5] - Returns the 2D array element on index array
I N S P E C T I N G P R O P E RT I E S [2][5] np.abs(arr) - Absolute value of each element in
arr.size - Returns number of elements in arr arr[1]=4 - Assigns array element on index 1 the the array
arr.shape - Returns dimensions of arr (rows, value 4 np.ceil(arr) - Rounds up to the nearest int
columns) arr[1,3]=10 - Assigns array element on index np.floor(arr) - Rounds down to the nearest int
arr.dtype - Returns type of elements in arr [1][3] the value 10 np.round(arr) - Rounds to the nearest int
arr.astype(dtype) - Convert arr elements to arr[0:3] - Returns the elements at indices 0,1,2
type dtype (On a 2D array: returns rows 0,1,2) STAT I ST I C S
arr.tolist() - Convert arr to a Python list arr[0:3,4] - Returns the elements on rows 0,1,2 np.mean(arr,axis=0) - Returns mean along
np.info(np.eye) - View documentation for at column 4 specific axis
np.eye arr[:2] - Returns the elements at indices 0,1 (On arr.sum() - Returns sum of arr
a 2D array: returns rows 0,1) arr.min() - Returns minimum value of arr
C O P Y I N G/S O RT I N G/ R E S H A P I N G arr[:,1] - Returns the elements at index 1 on all arr.max(axis=0) - Returns maximum value of
np.copy(arr) - Copies arr to new memory rows specific axis
arr.view(dtype) - Creates view of arr elements arr<5 - Returns an array with boolean values np.var(arr) - Returns the variance of array
with type dtype (arr1<3) & (arr2>5) - Returns an array with np.std(arr,axis=1) - Returns the standard
arr.sort() - Sorts arr boolean values deviation of specific axis
arr.sort(axis=0) - Sorts specific axis of arr ~arr - Inverts a boolean array arr.corrcoef() - Returns correlation coefficient
two_d_arr.flatten() - Flattens 2D array arr[arr<5] - Returns array elements smaller than 5 of array
two_d_arr to 1D

LEARN DATA SCIENCE ONLINE


Start Learning For Free - www.dataquest.io
LEARN DATA SCIENCE ONLINE
Start Learning For Free - www.dataquest.io

Data Science Cheat Sheet


Pandas

KEY IMPORTS
We’ll use shorthand in this cheat sheet Import these to start
df - A pandas DataFrame object import pandas as pd
s - A pandas Series object import numpy as np

I M P O RT I N G DATA SELECTION col1 in ascending order then col2 in descending


pd.read_csv(filename) - From a CSV file df[col] - Returns column with label col as Series order
pd.read_table(filename) - From a delimited text df[[col1, col2]] - Returns Columns as a new df.groupby(col) - Returns a groupby object for
file (like TSV) DataFrame values from one column
pd.read_excel(filename) - From an Excel file s.iloc[0] - Selection by position df.groupby([col1,col2]) - Returns a groupby
pd.read_sql(query, connection_object) - s.loc[0] - Selection by index object values from multiple columns
Reads from a SQL table/database df.iloc[0,:] - First row df.groupby(col1)[col2].mean() - Returns the
pd.read_json(json_string) - Reads from a JSON df.iloc[0,0] - First element of first column mean of the values in col2, grouped by the
formatted string, URL or file. values in col1 (mean can be replaced with
pd.read_html(url) - Parses an html URL, string or DATA C L E A N I N G almost any function from the statistics section)
file and extracts tables to a list of dataframes df.columns = ['a','b','c'] - Renames columns df.pivot_table(index=col1,values=
pd.read_clipboard() - Takes the contents of your pd.isnull() - Checks for null Values, Returns [col2,col3],aggfunc=mean) - Creates a pivot
clipboard and passes it to read_table() Boolean Array table that groups by col1 and calculates the
pd.DataFrame(dict) - From a dict, keys for pd.notnull() - Opposite of s.isnull() mean of col2 and col3
columns names, values for data as lists df.dropna() - Drops all rows that contain null df.groupby(col1).agg(np.mean) - Finds the
values average across all columns for every unique
E X P O RT I N G DATA df.dropna(axis=1) - Drops all columns that column 1 group
df.to_csv(filename) - Writes to a CSV file contain null values df.apply(np.mean) - Applies a function across
df.to_excel(filename) - Writes to an Excel file df.dropna(axis=1,thresh=n) - Drops all rows each column
df.to_sql(table_name, connection_object) - have have less than n non null values df.apply(np.max, axis=1) - Applies a function
Writes to a SQL table df.fillna(x) - Replaces all null values with x across each row
df.to_json(filename) - Writes to a file in JSON s.fillna(s.mean()) - Replaces all null values with
format the mean (mean can be replaced with almost J O I N /C O M B I N E
df.to_html(filename) - Saves as an HTML table any function from the statistics section) df1.append(df2) - Adds the rows in df1 to the
df.to_clipboard() - Writes to the clipboard s.astype(float) - Converts the datatype of the end of df2 (columns should be identical)
series to float pd.concat([df1, df2],axis=1) - Adds the
C R E AT E T E ST O B J E C TS s.replace(1,'one') - Replaces all values equal to columns in df1 to the end of df2 (rows should be
Useful for testing 1 with 'one' identical)
pd.DataFrame(np.random.rand(20,5)) - 5 s.replace([1,3],['one','three']) - Replaces df1.join(df2,on=col1,how='inner') - SQL-style
columns and 20 rows of random floats all 1 with 'one' and 3 with 'three' joins the columns in df1 with the columns
pd.Series(my_list) - Creates a series from an df.rename(columns=lambda x: x + 1) - Mass on df2 where the rows for col have identical
iterable my_list renaming of columns values. how can be one of 'left', 'right',
df.index = pd.date_range('1900/1/30', df.rename(columns={'old_name': 'new_ 'outer', 'inner'
periods=df.shape[0]) - Adds a date index name'}) - Selective renaming
df.set_index('column_one') - Changes the index STAT I ST I C S
V I E W I N G/ I N S P E C T I N G DATA df.rename(index=lambda x: x + 1) - Mass These can all be applied to a series as well.
df.head(n) - First n rows of the DataFrame renaming of index df.describe() - Summary statistics for numerical
df.tail(n) - Last n rows of the DataFrame columns
df.shape() - Number of rows and columns F I LT E R, S O RT, & G R O U P BY df.mean() - Returns the mean of all columns
df.info() - Index, Datatype and Memory df[df[col] > 0.5] - Rows where the col column df.corr() - Returns the correlation between
information is greater than 0.5 columns in a DataFrame
df.describe() - Summary statistics for numerical df[(df[col] > 0.5) & (df[col] < 0.7)] - df.count() - Returns the number of non-null
columns Rows where 0.7 > col > 0.5 values in each DataFrame column
s.value_counts(dropna=False) - Views unique df.sort_values(col1) - Sorts values by col1 in df.max() - Returns the highest value in each
values and counts ascending order column
df.apply(pd.Series.value_counts) - Unique df.sort_values(col2,ascending=False) - Sorts df.min() - Returns the lowest value in each column
values and counts for all columns values by col2 in descending order df.median() - Returns the median of each column
df.sort_values([col1,col2], df.std() - Returns the standard deviation of each
ascending=[True,False]) - Sorts values by column

LEARN DATA SCIENCE ONLINE


Start Learning For Free - www.dataquest.io
Basic plots Scales API Tick locators API Animation API

plot([X], Y, [fmt], …) API ax.set_[xy]scale(scale, …) from matplotlib import ticker import matplotlib.animation as mpla
0.0 0.0
ax.[xy]axis.set_[minor|major]_locator(locator)
Cheat sheet Version 3.7.4 X, Y, fmt, color, marker, linestyle linear log
+ any values + values > 0
2.5 2 0 2 symlog 2.510102101 0logit
- 0
ticker.NullLocator() T = np.linspace(0, 2*np.pi, 100)
S = np.sin(T)
Quick start API scatter(X, Y, …)
X, Y, [s]izes, [c]olors, marker, cmap 0.0
API
0.0
any values 0 < values < 1
ticker.MultipleLocator(0.5)
0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
line, = plt.plot(T, S)
import numpy as np
import matplotlib as mpl
2.5 1000100
-
2.5 1
+ 0 1 ticker.FixedLocator([0, 1, 5])
0 1 5
def animate(i):
line.set_ydata(np.sin(T+i/50))
import matplotlib.pyplot as plt bar[h](x, height, …) API
Projections
2 API
ticker.LinearLocator(numticks=3)
0.0 2.5 5.0
anim = mpla.FuncAnimation(
plt.gcf(), animate, interval=5)
x, height, width, bottom, align, color ticker.IndexLocator(base=0.5, offset=0.25)
0.25 0.75 1.25 1.75 2.25 2.75 3.25 3.75 4.25 4.75 plt.show()
subplot(…, projection=p)
ticker.AutoLocator()
X = np.linspace(0, 2*np.pi, 100) p=’polar’ p=’3d’ API 0 1 2 3 4 5
Y = np.cos(X) imshow(Z, …) API
ticker.MaxNLocator(n=4)
Styles API
Z, cmap, interpolation, extent, origin 0.0 1.5 3.0 4.5
fig, ax = plt.subplots() ticker.LogLocator(base=10, numticks=15) plt.style.use(style)
103 104 105 106 107 108 109 1010
ax.plot(X, Y, color=’green’) contour[f]([X], [Y], Z, …) API p=ccrs.Orthographic() API default classic grayscale
1.0
import cartopy.crs as ccrs
1.0 1.0

X, Y, Z, levels, colors, extent, origin 0.5 0.5 0.5

fig.savefig(“figure.pdf”) Tick formatters


0.0 0.0 0.0
API 0.5 0.5 0.5

plt.show() 1.0
0 1 2 3 4 5 6
1.0
0 1 2 3 4 5 6 7
1.0
0 1 2 3 4 5 6

pcolormesh([X], [Y], Z, …) API from matplotlib import ticker


ax.[xy]axis.set_[minor|major]_formatter(formatter)
ggplot seaborn-v0_8 fast
Anatomy of a figure X, Y, Z, vmin, vmax, cmap
1.0 1.0 1.0

Lines
0.5 0.5 0.5
API
ticker.NullFormatter()
0.0 0.0 0.0

0.5 0.5 0.5

4
Anatomy of a figure linestyle or ls
1.0
0 1 2 3 4 5 6
1.0
0 1 2 3 4 5 6
1.0
0 1 2 3 4 5 6

Title Blue signal quiver([X], [Y], U, V, …) API ticker.FixedFormatter(['zero', 'one', 'two', ]) bmh Solarize_Light2 seaborn-v0_8-notebook
Major tick Red signal X, Y, U, V, C, units, angles zero one two three four five 1.0 1.0 1.0
"-" ":" "--" "-." (0,(0.01,2)) 0.5 0.5 0.5

Legend ticker.FuncFormatter(lambda x, pos: "[%.2f]" % x)


capstyle or dash_capstyle 0.0 0.0 0.0

0.5 0.5 0.5


[0.00] [1.00] [2.00] [3.00] [4.00] [5.00]
Minor tick 1.0
0 1 2 3 4 5 6
1.0
0 1 2 3 4 5 6
1.0
0 1 2 3 4 5 6

pie(X, …) API "butt" "round" "projecting" ticker.FormatStrFormatter('>%d<')


3
Major tick label Grid
Z, explode, labels, colors, radius >0< >1< >2< >3< >4< >5<

Line
ticker.ScalarFormatter() Quick reminder
(line plot) Markers API 0 1 2 3 4 5

text(x, y, text, …) API ticker.StrMethodFormatter('{x}') ax.grid()


T
Y axis label

TEX
2 x, y, text, va, ha, size, weight, transform 0.0 1.0 2.0 3.0 4.0 5.0 ax.set_[xy]lim(vmin, vmax)
'.' 'o' 's' 'P' 'X' '*' 'p' 'D' '<' '>' '^' 'v' ticker.PercentFormatter(xmax=5) ax.set_[xy]label(label)
Y axis label 0% 20% 40% 60% 80% 100%
Markers ax.set_[xy]ticks(ticks, [labels])
(scatter plot)
fill[_between][x](…) API '1' '2' '3' '4' '+' 'x' '|' '_' 4 5 6 7 ax.set_[xy]ticklabels(labels)
1
X, Y1, Y2, color, where Ornaments ax.set_title(title)
'$ $''$ $''$ $''$ $''$ $''$ $''$ $''$ $''$ $''$ $''$ $''$ $' ax.tick_params(width=10, …)
Spines
markevery ax.legend(…) API ax.set_axis_[on|off]()
Figure Line
10 [0, -1] (25, 5) [0, 25, -1] handles, labels, loc, title, frameon
Axes (line plot)
Advanced plots fig.suptitle(title)
0 title
0 0.25 0.50 0.75 1 1.25 1.50 1.75 2 2.25 2.50 2.75 3 3.25 3.50 3.75 4
Minor tick label
X axis label step(X, Y, [fmt], …) API
Colors API
Legend label
fig.tight_layout()
plt.gcf(), plt.gca()
X axis label X, Y, fmt, color, marker, where handletextpad handle
markerfacecolor (mfc)
1 C0 C1 C2 C3 C4 C5 C6 C7 C8 C9
handlelength mpl.rc(’axes’, linewidth=1, …)
0
’Cn’ Label 1 Label 3 [fig|ax].patch.set_alpha(0)
Subplots layout API 0 b 2 g 4 r 6 c 8 m 10 y 12 k 14 w 16 ’x’
1 DarkRed Firebrick Crimson IndianRed Salmon labelspacing markeredgecolor (mec) text=r’$\frac{-e^{i\pi}}{2^n}$’
boxplot(X, …) API
X, notch, sym, bootstrap, widths 10 (1,0,0) (1,0,0,0.75) (1,0,0,0.5) (1,0,0,0.25)
’name’ Label 2 Label 4
subplot[s](rows, cols, …) API 0 2 4 6 8 10 12 14 16 (R,G,B[,A])
10 #FF0000 borderpad columnspacing numpoints or scatterpoints
fig, axs = plt.subplots(3, 3) 0 2 4 #FF0000BB
6 8 #FF000088
10 12 #FF000044
14 16 ’#RRGGBB[AA]’
10 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 borderaxespad
Keyboard shortcuts API
0 2 4 6 8 10 12 14 16 ’x.y’
0
errorbar(X,Y,xerr,yerr, …) API 0 2 4 6 8 10 12 14 16 ctrl + s Save ctrl + w Close plot
G = gridspec(rows,cols, …) API X, Y, xerr, yerr, fmt ax.colorbar(…) API
ax = G[0, :] Colormaps API mappable, ax, cax, orientation r Reset view f Fullscreen 0/1
f View forward b View back
hist(X, bins, …) API plt.get_cmap(name) p Pan view o Zoom to rect
ax.inset_axes(extent) API X, bins, range, density, weights 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
Uniform x X pan/zoom y Y pan/zoom
viridis g Minor grid 0/1 G Major grid 0/1
violinplot(D, …) API magma
ax.annotate(…) API l X axis log/linear L Y axis log/linear
d=make_axes_locatable(ax) API D, positions, widths, vert plasma
text, xy, xytext, xycoords, textcoords, arrowprops
ax = d.new_horizontal(’10%’) Sequential
barbs([X], [Y], U, V, …) API
Greys
text Ten simple rules READ
YlOrBr xytext xy
X, Y, U, V, C, length, pivot, sizes textcoords xycoords
Wistia 1. Know your audience
Getting help Diverging 2. Identify your message
Spectral 3. Adapt the figure
matplotlib.org eventplot(positions, …) API
4. Captions are not optional
Å
positions, orientation, lineoffsets
coolwarm
Event handling API
H github.com/matplotlib/matplotlib/issues RdGy 5. Do not trust the defaults
ď discourse.matplotlib.org Qualitative fig, ax = plt.subplots() 6. Use color effectively
W stackoverflow.com/questions/tagged/matplotlib hexbin(X, Y, C, …) API
tab10 def on_click(event): 7. Do not mislead the reader
Ż https://gitter.im/matplotlib/matplotlib X, Y, C, gridsize, bins
tab20 print(event) 8. Avoid “chartjunk”
F twitter.com/matplotlib Cyclic fig.canvas.mpl_connect( 9. Message trumps beauty
a Matplotlib users mailing list twilight ’button_press_event’, on_click) 10. Get the right tool
Matplotlib for beginners
Matplotlib is a library for making 2D plots in Python. It is
Z = np.random.uniform(0, 1, (8, 8))
Organize
designed with the philosophy that you should be able to
create simple plots with just a few commands: You can plot several data on the same figure, but you can
ax.contourf(Z) also split a figure in several subplots (named Axes):
1 Initialize
Z = np.random.uniform(0, 1, 4) X = np.linspace(0, 10, 100)
import numpy as np Y1, Y2 = np.sin(X), np.cos(X)
import matplotlib.pyplot as plt ax.pie(Z) ax.plot(X, Y1, X, Y2)

Z = np.random.normal(0, 1, 100) fig, (ax1, ax2) = plt.subplots(2, 1)


2 Prepare
ax1.plot(X, Y1, color=”C1”)
X = np.linspace(0, 10*np.pi, 1000) ax.hist(Z) ax2.plot(X, Y2, color=”C0”)
Y = np.sin(X)
X = np.arange(5) fig, (ax1, ax2) = plt.subplots(1, 2)
3 Render Y = np.random.uniform(0, 1, 5) ax1.plot(Y1, X, color=”C1”)
ax.errorbar(X, Y, Y∕4) ax2.plot(Y2, X, color=”C0”)
fig, ax = plt.subplots()
ax.plot(X, Y) Z = np.random.normal(0, 1, (100, 3))
plt.show() Label (everything)
ax.boxplot(Z)
4 Observe A Sine wave
ax.plot(X, Y)
1.0 fig.suptitle(None)
0.5 Tweak ax.set_title(”A Sine wave”)
0.0
0.5 You can modify pretty much anything in a plot, including lim-
ax.plot(X, Y)
1.0 its, colors, markers, line width and styles, ticks and ticks la-
0 5 10 15 20 25 30 ax.set_ylabel(None)
bels, titles, etc.
ax.set_xlabel(”Time”)
Time

Choose X = np.linspace(0, 10, 100)


Y = np.sin(X) Explore
Matplotlib offers several kind of plots (see Gallery): ax.plot(X, Y, color=”black”)
Figures are shown with a graphical user interface that al-
X = np.random.uniform(0, 1, 100) X = np.linspace(0, 10, 100) lows to zoom and pan the figure, to navigate between the
Y = np.random.uniform(0, 1, 100) Y = np.sin(X) different views and to show the value under the mouse.
ax.scatter(X, Y) ax.plot(X, Y, linestyle=”--”)
Save (bitmap or vector format)
X = np.arange(10) X = np.linspace(0, 10, 100)
Y = np.random.uniform(1, 10, 10) Y = np.sin(X)
ax.bar(X, Y) ax.plot(X, Y, linewidth=5) fig.savefig(”my-first-figure.png”, dpi=300)
fig.savefig(”my-first-figure.pdf”)
Z = np.random.uniform(0, 1, (8, 8)) X = np.linspace(0, 10, 100)
Y = np.sin(X)
Matplotlib 3.7.4 handout for beginners. Copyright (c) 2021 Matplotlib Development
ax.imshow(Z) ax.plot(X, Y, marker=”o”) Team. Released under a CC-BY 4.0 International License. Supported by NumFOCUS.

You might also like