# We will now learn how to make figures in Python ...

# We will now learn how to make figures in Python. # It is useful to know that # there are many useful softwares to make figures, such as # gnuplot (for free), xmgrace (for free), # Mathematica and Matlab (which require licenses).

# matplotlib is a plotting library for Python # pyplot is a module that provides a MATLAB-like interface # matplotlib.pyplot makes matplotlib work like MATLAB

# ---------------------------------------------# MORE INFO # # # # ----------------------------------------------

# ---------------------------------------------# ANIMATION: # #

robotics_lab_animation_example.txt # ----------------------------------------------

# ---------------------------------------------# DRAWINGS: # # ----------------------------------------------

# ---------------------------------------------# IF ANIMATION DOES NOT SHOW in SPYDER #

working-in-spyder

# You can go to: python > Preferences > IPython Console > Graphics > Backend # and change it from "Inline" to "Automatic"

# After changing values, do not forget to restart IDE (Spyder, PyCharm, etc.) # ----------------------------------------------

# ---------------------------------------------# Before anything, let us import # numpy

# and # matplolib.pyplot # and # math -- just in case we need it also

import matplotlib.pyplot as plt import numpy as np import math # ----------------------------------------------

# --------------------------# Example 1 # --------------------------# This plot makes a straigth line, where the points are # (x,y) = (0,1), (1,2), (2,3), (3, 4) # The y values are given with the list below # Since the x values were not given, # they are automatically generated by matplotlib print('EXAMPLE 1') plt.plot([1,2,3,4]) plt.ylabel('Label the y-axis') # Show the plot plt.show()

# --------------------------# Example 2 # --------------------------print() print('EXAMPLE 2') # The default choice for the plot in Example 1 # is a blue straight line, denoted by `b-` # If we wanted, red circles instead, we would need to # explicitly write 'ro' plt.plot([1,2,3,4],'ro') # 'ro' is equivalent to color='red' and linestyle='--' # so you could also USE: # ---------------------------------------------# plt.plot([1,2,3,4],color='red',linestyle='--') # ---------------------------------------------plt.ylabel('Label the y-axis') # Show the plot plt.show()

# ----------------------------------------------

# --------------------------# Example 3 # --------------------------print() print('EXAMPLE 3') # Here, we specify # --) x and y values # --) color and line type plt.plot([2,3,4,5], [4,9,16,25], 'ro') plt.plot([2,3,4,5], [4,9,16,25], 'b--') # --) range of the plot plt.axis([0, 6, 0, 30]) # --) axes labels plt.xlabel('Values of x') plt.ylabel('Values of y') # Show the plot plt.show() # # SHORTER ALTERNATIVE print() print('Or equivalently') x = [2,3,4,5] y = [4,9,16,25] plt.plot(x,y, 'ro', x,y,'b--') plt.axis([0, 6, 0, 30]) plt.xlabel('Values of x') plt.ylabel('Values of y') plt.show() #

# ----------------------------------------------

#

COMMENTS

# ----------------------------------------------

# From the plots above, we conclude that we just need

# the data for the x- and for y-axis in the form of LISTS.

# We can also use ARRAYS and the result is the same.

# Remember that elements can be appended to a LIST,

# but when using an ARRAY, the size has to be decided beforehand.

# In the example below, we will use ARRAYS.

# Two very convenient functions from NUMPY are

# 1)

LINSPACE

# linspace = returns evenly spaced numbers over a specified interval

# np.linspace(first number, last number, total number of numbers)

# For example

# np.linspace(0,2,11) gives

# array([0. , 0.2, 0.4, 0.6, 0.8, 1. , 1.2, 1.4, 1.6, 1.8, 2. ])

# 2)

ARANGE

# arange = Similar to linspace, but it uses a step size # np.arange(first number, last number + dx, dx =step size, increment) # For example # np.arange(0,2,0.2) gives # array([0. , 0.2, 0.4, 0.6, 0.8, 1. , 1.2, 1.4, 1.6, 1.8]) # NOTICE that above we did NOT get the final value 2. # ----------------------------------------------

# --------------------------# Example 4 (USING NUMPY) # --------------------------print() print('EXAMPLE 4: using numpy') # Here, we use numpy to generate the data # linspace = returns evenly spaced numbers over a specified interval x = np.linspace(0, 2, 100) # print(x) # SIMILAR # arange = Similar to linspace, but uses a step size # x = np.arange(0, 2, 0.02) # print(x) plt.plot(x, x, label='linear') plt.plot(x, x**2, label='quadratic') plt.plot(x, x**3, label='cubic')

plt.xlabel('x label') plt.ylabel('y label')

plt.title('Three-Curve Plot')

# Add a legend (note that in plt.plot, we have the curves names) plt.legend()

plt.show()

# --------------------------# Example 5 (SINE and COSINE) # --------------------------# SAVE AS PDF or PNG # --------------------------print() print('EXAMPLE 5: SAVE') # To Save, we give a name to the figure. # In the case below, it is called "outFig". # Many online examples, use simply "fig" outFig = plt.figure() # values of x, y1, and y2

x = np.arange(0, 10, 0.2) y1 = np.sin(x) y2 = np.cos(x) # sine plot: red dashed line plt.plot(x, y1, 'r--',label = 'sin') # cosine plot: black solid line plt.plot(x, y2, color='black',linestyle='-',linewidth=3.0,label = 'cos') # Show the legend plt.legend() # x and y labels (colors and fontsizes) plt.xlabel('Values of x', fontsize='12') plt.ylabel('Values of y',fontsize='18',color='red') # x and y TICKS (color, fontsize, weight, selection of values) plt.xticks(fontsize='12',weight='bold') plt.yticks([-1,0,1],[r'$-1.000$', r'$zero$',r'$one$'],color='blue') # Range of the plot plt.axis([0, 12, -1.5, 1.5]) # TITLE plt.title('Sin-Cos Plot') # Adding a GRID plt.grid(True) # Show the plot plt.show() # Save the figure: # bbox_inches='tight' guarantees that the figure is not chopped outFig.savefig("MyFig01.pdf",bbox_inches='tight') outFig.savefig("MyFig01.png",bbox_inches='tight')

# --------------------------# Example 6 LOG-PLOT # --------------------------# and FIGURE SIZE # --------------------------print() print('EXAMPLE 6: LOG') # Data x = np.arange(0, 5, 0.2) y = np.exp(x) plt.plot(x, y,'ro') plt.plot(x, y, color='black') plt.title('Linear Plot') plt.show()

# LOG plot # figsize=(6,2) = size of the panel in inches # width is 6in and height is 2in plt.figure(figsize=(6,2)) plt.yscale('log') plt.plot(x, y,'ro') plt.plot(x, y, color='black')

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download