In [1]:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np

In [2]:
matplotlib.__version__,np.__version__


Out[2]:
('3.0.3', '1.16.3')

In [3]:
import numpy as np
import matplotlib.pyplot as plt

np.random.seed(42)

# generate random data for plotting
x = np.linspace(0.0,100,50)
y = np.random.normal(size=50)

plt.ylim(-3,3)

plt.plot(x,y)
plt.legend(['line plot 1'])

plt.show()


multiple plots same axes


In [4]:
import numpy as np
import matplotlib.pyplot as plt

plt.clf()

np.random.seed(42)

x = np.linspace(0.0,100,20)

y1 = np.random.normal(scale=0.2,size=20)
y2 = np.random.normal(scale=0.5,size=20)
y3 = np.random.normal(scale=0.8,size=20)

plt.ylim(-3,3)

plt.plot(x,y1,label='plot 1')
plt.plot(x,y2, label='plot 2')
plt.plot(x,y3, label='plot 3')

plt.legend()

plt.show()


legend for single axes


In [5]:
import numpy as np
import matplotlib.pyplot as plt

plt.clf()

np.random.seed(42)

fig, axes = plt.subplots(2,2)

# just plot things on each individual axes
x = np.linspace(0.0,100,20)
y1 = np.random.normal(scale=0.2,size=20)
y2 = np.random.normal(scale=0.5,size=20)
y3 = np.random.normal(scale=0.8,size=20)

axes[0][0].scatter(x,y1)
axes[0][1].bar(x,y1,width=3.0)
axes[1][0].scatter(x,y2)
axes[1][1].plot(x,y2)

# you can set a legend for a single subplot
axes[1][1].legend(['plot 4'])

plt.subplots_adjust(wspace=0.2, hspace=0.2)

plt.gcf().set_size_inches(8,8)

plt.show()


<Figure size 432x288 with 0 Axes>

legend for scatter plot


In [6]:
import numpy as np
import matplotlib.pyplot as plt

np.random.seed(42)

# generate random data for plotting
x = np.linspace(0.0,100,50)
y = np.random.normal(size=50)
y2 = np.random.normal(size=50)

plt.ylim(-3,3)

plt.scatter(x,y,label='plot 1')
plt.scatter(x,y2,label='plot 2')

plt.legend()

plt.show()


disable legend


In [7]:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

np.random.seed(42)

plt.clf()

# generate random data for plotting
x = np.linspace(0.0,100,50)
y = x*2

df = pd.DataFrame({
    'x':x,
    'y':y
})

# this will add legends by default
df.plot(kind='line')

plt.gca().legend_.remove()

plt.show()


<Figure size 432x288 with 0 Axes>

customize number of columns


In [8]:
import numpy as np
import matplotlib.pyplot as plt
import math

np.random.seed(42)

# generate random data for plotting
x = np.linspace(0.0,100,50)

y2 = x*2
y3 = x*3
y4 = x*4
y5 = x*5
y6 = x*6
y7 = x*7
y8 = x*8
y9 = x*9
y10 = x*10

plt.plot(x,y2,label='y=2x',linewidth=3)
plt.plot(x,y3,label='y=3x',linewidth=3)
plt.plot(x,y4,label='y=4x',linewidth=3)
plt.plot(x,y5,label='y=5x',linewidth=3)
plt.plot(x,y6,label='y=6x',linewidth=3)
plt.plot(x,y7,label='y=7x',linewidth=3)
plt.plot(x,y8,label='y=8x',linewidth=3)
plt.plot(x,y9,label='y=9x',linewidth=3)
plt.plot(x,y10,label='y=10x',linewidth=3)

plt.legend(ncol=2)

plt.show()


legend outside of plot


In [9]:
import numpy as np
import matplotlib.pyplot as plt
import math

plt.clf()

np.random.seed(42)

# generate random data for plotting
x = np.linspace(0.0,100,50)

y2 = x*2
y3 = x*3
y4 = x*4
y5 = x*5


plt.plot(x,y2,label='y=2x')
plt.plot(x,y3,label='y=3x')
plt.plot(x,y4,label='y=4x')
plt.plot(x,y5,label='y=5x')

plt.gca().legend(loc='center left', bbox_to_anchor=(1, 0.5))

# box = plt.gca().get_position()
# plt.gca().set_position([box.x0, box.y0, box.width * 0.8, box.height])

plt.gcf().set_size_inches(6,5)

plt.show()


sort legend labels


In [10]:
import numpy as np
import matplotlib.pyplot as plt
import math

plt.clf()

np.random.seed(42)

# generate random data for plotting
x = np.linspace(0.0,100,50)

y2 = x*2
y3 = x*3
y4 = x*4
y5 = x*5
y6 = x*6
y7 = x*7

colour_blind_cycle = ['#377eb8', '#ff7f00', '#4daf4a',
                  '#f781bf', '#a65628', '#984ea3',
                  '#999999', '#e41a1c', '#dede00']


plt.gca().set_prop_cycle(color=colour_blind_cycle)

plt.plot(x,y2,label='y=2x',linewidth=3)
plt.plot(x,y3,label='y=3x',linewidth=3)
plt.plot(x,y4,label='y=4x',linewidth=3)
plt.plot(x,y5,label='y=5x',linewidth=3)
plt.plot(x,y6,label='y=6x',linewidth=3)
plt.plot(x,y7,label='y=7x',linewidth=3)


current_handles, current_labels = plt.gca().get_legend_handles_labels()

reversed_handles = list(reversed(current_handles))
reversed_labels = list(reversed(current_labels))

plt.legend(reversed_handles,reversed_labels)

plt.show()


sort legend labels using pandas


In [15]:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

np.random.seed(42)

plt.clf()

# generate random data for plotting
x = np.linspace(0.0,100,50)

y  = x*1
y2 = x*2
y3 = x*3
y4 = x*4
y5 = x*5
y6 = x*6
y7 = x*7


df = pd.DataFrame({
    'y':y,
    'y2':y2,
    'y3':y3,
})

# this will add legends by default
df.plot(kind='line',legend='reverse')

plt.show()


<Figure size 432x288 with 0 Axes>