Matplotlib PowerPoint PPT Presentation

presentation player overlay
About This Presentation
Transcript and Presenter's Notes

Title: Matplotlib


1
PLOTTING AN INLINE GRAPH
2
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 graphfig  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')

3
  • O/P

4
Subplots() function
  • This function manages axes and plots the graph
    automatically.
  • It returns 2 values in the form of tuple.
  • 1.figure
  • 2.axes
  • Egfig, axes  plt.subplots()axes.plot(arr1, arr
    2)axes.set_xlabel('Plotting arr1 on X Axis')axes
    .set_ylabel('Plotting arr2 on Y Axis')axes.set_ti
    tle('Logarithmic graph')
  • O/P

5
Plotting multiple graph in 1 canvas using OOA
  • Syntaxfig,axesplt.subplots(nrows,ncols)
  • Egfig, axes  plt.subplots(nrows  3, ncols  2)
    axes2,1.plot(arr1,arr2)
  • O/P

6
Figure size, Aspect Ratio and DPI
  • Matplotlib allows the aspect ratio, DPI and
    figure size to be specified when
    the Figure object is created using
    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.

7
  • Egfig  plt.figure(figsize  (5,4), dpi  100)a
    xes1  fig.add_axes(0.1, 0.1, 0.9,0.9)  l,b,w,h
     0 and 1axes2  fig.add_axes(0.55, 0.15, 0.4,0
    .4)  l,b,w,h 0 and 1axes1.plot(arr1, arr2)ax
    es1.set_xlabel('Plotting arr1 on X Axis')axes1.se
    t_ylabel('Plotting arr2 on Y Axis')axes1.set_titl
    e('Logarithmic graph')axes2.plot(arr2, arr1)axes
    2.set_xlabel('Plotting arr2 on X Axis')axes2.set_
    ylabel('Plotting arr1 on Y Axis')axes2.set_title(
    'Logarithmic graph')
  • TO save the figurefig.savefig('log_plot.png', dp
    i 1000)

8
  • O/P

9
Labels and titles
  • Figure titlesA 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 labelsSimilarly, with the
    methods set_xlabel and set_ylabel, we can set the
    labels of the X and Y axes.
  • LegendsLegends 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(loc0) let matplotlib
    decide the optimal location ax.legend(loc1)
    upper right corner ax.legend(loc2)
    upper left corner ax.legend(loc3)
    lower left corner ax.legend(loc4)
    lower right corner

10
Example
  • fig  plt.figure()axes1  fig.add_axes(0.1, 0.1,
     0.9,0.9)  l,b,w,h -0 and 1axes2  fig.add_ax
    es(0.55, 0.15, 0.4,0.4)  l,b,w,h -0 and 1axe
    s1.plot(arr1, arr2, label  "arr2  log(arr1)")ax
    es1.set_xlabel('Plotting arr1 on X Axis')axes1.se
    t_ylabel('Plotting arr2 on Y Axis')axes1.set_titl
    e('Logarithmic graph')axes1.legend()axes2.plot(a
    rr2, arr1, label  "arr1  log(arr2)")axes2.set_x
    label('Plotting arr2 on X Axis')axes2.set_ylabel(
    'Plotting arr1 on Y Axis')axes2.set_title('Logari
    thmic graph')axes2.legend()

11
O/P
12
  • Egfig, ax  plt.subplots()ax.plot(x, x2, labe
    l"y  x2")ax.plot(x, x3, label"y  x3")a
    x.legend(loc2)  upper left cornera
    x.set_xlabel('x')ax.set_ylabel('y')ax.set_title(
    'title')
  • O/P
Write a Comment
User Comments (0)
About PowerShow.com