Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Matplotlib
################################################################################################
# name: barplot-03.py
# desc: Simple bar plot with options
# date: 2018-07-02
# Author: conquistadorjd
# Documentation : https://matplotlib.org/api/_as_gen/matplotlib.pyplot.bar.html#matplotlib.pyplot.bar
################################################################################################
from matplotlib import pyplot as plt
import numpy as np
print('*** Program Started ***')
drinks = ["cappuccino", "latte", "chai", "americano", "mocha", "espresso"]
sales = [71, 91, 56, 66, 52, 27]
salesyerr = [5, 5, 5, 5, 5, 5]
# This is with mandatory fields
# plt.bar(np.arange(len(drinks)), sales)
# This is with multiple options
plt.bar(np.arange(len(drinks)), sales, width=0.5, yerr=salesyerr, label="Jagur", color='r',edgecolor='b', fill=True, hatch='*',linestyle='--',align='center')
plt.xlabel('Sample x Axis')
plt.ylabel('Sample y Axis')
plt.title('This is bar plot using matplotlib')
# plt.legend(drinks)
plt.legend(loc=2)
# Saving image
plt.savefig('barplot-03.png')
# In case you dont want to save image but just displya it
plt.show()
print('*** Program ended ***')
################################################################################################
# name: barplot-04.py
# desc: Simple bar plot with options - horizontal bar
# date: 2018-07-02
# Author: conquistadorjd
################################################################################################
from matplotlib import pyplot as plt
import numpy as np
print('*** Program Started ***')
drinks = ["cappuccino", "latte", "chai", "americano", "mocha", "espresso"]
sales = [71, 91, 56, 66, 52, 27]
plt.barh(np.arange(len(drinks)), sales,label="Jagur", color='b',edgecolor='b')
plt.xlabel('Sample x Axis')
plt.ylabel('Sample y Axis')
plt.title('This is bar plot using matplotlib')
# plt.legend(drinks)
plt.legend(loc=2)
# Saving image
plt.savefig('barplot-04.png')
# In case you dont want to save image but just displya it
plt.show()
print('*** Program ended ***')
################################################################################################
# name: barplot-05.py
# desc: stacked bar plot
# date: 2018-07-02
# Author: conquistadorjd
################################################################################################
from matplotlib import pyplot as plt
import numpy as np
print('*** Program Started ***')
drinks = ["cappuccino", "latte", "chai", "americano", "mocha", "espresso"]
sales = [71, 91, 56, 66, 52, 27]
inquiry = [60, 50, 70, 66, 52, 27]
salesyerr = [5, 5, 5, 5, 5, 5]
inquiryerr = [5, 5, 5, 5, 5, 5]
plt.bar(np.arange(len(drinks)), sales,width=0.5,label="Jagur", color='r',edgecolor='r',yerr=salesyerr)
plt.bar(np.arange(len(drinks)), inquiry,bottom=sales,width=0.5,label="Range Rover", color='b',edgecolor='b',yerr=inquiryerr)
plt.xlabel('Sample x Axis')
plt.ylabel('Sample y Axis')
plt.title('This is bar plot using matplotlib')
# plt.legend(drinks)
plt.legend(loc=2)
# Saving image
plt.savefig('barplot-05.png')
# In case you dont want to save image but just displya it
plt.show()
print('*** Program ended ***')
################################################################################################
# name: barplot-06.py
# desc: stacked bar plot
# date: 2018-07-02
# Author: conquistadorjd
################################################################################################
from matplotlib import pyplot as plt
import numpy as np
print('*** Program Started ***')
drinks = ["cappuccino", "latte", "chai", "americano", "mocha", "espresso"]
jagur = [71, 91, 56, 66, 52, 95]
rangerover = [60, 50, 70, 85, 69, 27]
jaguryerr = [5, 5, 5, 5, 5, 5]
rangerovererr = [5, 5, 5, 5, 5, 5]
width = 0.25
## simple way of doing this
# plt.bar(np.arange(len(drinks)), jagur,+width,align='edge',label="Jagur", color='r',edgecolor='r',yerr=jaguryerr)
# plt.bar(np.arange(len(drinks)), rangerover,-width ,align='edge',label="Range Rover", color='b',edgecolor='b',yerr=rangerovererr)
### Another way of doing same thing
plt.bar(np.arange(len(drinks)), jagur,width,align='edge',label="Jagur", color='r',edgecolor='r',yerr=jaguryerr)
plt.bar(np.arange(len(drinks))+width, rangerover,width ,align='edge',label="Range Rover", color='b',edgecolor='b',yerr=rangerovererr)
plt.bar(np.arange(len(drinks))+width+width, rangerover,width ,align='edge',label="Land Rover", color='c',edgecolor='c',yerr=rangerovererr)
print(type(np.arange(len(drinks))))
print(type(range(len(drinks))))
plt.xlabel('Sample x Axis')
plt.ylabel('Sample y Axis')
plt.title('Monthly sales of cars')
# plt.legend(drinks)
plt.legend(loc=2)
# Saving image
plt.savefig('barplot-06.png')
# In case you dont want to save image but just displya it
plt.show()
print('*** Program ended ***')
################################################################################################
# name: heatmap-01.py
# desc: heatmap with matplotlib
# date: 2018-07-03
# Author: conquistadorjd
################################################################################################
from matplotlib import pyplot as plt
import numpy as np
print('*** Program Started ***')
uniform_data = np.random.rand(10, 12)
# fig, ax = plt.subplots()
# im = ax.imshow(uniform_data)
########## second way
# fig, ax = plt.subplots()
plt.imshow(uniform_data)
# Saving image
plt.savefig('heatmap-01.png')
# In case you dont want to save image but just displya it
plt.show()
print('*** Program ended ***')
################################################################################################
# name: heatmap-02.py
# desc: heatmap with seaborn
# date: 2018-07-03
# Author: conquistadorjd
################################################################################################
from matplotlib import pyplot as plt
import numpy as np
import seaborn as sns
print('*** Program Started ***')
sns.set()
np.random.seed(0)
uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_data)
# Saving image
plt.savefig('heatmap-02.png')
# In case you dont want to save image but just displya it
plt.show()
print('*** Program ended ***')
################################################################################################
# name: heatmap-03.py
# desc: heatmap with seaborn
# date: 2018-07-03
# Author: conquistadorjd
################################################################################################
from matplotlib import pyplot as plt
import numpy as np
import seaborn as sns
print('*** Program Started ***')
sns.set()
flights = sns.load_dataset("flights")
flights = flights.pivot("month", "year", "passengers")
# print(flights)
# print(type(flights))
# cust_color = ["#9b59b6", "#3498db", "#95a5a6", "#e74c3c", "#34495e", "#2ecc71"]
# BuGn_r,Blues,BrBG,cubehelix,sns.cubehelix_palette(8),sns.light_palette("green"),sns.light_palette("navy", reverse=False)
ax = sns.heatmap(flights,annot=False,linewidths=0.0,cbar=True,cmap="BuGn_r")
# Saving image
plt.savefig('heatmap-03.png')
# In case you dont want to save image but just displya it
plt.show()
print('*** Program ended ***')
################################################################################################
# name: histogram-01.py
# desc: histogram simple example
# date: 2018-07-03
# Author: conquistadorjd
################################################################################################
from matplotlib import pyplot as plt
import numpy as np
print('*** Program Started ***')
N_points = 100000
n_bins = 20
# Generate a normal distribution, center at x=0 and y=5
# x = np.random.randn(N_points)
# y = .4 * x + np.random.randn(100000) + 5
# print(x)
# print(y)
# # plt.hist(x, bins=n_bins)
# plt.hist(x, bins='auto')
rng = np.random.RandomState(10) # deterministic random data
a = np.hstack((rng.normal(size=1000),rng.normal(loc=5, scale=2, size=1000)))
plt.hist(a, bins='auto',color='#1dd5e2',facecolor='red') # arguments are passed to np.histogram
plt.title("Histogram with 'auto' bins")
# Saving image
plt.savefig('histogram-01.png')
# In case you dont want to save image but just displya it
plt.show()
print('*** Program ended ***')
################################################################################################
# name: histogram-02.py
# desc: histogram simple example
# date: 2018-07-03
# Author: conquistadorjd
################################################################################################
from matplotlib import pyplot as plt
import numpy as np
print('*** Program Started ***')
# Fixing random state for reproducibility
np.random.seed(19680801)
mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
# the histogram of the data
# n, bins, patches = plt.hist(x, 50, density=True, facecolor='g', alpha=0.75)
# plt.hist(x, bins='auto', density=True, facecolor='g', alpha=0.75)
plt.hist(x, 50, density=True, facecolor='g', alpha=0.75,cumulative=False,orientation='vertical', log=True)
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ')
# plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
# plt.axis([40, 160, 0, 0.03])
plt.grid(True)
# Saving image
plt.savefig('histogram-02.png')
# In case you dont want to save image but just displya it
plt.show()
print('*** Program ended ***')
################################################################################################
# name: lineplot_01_plot_char.py
# desc: various parameters
# date: 2018-12-24
# Author: conquistadorjd
###################################################################################################################
# version control
# 2018-12-24 : First version created in python3.6
###################################################################################################################
from matplotlib import pyplot as plt
import numpy as np
x= np.array([1,2,3,4,5,6,7,8,9,10])
y= np.array([1,2,4,9,15,18,22,29,39,50])
#plt.scatter(x,y)
plt.plot(x,y,color='green',alpha=0.5,label="test graph")
plt.legend(loc=2)
plt.title('linear graph')
plt.xlabel('Sample x Axis')
plt.ylabel('Sample y Axis')
plt.xlim( 1, 20 ) # set the xlim to xmin, xmax
plt.ylim( 1, 20 ) # set the ylim to ymin, ymax
plt.yticks( [5,15,20] )
plt.xticks( [5,7,15,20,25], ('Tom', 'Dick', 'Harry', 'Sally', 'Sue') )
plt.yscale('linear') # The available scales are: ‘linear’ | ‘log’ | ‘logit’ | ‘symlog’
plt.xscale('linear') # The available scales are: ‘linear’ | ‘log’ | ‘logit’ | ‘symlog’
plt.hlines(y=15, xmin=1, xmax=15,linewidth=0.5, color='g')
plt.vlines(x=10, ymin=1, ymax=15,linewidth=0.5, color='g')
plt.grid(color='g', linestyle='--', linewidth=0.5,markevery=int)
# plt.fill_betweenx(y, x1, x2=0, where=None, step=None, hold=None, data=None, **kwargs)
# Adding a point on graph
x1=[5,15.5]
y1=[5,15.5]
plt.scatter(x1,y1,color='red')
plt.show()
################################################################################################
# name: pyplot-01.py
# desc: First plot using pyplot
# date: 2018-07-1
# Author: conquistadorjd
################################################################################################
from matplotlib import pyplot as plt
import numpy as np
print('*** Program Started ***')
x= np.arange(1,17,1)
y= np.array([1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4])
plt.plot(x,y)
# Saving image
# plt.savefig('pyplot-01.png')
# In case you dont want to save image but just displya it
plt.show()
print('*** Program ended ***')
################################################################################################
# name: pyplot-03.py
# desc: Plotting with line properties
# date: 2018-07-1
# Author: conquistadorjd
################################################################################################
from matplotlib import pyplot as plt
import numpy as np
print('*** Program Started ***')
x= np.arange(1,17,1)
y= np.array([1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4])
plt.plot(x,y,linewidth=1.0, linestyle='--',marker='v',color='g',label='first')
# Saving image
# plt.savefig('pyplot-03.png')
# In case you dont want to save image but just displya it
plt.show()
print('*** Program ended ***')
################################################################################################
# name: scatterplot-00.py
# desc: Simple scatter plot
# date: 2018-06-15
# Author: conquistadorjd
################################################################################################
from matplotlib import pyplot as plt
import numpy as np
print('*** Program Started ***')
x= np.arange(1,17,1)
# y= np.arange(2,34,2)
y= np.random.randint(2, 50,size=16)
plt.scatter(x,y,s=None, marker='o',color='g',edgecolors='g',alpha=0.9,label="Jagur")
# labels
plt.xlabel('Sample x Axis')
plt.ylabel('Sample y Axis')
plt.title('This is scatter plot using matplotlib')
plt.legend(loc=2)
# Saving image
plt.savefig('scarrerplot-00.png')
# In case you dont want to save image but just displya it
plt.show()
print('*** Program ended ***')
################################################################################################
# name: scatterplot-01.py
# desc: Complex Scatter plot
# date: 2018-06-15
# Author: conquistadorjd
################################################################################################
from matplotlib import pyplot as plt
import numpy as np
print('*** Program Started ***')
x= np.arange(1,17,1)
y1= np.random.randint(2, 50,size=16)
y2= np.random.randint(2, 50,size=16)
plt.scatter(x,y1,s=None, marker='o',color='g',edgecolors='g',alpha=0.9,label="Jagur")
plt.scatter(x,y2,s=None, marker='*',color='r',edgecolors='r',alpha=0.9,label="Range Rover")
plt.xlabel('Sample x Axis')
plt.ylabel('Sample y Axis')
plt.title('This is scatter plot using matplotlib')
plt.legend(loc=2)
# Saving image
plt.savefig('scarrerplot-01.png')
# In case you dont want to save image but just displya it
plt.show()
print('*** Program ended ***')
################################################################################################
# name: subplot-01.py
# desc: Fsubplot using matplotlib
# date: 2018-07-1
# Author: conquistadorjd
################################################################################################
import matplotlib.pyplot as plt
import numpy as np
print('*** Program Started ***')
t= np.arange(1,17,1)
y1= np.random.randint(1, 16,size=16)
y2= np.random.randint(1, 32,size=16)
y3= np.random.randint(1, 48,size=16)
ax1 = plt.subplot(311)
plt.plot(t, y1,label="Jagur")
plt.xlabel('Sample x Axis')
plt.ylabel('Sample y Axis')
plt.legend(loc=2)
plt.title('subplot')
# plt.grid(True)
# plt.yscale('log')
# make these tick labels invisible
# plt.setp(ax1.get_xticklabels(), visible=False , fontsize=10)
# share x only
ax2 = plt.subplot(312, sharex=ax1)
plt.plot(t, y2,label="Range Rover")
plt.xlabel('Sample x Axis')
plt.ylabel('Sample y Axis')
plt.legend(loc=2)
# plt.title('linear')
# make these tick labels invisible
# plt.setp(ax2.get_xticklabels(), visible=False, fontsize=10)
# share x and y
ax3 = plt.subplot(313, sharex=ax1)
plt.plot(t, y3, label="Land Rover")
plt.xlabel('Sample x Axis')
plt.ylabel('Sample y Axis')
plt.legend(loc=2)
# make these tick labels visible with required font size
# plt.setp(ax3.get_xticklabels(), visible=True, fontsize=10)
# Saving image
plt.savefig('subplot-01.png')
# In case you dont want to save image but just displya it
plt.show()
print('*** Program ended ***')
################################################################################################
# name: subplot-02.py
# desc: Subplot using matplotlib grid 2*2
# date: 2018-07-1
# Author: conquistadorjd
################################################################################################
import matplotlib.pyplot as plt
import numpy as np
print('*** Program Started ***')
t= np.arange(1,17,1)
y1= np.random.randint(1, 16,size=16)
y2= np.random.randint(1, 32,size=16)
y3= np.random.randint(1, 48,size=16)
y4= np.random.randint(1, 48,size=16)
ax1 = plt.subplot(221)
plt.plot(t, y1,label="Jagur")
plt.xlabel('Sample x Axis')
plt.ylabel('Sample y Axis')
plt.legend(loc=2)
plt.title('subplot')
# make these tick labels invisible
# plt.setp(ax1.get_xticklabels(), visible=False , fontsize=10)
# share x only
ax2 = plt.subplot(222, sharex=ax1, sharey=ax1)
plt.plot(t, y2,label="Range Rover")
plt.xlabel('Sample x Axis')
plt.ylabel('Sample y Axis')
plt.legend(loc=2)
plt.title('subplot')
# make these tick labels invisible
# plt.setp(ax2.get_xticklabels(), visible=False, fontsize=10)
# share x and y
ax3 = plt.subplot(223,sharex=ax1, sharey=ax1)
plt.plot(t, y3,label="Land Rover")
plt.xlabel('Sample x Axis')
plt.ylabel('Sample y Axis')
plt.legend(loc=2)
# plt.title('subplot')
# make these tick labels visible with required font size
# plt.setp(ax3.get_xticklabels(), visible=True, fontsize=10)
# share x and y
ax4 = plt.subplot(224,sharex=ax1, sharey=ax1)
plt.plot(t, y4,label="Tata Motors")
plt.xlabel('Sample x Axis')
plt.ylabel('Sample y Axis')
plt.legend(loc=2)
# plt.title('subplot')
# make these tick labels visible with required font size
# plt.setp(ax3.get_xticklabels(), visible=True, fontsize=10)
# Saving image
plt.savefig('subplot-02.png')
# In case you dont want to save image but just displya it
plt.show()
print('*** Program ended ***')
#####################################################################################################################
# name: subplot-03.py
# desc: First row having two columns and second row having only 1 column
# date: 2018-07-1
# Author: conquistadorjd
######################################################################################################################
import matplotlib.pyplot as plt
import numpy as np
print('*** Program Started ***')
t= np.arange(1,17,1)
y1= np.random.randint(1, 16,size=16)
y2= np.random.randint(1, 32,size=16)
y3= np.random.randint(1, 48,size=16)
y4= np.random.randint(1, 48,size=16)
ax1 = plt.subplot(221)
plt.plot(t, y1,label="Jagur")
plt.xlabel('Sample x Axis')
plt.ylabel('Sample y Axis')
plt.legend(loc=2)
plt.title('subplot')
# make these tick labels invisible
# plt.setp(ax1.get_xticklabels(), visible=False , fontsize=10)
# share x only
ax2 = plt.subplot(222, sharex=ax1, sharey=ax1)
plt.plot(t, y2,label="Range Rover")
plt.xlabel('Sample x Axis')
plt.ylabel('Sample y Axis')
plt.legend(loc=2)
plt.title('subplot')
# make these tick labels invisible
# plt.setp(ax2.get_xticklabels(), visible=False, fontsize=10)
# share x and y
ax3 = plt.subplot(212,sharex=ax1, sharey=ax1)
plt.plot(t, y3,label="Land Rover")
plt.xlabel('Sample x Axis')
plt.ylabel('Sample y Axis')
plt.legend(loc=2)
# plt.title('subplot')
# make these tick labels visible with required font size
# plt.setp(ax3.get_xticklabels(), visible=True, fontsize=10)
# Saving image
plt.savefig('subplot-03.png')
# In case you dont want to save image but just displya it
plt.show()
print('*** Program ended ***')
################################################################################################
# name: subplot-11.py
# desc: Subplot using matplotlib
# date: 2018-12-24
# Author: conquistadorjd
################################################################################################
import matplotlib.pyplot as plt
import numpy as np
print('*** Program Started ***')
t= np.arange(1,17,1)
y1= np.random.randint(1, 16,size=16)
y2= np.random.randint(1, 32,size=16)
y3= np.random.randint(1, 48,size=16)
ax1 = plt.subplot2grid((3,3), (0,0), colspan=3, rowspan=1)
plt.plot(t, y1)
# share x only
ax2 = plt.subplot2grid((3,3), (1,0), colspan=3,rowspan=1,sharex=ax1)
plt.plot(t, y2)
# share x and y
ax3 = plt.subplot2grid((3,3), (2, 0), colspan=3,rowspan=1,sharex=ax1)
plt.plot(t, y3)
# Saving image
plt.savefig('subplot-11.png')
# In case you dont want to save image but just displya it
plt.show()
print('*** Program ended ***')
################################################################################################
# name: subplot-12.py
# desc: Subplot using matplotlib
# date: 2018-12-24
# Author: conquistadorjd
################################################################################################
import matplotlib.pyplot as plt
import numpy as np
print('*** Program Started ***')
t= np.arange(1,17,1)
t2= np.arange(1,49,1)
y1= np.random.randint(1, 16,size=16)
y2= np.random.randint(1, 32,size=48)
y3= np.random.randint(1, 48,size=16)
y4= np.random.randint(1, 64,size=16)
y5= np.random.randint(1, 124,size=16)
ax1 = plt.subplot2grid((5,3), (0,0), colspan=3, rowspan=1)
plt.plot(t, y1)
ax2 = plt.subplot2grid((5,3), (1,0), colspan=2,rowspan=1)
plt.plot(t2, y2)
ax3 = plt.subplot2grid((5,3), (1, 2), colspan=2,rowspan=2,sharex=ax1)
plt.plot(t, y3)
ax4 = plt.subplot2grid((5,3), (2, 0), colspan=2,rowspan=1)
plt.plot(t, y4)
ax5 = plt.subplot2grid((5,3), (3, 0), colspan=3,rowspan=2,sharex=ax1)
plt.plot(t*2, y5)
# ax6 = plt.subplot2grid((5,3), (4, 0), colspan=3,rowspan=1,sharex=ax1)
# plt.plot(t, y3)
# Saving image
plt.savefig('subplot-12.png')
# In case you dont want to save image but just displya it
plt.show()
print('*** Program ended ***')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment