plotting with matplotlib code example

Example 1: how to plot a graph using matplotlib

from matplotlib import pyplot as plt
plt.plot([0, 1, 2, 3, 4, 5], [0, 1, 4, 9, 16, 25])
plt.show()

Example 2: matplotlib plot

import matplotlib.pyplot as plt
fig = plt.figure(1)	#identifies the figure 
plt.title("Y vs X", fontsize='16')	#title
plt.plot([1, 2, 3, 4], [6,2,8,4])	#plot the points
plt.xlabel("X",fontsize='13')	#adds a label in the x axis
plt.ylabel("Y",fontsize='13')	#adds a label in the y axis
plt.legend(('YvsX'),loc='best')	#creates a legend to identify the plot
plt.savefig('Y_X.png')	#saves the figure in the present directory
plt.grid()	#shows a grid under the plot
plt.show()

Example 3: plot in python

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_axes((0.1,0.4,0.5,0.5))

ax1.set_title('Title of Plot')
ax1.set_xlabel('X')
ax1.set_ylabel('Y')

ax1.plot(X,Y,c='black',linestyle='--',linewidth=0.5)
ax1.scatter(X,Y,c='red',s=0.5)
ax1.legend(['Line','Scatter'])

ax1.xaxis.set_ticks([0.0,0.1,0.2,0.3])
ax1.yaxis.set_ticks([0.0,0.1,0.2,0.3])

ax1.grid(which='major')
ax1.tick_params(direction='in')

txt = 'Write something on the graph page\
		\n over one or more lines.'

fig.text(0.5,0.05,txt,ha='center')

fig.savefig('Figure.pdf')

Example 4: python matplt

import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')
plt.show()

Example 5: show graph matplotlib axes

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv("filesc/file1.csv")
df.head()

BBox = ((df.x.min(),   df.x.max(), df.y.min(), df.y.max()))

ruh_m = plt.imread('map.png')

print(BBox)

fig, ax = plt.subplots(figsize = (8,7))
ax.scatter(df.x, df.y, zorder=1, alpha= 0.2, c='b', s=10)
ax.set_title('Plotting Spatial Data on Map')
ax.set_xlim(BBox[0],BBox[1])
ax.set_ylim(BBox[2],BBox[3])
ax.imshow(ruh_m, zorder=0, extent = BBox, aspect= 'equal')
plt.show()

Example 6: how do a plot on matplotlib python

import matplotlib.pyplot as plt
%matplotlib inline
plt.plot(data)
#this is not nessisary but makes your plot more readable
plt.ylabel('y axis means ...')
plt.xlabel('x axis means ...')

Tags:

Misc Example