Python Plotting With Matplotlib

Creating visually appealing figures and plots is a vital aspect of Data Science, and in Python, the go-to plotting package is matplotlib, written purely in Python. As one of the most widely used Python libraries for generating static, animated, and interactive visualizations, matplotlib offers both quick data visualization capabilities and the ability to produce professional-quality figures in various formats. Although highly customizable, finding the right settings for an attractive plot can be challenging. Furthermore, matplotlib optimizes performance, even for large arrays, by using NumPy arrays and other extension code.

Installing matplotlib

There are multiple ways to install matplotlib on your operating system, with the easiest approach being the use of the pip command. By simply executing pip install matplotlib, you can easily install the matplotlib library and start creating visualizations in Python. Open the command prompt, type the following command.

pip install matplotlib

Once you can successfully install matplotlib library , then you are ready to continue.

Example 1
from matplotlib import pyplot as plt plt.plot([10,20,30],[40,50,10]) plt.show()

In the above code, import pyplot from matplotlib - use pyplot to "plot" some data to the canvas in memory and then use plt.show(), which is pyplot, to show what you've got.


Python matplotlib
Example 2
from numpy import * import matplotlib.pyplot as plt x = arange(1001) y = mod(x,2.87) l1 = plt.hist(y,color='y',rwidth = 0.8) lx = plt.xlabel("y") ly = plt.ylabel("num(y)") tl = plt.title("y = mod(arange(1001),2.87)") plt.show()

pyplot from matplotlib
Example-3
from numpy import * import matplotlib.pyplot as plt x = linspace(0,10,51) y = linspace(0,8,41) (X,Y) = meshgrid(x,y) a = exp(-((X-2.5)**2 + (Y-4)**2)/4) - exp(-((X-7.5)**2 + (Y-4)**2)/4) c = plt.contour(x,y,a) l = plt.clabel(c) lx = plt.xlabel("x") ly = plt.ylabel("y") plt.show()

matplot python data science
Example-4
from matplotlib import pyplot as plt from matplotlib import style style.use('ggplot') x = [5,8,10] y = [12,16,6] x2 = [6,9,11] y2 = [6,15,7] plt.bar(x, y, align='center') plt.bar(x2, y2, color='g', align='center') plt.title('Example-4') plt.ylabel('Y-axis') plt.xlabel('X-axis') plt.show()

python data science libraries
Example-5
from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import numpy as np fig = plt.figure() ax = fig.add_subplot(111,projection='3d') for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]): xs = np.arange(20) ys = np.random.rand(20) cs = [c] * len(xs) cs[0] = 'c' ax.bar(xs, ys, zs=z, zdir='y', color=cs, alpha=0.8) ax.set_xlabel('X-Axis') ax.set_ylabel('Y-Axis') ax.set_zlabel('Z-Axis') plt.show()

Python data science tutorial

Conclusion

Python Plotting with Matplotlib offers a straightforward and versatile means to create compelling visualizations in Python. Utilizing the pip command to install matplotlib allows users to seamlessly access its powerful plotting capabilities and begin producing data-driven graphics with ease.