Matplotlib > All categories

Name
Code
Output
gaussian = numpy.random.normal(loc=1.0, scale=0.1,size = 1000)
matplotlib.pyplot.boxplot(gaussian)
matplotlib.pyplot.show()

       
           Box and whisker plot (basic)
       
    
# Data
gauss1 = numpy.random.normal(loc=1.0, scale=10.0,size = 1000)
gauss2 = numpy.random.normal(loc=5.0, scale=7.0,size = 1000)
gauss3 = numpy.random.normal(loc=10.0, scale=5.0,size = 1000)
# New Figure
fig = matplotlib.pyplot.figure(figsize=(7,4),facecolor='silver')
# New Axes
ax = fig.add_subplot(111,facecolor='azure')
# Add grid
ax.grid(color='b', linestyle='-', linewidth=0.5)
# Plot the data
bw_plot = ax.boxplot([gauss1,gauss2,gauss3],labels=['A', 'B', 'C'],patch_artist=True)
ax.set(title="Plot title", xlabel="x axis", ylabel="y axis")
for item in ['boxes', 'whiskers','medians', 'means','caps','fliers']:
    matplotlib.pyplot.setp(bw_plot[item], color='blue',linewidth=2)
for box_patch in bw_plot['boxes']:
        box_patch.set(facecolor='orange') 
# Fit the plot layout
matplotlib.pyplot.tight_layout()
# Show the plot
matplotlib.pyplot.show()

       
           Box and whisker plot (customized)
       
    
x = [0,1]
y = [0,1]
matplotlib.pyplot.fill_between(x,y)
matplotlib.pyplot.show()

       
           Fill between plot (basic)
       
    
# Data
x = numpy.linspace(0,30,100)
y1 = numpy.sin(x)
#y2 = x**2
# New Figure
fig = matplotlib.pyplot.figure(figsize=(7,4),facecolor='silver')
# New Axes
ax = fig.add_subplot(111,facecolor='azure')
# Add grid
ax.grid(color='b', linestyle='-', linewidth=0.5)
# Plot the data
matplotlib.pyplot.fill_between(x,y1,where=y1>0, facecolor='green', interpolate=True)
matplotlib.pyplot.fill_between(x,y1,where=y1<0, facecolor='red', interpolate=True)
ax.set(title="Plot title", xlabel="x axis", ylabel="y axis") 
# Fit the plot layout
matplotlib.pyplot.tight_layout()
# Show the plot
matplotlib.pyplot.show()

       
           Fill between plot (customized)
       
    
x = [1,2,3,4]
y = [0,1,1,0]
matplotlib.pyplot.fill(x,y)
matplotlib.pyplot.show()

       
           Fill plot (basic)
       
    
# Data
x = numpy.linspace(1,5,100)
y1 = numpy.max(x)**2-x**2
y2 = x**2
# New Figure
fig = matplotlib.pyplot.figure(figsize=(7,4),facecolor='silver')
# New Axes
ax = fig.add_subplot(111,facecolor='azure')
# Add grid
ax.grid(color='b', linestyle='-', linewidth=0.5)
# Plot the data
matplotlib.pyplot.fill(x,y1,'red',zorder=1,alpha=0.5)
matplotlib.pyplot.fill(x,y2,'green',zorder=2,alpha=0.5)
ax.set(title="Plot title", xlabel="x axis", ylabel="y axis") 
# Fit the plot layout
matplotlib.pyplot.tight_layout()
# Show the plot
matplotlib.pyplot.show()

       
           Fill plot (customized)
       
    
gaussian = numpy.random.normal(loc=1.0, scale=0.1,size = 1000)
matplotlib.pyplot.hist(gaussian)
matplotlib.pyplot.show()

       
           Histogram (basic)
       
    
# Data
gaussian = numpy.random.normal(loc=1.0, scale=0.1,size = 10000)
# New Figure
fig = matplotlib.pyplot.figure(figsize=(7,4),facecolor='silver')
# New Axes
ax = fig.add_subplot(111,facecolor='azure')
# Add grid
ax.grid(color='b', linestyle='-', linewidth=0.5)
# set ticks
ax.xaxis.set_ticks(numpy.arange(min(x), max(x)+1, 1.0))
# Plot the data
ax.hist(gaussian,bins=50,color='orange')
ax.set(title="Plot title", xlabel="x axis", ylabel="y axis")
# Fit the plot layout
matplotlib.pyplot.tight_layout()
# Show the plot
matplotlib.pyplot.show()

       
           Histogram (customized)
       
    
x = [1,2,3,4]
y = [1,2,1,5]
matplotlib.pyplot.barh(x,y)
matplotlib.pyplot.show()

       
           Horizontal bar plot (basic)
       
    
# Data
x = [1,2,3,4]
y = [1,2,1,5]
# New Figure
fig = matplotlib.pyplot.figure(figsize=(7,4),facecolor='silver')
# New Axes
ax = fig.add_subplot(111,facecolor='azure')
# Add grid
ax.grid(color='b', linestyle='-', linewidth=0.5)
# set ticks
ax.xaxis.set_ticks(numpy.arange(min(x), max(x)+1, 1.0))
# Plot the data
ax.barh(x, y,color=['red','lime','green','orange'],tick_label=['A','B','C','D'])
ax.set(title="Plot title", xlabel="x axis", ylabel="y axis")
# Fit the plot layout
matplotlib.pyplot.tight_layout()
# Show the plot
matplotlib.pyplot.show()

       
           Horizontal bar plot (customized)
       
    
# matplotlib is usually imported as mpl
import matplotlib as mpl
# matplotlib pyplot is usually imported as plt
# in other cheats we use full matplotlib.pyplot to avoid confusion
import matplotlib.pyplot as plt

       
    
x = [0,1,2,3,4]
y = [1,2,3,2,3]
matplotlib.pyplot.plot(x, y)
matplotlib.pyplot.show()

       
           Linear plot (basic)
       
    
# Data
x = numpy.linspace(0,4,10)
y1 = x
y2 = x**2
# New Figure
fig = matplotlib.pyplot.figure(figsize=(7,4),facecolor='silver')
# New Axes
ax = fig.add_subplot(111,facecolor='azure')
# Add grid
ax.grid(color='b', linestyle='-', linewidth=0.5)
# set ticks
ax.xaxis.set_ticks(numpy.arange(min(x), max(x)+1, 1.0))
# Plot the data
ax.plot(x,y1,color='blue', linestyle='dashed', marker='o',
     markerfacecolor='orange', markersize=10)
ax.plot(x,y2,color='red', linestyle='dashed', marker='o',
     markerfacecolor='silver', markersize=10)
ax.set(title="Plot title", xlabel="x axis", ylabel="y axis")
# Fit the plot layout
matplotlib.pyplot.tight_layout()
# Show the plot
matplotlib.pyplot.show()

       
           Linear plot (customized)
       
    
# Data
x = [0,1,2,3,4]
y = [1,2,3,2,3]
# New Figure
fig = matplotlib.pyplot.figure(figsize=(7,4),facecolor='silver')
# SUBPLOT 1
# New Axes with grid
ax1 = fig.add_subplot(221,facecolor='azure')
ax1.grid(color='b', linestyle='-', linewidth=0.5)
# Plot the data
matplotlib.pyplot.plot(x,y,color='red')
ax1.set(title="Plot1 title", xlabel="x axis", ylabel="y axis") 
# SUBPLOT 2
# New Axes with grid
ax2 = fig.add_subplot(222,facecolor='azure')
ax2.grid(color='b', linestyle='-', linewidth=0.5)
# Plot the data
matplotlib.pyplot.scatter(x,y,color='green')
ax2.set(title="Plot2 title", xlabel="x axis", ylabel="y axis") 
# SUBPLOT 3
# New Axes with grid
ax3 = fig.add_subplot(223,facecolor='azure')
ax3.grid(color='b', linestyle='-', linewidth=0.5)
# Plot the data
matplotlib.pyplot.bar(x,y,color='orange')
ax3.set(title="Plot3 title", xlabel="x axis", ylabel="y axis") 
# SUBPLOT 4
# New Axes with grid
ax3 = fig.add_subplot(224,facecolor='azure')
ax3.grid(color='b', linestyle='-', linewidth=0.5)
# Plot the data
matplotlib.pyplot.fill_between(x,y,color='lime')
ax3.set(title="Plot4 title", xlabel="x axis", ylabel="y axis") 
# Fit the plot layout
matplotlib.pyplot.tight_layout()
# Show the plot
matplotlib.pyplot.show()

       
           Multiple subplots
       
    
labels = ['A','B','C','D']
shares = [10,20,30,40] 
matplotlib.pyplot.pie(shares,labels=labels)
matplotlib.pyplot.show()

       
           Pie chart (basic)
       
    
labels = ['A','B','C','D']
shares = [10,20,30,20] 
# Data
gaussian = numpy.random.normal(loc=1.0, scale=0.1,size = 10000)
# New Figure
fig = matplotlib.pyplot.figure(figsize=(7,4),facecolor='azure')
# New Axes
ax = fig.add_subplot(111)
# Plot the data
ax.pie(shares,explode=(0.2,0,0,0),labels=labels,
       colors=['red','green','orange','yellow'],
       autopct='%.0f%%',shadow=True,radius=0.9,counterclock=False)
ax.set(title="Plot title")
# Fit the plot layout
matplotlib.pyplot.tight_layout()
# Show the plot
matplotlib.pyplot.show()

       
           Pie chart (customized)
       
    
print("Matplotlib version: ",matplotlib.__version__)
Matplotlib version:  2.0.2
x = numpy.linspace(0,5,20)
y = 2*x
matplotlib.pyplot.scatter(x, y)
matplotlib.pyplot.show()

       
           Scatter plot (basic)
       
    
# Data
x = numpy.linspace(0,4,30)
y1 = x**2
y2 = x**(0.5)
# New Figure
fig = matplotlib.pyplot.figure(figsize=(7,4),facecolor='silver')
# New Axes
ax = fig.add_subplot(111,facecolor='azure')
# Add grid
ax.grid(color='b', linestyle='-', linewidth=0.5)
# set ticks
ax.xaxis.set_ticks(numpy.arange(min(x), max(x)+1, 1.0))
# Plot the data
ax.scatter(x, y1, c='blue', marker='x', linewidths=5)
ax.scatter(x, y2, c='green', marker='o',linewidths=5)
ax.set(title="Plot title", xlabel="x axis", ylabel="y axis")
# Fit the plot layout
matplotlib.pyplot.tight_layout()
# Show the plot
matplotlib.pyplot.show()

       
           Scatter plot (customized)
       
    
x = [0,1,2,3,4]
y1 = [0,1,1,3,3]
y2 = [0,1,2,3,4]
y3 = [0,2,1,2,2]
matplotlib.pyplot.stackplot(x, y1, y2,y3)
matplotlib.pyplot.show()

       
           Stack plot (basic)
       
    
# Data
x = [0,1,2,3,4]
y1 = [0,1,1,3,3]
y2 = [0,1,2,3,4]
y3 = [0,2,1,2,2]
# New Figure
fig = matplotlib.pyplot.figure(figsize=(7,4),facecolor='silver')
# New Axes
ax = fig.add_subplot(111,facecolor='azure')
# Add grid
ax.grid(color='b', linestyle='-', linewidth=0.5)
# set ticks
ax.xaxis.set_ticks(numpy.arange(min(x), max(x)+1, 1.0))
# Plot the data
ax.stackplot(x,y1,y2,y3,labels = ['A','B','C'],colors =['green','red','orange'])
ax.set(title="Plot title", xlabel="x axis", ylabel="y axis")
ax.legend(loc=2)
# Fit the plot layout
matplotlib.pyplot.tight_layout()
# Show the plot
matplotlib.pyplot.show()

       
           Stack plot (customized)
       
    
x = [1,2,3,4]
y = [1,2,1,5]
matplotlib.pyplot.bar(x,y)
matplotlib.pyplot.show()

       
           Vertical bar plot (basic)
       
    
# Data
x = [1,2,3,4]
y = [1,2,1,5]
# New Figure
fig = matplotlib.pyplot.figure(figsize=(7,4),facecolor='silver')
# New Axes
ax = fig.add_subplot(111,facecolor='azure')
# Add grid
ax.grid(color='b', linestyle='-', linewidth=0.5)
# set ticks
ax.xaxis.set_ticks(numpy.arange(min(x), max(x)+1, 1.0))
# Plot the data
ax.bar(x, y,color=['red','lime','green','orange'],tick_label=['A','B','C','D'])
ax.set(title="Plot title", xlabel="x axis", ylabel="y axis")
# Fit the plot layout
matplotlib.pyplot.tight_layout()
# Show the plot
matplotlib.pyplot.show()

       
           Vertical bar plot (customized)
       
    
gaussian = numpy.random.normal(loc=1.0, scale=0.1,size = 1000)
matplotlib.pyplot.violinplot(gaussian)
matplotlib.pyplot.show()

       
           Violin plot (basic)
       
    
# Data
gauss1 = numpy.random.normal(loc=1.0, scale=10.0,size = 1000)
gauss2 = numpy.random.normal(loc=5.0, scale=7.0,size = 1000)
gauss3 = numpy.random.normal(loc=10.0, scale=5.0,size = 1000)
# New Figure
fig = matplotlib.pyplot.figure(figsize=(7,4),facecolor='silver')
# New Axes
ax = fig.add_subplot(111,facecolor='azure')
# Add grid
ax.grid(color='b', linestyle='-', linewidth=0.5)
# Plot the data
bw_plot = ax.violinplot([gauss1,gauss2,gauss3],positions=[0,2,4],
                        vert=True,showmeans=True,showextrema=True,showmedians=True)
for item in bw_plot['bodies']:
    item.set_facecolor('red')
    item.set_edgecolor('grey')
    item.set_alpha(0.8)
ax.set(title="Plot title", xlabel="x axis", ylabel="y axis") 
labels = ['A','B','C','D','E']
ax.set_xticks(numpy.arange(0, len(labels)))
ax.set_xticklabels(labels)
# Fit the plot layout
matplotlib.pyplot.tight_layout()
# Show the plot
matplotlib.pyplot.show()

       
           Violin plot (customized)