1 / 12

Matplotlib

Matplotlib has various functions that can be used to plot the graph. It has features for plotting the inline graph and plotting several graphs in one canvas is easily possible using matplotlib.Moreover, it is also possible to control the scale of the graph, the aspect ratio and the DPI of the plots in matplotlib.The subplots() function helps you to monitor the axes.The following tutorial deals with the above mentioned concepts.<br><br>To learn more visit: http://bit.ly/DatascienceCourse

Download Presentation

Matplotlib

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Plotting an inline graph •Inline graph can be plotted using matplotlib.It is a graph that contains a main graph and a subgraph in the same canvas. Method to plot an inline graph: fig = plt.figure() axes1 = fig.add_axes([0.1, 0.1, 0.9,0.9]) #l,b,w,h -[0 and 1] axes2 = fig.add_axes([0.55, 0.15, 0.4,0.4]) #l,b,w,h -[0 and 1] axes1.plot(arr1, arr2) axes1.set_xlabel('Plotting arr1 on X Axis') axes1.set_ylabel('Plotting arr2 on Y Axis') axes1.set_title('Logarithmic graph') axes2.plot(arr2, arr1) axes2.set_xlabel('Plotting arr2 on X Axis') axes2.set_ylabel('Plotting arr1 on Y Axis') axes2.set_title('Logarithmic graph')

  2. • O/P:

  3. Subplots() function • This function manages axes and plots the graph automatically. • It returns 2 values in the form of tuple. 1.figure 2.axes • Eg: fig, axes = plt.subplots() axes.plot(arr1, arr2) axes.set_xlabel('Plotting arr1 on X Axis') axes.set_ylabel('Plotting arr2 on Y Axis') axes.set_title('Logarithmic graph') • O/P:

  4. Plotting multiple graph in 1 canvas using OOA • Syntax:fig,axes=plt.subplots(nrows,ncols) • Eg: fig, axes = plt.subplots(nrows = 3, ncols = 2) axes[2,1].plot(arr1,arr2) • O/P:

  5. Figure size, Aspect Ratio and DPI • Matplotlib allows the aspect ratio, DPI and figure size to the Figure object the figsize and dpi keyword arguments. • figsize is a tuple of the width and height of the figure in inches, and dpi is the dots-per-inch (pixel per inch). • Higher the dpi ,clearer the image. be specified created when using is

  6. • Eg: fig = plt.figure(figsize = (5,4), dpi = 100) axes1 = fig.add_axes([0.1, 0.1, 0.9,0.9]) #l,b,w,h [0 and 1] axes2 = fig.add_axes([0.55, 0.15, 0.4,0.4]) #l,b,w,h [0 and 1] axes1.plot(arr1, arr2) axes1.set_xlabel('Plotting arr1 on X Axis') axes1.set_ylabel('Plotting arr2 on Y Axis') axes1.set_title('Logarithmic graph') axes2.plot(arr2, arr1) axes2.set_xlabel('Plotting arr2 on X Axis') axes2.set_ylabel('Plotting arr1 on Y Axis') axes2.set_title('Logarithmic graph') • TO save the figure: fig.savefig('log_plot.png', dpi= 1000)

  7. • O/P:

  8. Labels and titles • Figure titles:A title can be added to each axis instance in a figure. To set the title, use the set_title method in the axes instance. • Axis labels:Similarly, with the methods set_xlabel and set_ylabel, we can set the labels of the X and Y axes. • Legends:Legends for curves in a figure can be added with the legend. – The legend function takes an optional keyword argument loc that can be used to specify where in the figure the legend is to be drawn – Use the label="label text" keyword argument when plots or other objects are added to the figure. – The following adjust the location: – ax.legend(loc=0) # let matplotlib decide the optimal location ax.legend(loc=1) # upper right corner ax.legend(loc=2) # upper left corner ax.legend(loc=3) # lower left corner ax.legend(loc=4) # lower right corner

  9. Example • fig = plt.figure() axes1 = fig.add_axes([0.1, 0.1, 0.9,0.9]) #l,b,w,h - [0 and 1] axes2 = fig.add_axes([0.55, 0.15, 0.4,0.4]) #l,b,w,h - [0 and 1] axes1.plot(arr1, arr2, label = "arr2 = log(arr1)") axes1.set_xlabel('Plotting arr1 on X Axis') axes1.set_ylabel('Plotting arr2 on Y Axis') axes1.set_title('Logarithmic graph') axes1.legend() axes2.plot(arr2, arr1, label = "arr1 = log(arr2)") axes2.set_xlabel('Plotting arr2 on X Axis') axes2.set_ylabel('Plotting arr1 on Y Axis') axes2.set_title('Logarithmic graph') axes2.legend()

  10. O/P:

  11. • Eg: fig, ax = plt.subplots() ax.plot(x, x**2, label="y = x**2") ax.plot(x, x**3, label="y = x**3") ax.legend(loc=2) # upper left corner ax.set_xlabel('x') ax.set_ylabel('y') ax.set_title('title') • O/P:

More Related