479 views

Uploaded by David Vivas

python y gradientes

- numerical_matrix_analysis_linear_systems_and_lea.pdf
- Python Strings Reference Card
- Python Basic concept
- Numerical Methods Book.pdf
- MIT Numerical PDE
- Python quick reference
- Numerical Ability for Bank Exam
- LaTeX Tutorial
- Numerical and experimental studies of nonlinear wave loads of ships
- The Navier-Stokes Equations Theory And Numerical Methods
- solar cell numerical solution
- Python for You and Me
- Scientific Programming in Python
- EC 2-Manual of Numerical Methods in Concrete
- NUMERICAL MODELLING OF TIME DEPENDENT PORE PRESSURE RESPONSE INDUCED BY HELICAL PILE INSTALLATION
- Numerical Renaissance
- Python distutils
- Delaunay Triangulation and Meshing - George & Borouchaki
- How to Help Your Child Succeed in School 2005 Edition
- python design pattern

You are on page 1of 4

This guide assumes that you have already installed NumPy and Matplotlib for your Python distribution.

You can check if it is installed by importing it:

import numpy as np

import matplotlib.pyplot as plt

For those of you familiar with MATLAB, the basic Matplotlib syntax is very similar.

Line plots

The basic syntax for creating line plots is plt.plot(x,y), where x and y are arrays of the same length that

specify the (x, y) pairs that form the line. For example, lets plot the cosine function from 2 to 1. To do

so, we need to provide a discretization (grid) of the values along the x-axis, and evaluate the function on

each x value. This can typically be done with numpy.arange or numpy.linspace.

xvals = np.arange(-2, 1, 0.01)

yvals = np.cos(xvals)

plt.plot(xvals, yvals)

# Create line plot with yvals against xvals

plt.show()

You should put the plt.show command last after you have made all relevant changes to the plot. You can

create multiple figures by creating new figure windows with plt.figure(). To output all these figures at

once, you should only have one plt.show command at the very end. Also, unless you turned the interactive

mode on, the code will be paused until you close the figure window.

Suppose we want to add another plot, the quadratic approximation to the cosine function. We do so

below using a different color and line type. We also add a title and axis labels, which is highly recommended

in your own work. Also note that we moved the plt.show command to the end so that it shows both plots.

newyvals = 1 - 0.5 * xvals**2

plt.title(Example plots)

plt.xlabel(Input)

plt.ylabel(Function values)

plt.show()

The third parameter supplied to plt.plot above is an optional format string. The particular one specified

above gives a red dashed line. See the extensive Matplotlib documentation online for other formatting

commands, as well as many other plotting properties that were not covered here:

http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.plot

1

Contour plots

The basic syntax for creating contour plots is plt.contour(X,Y,Z,levels). To trace a contour, plt.contour

requires a 2-D array Z that specifies function values on a grid. The underlying grid is given by X and Y,

either both as 2-D arrays with the same shape as Z, or both as 1-D arrays where len(X) is the number of

columns in Z and len(Y) is the number of rows in Z.

In most situations it is more convenient to work with the underlying grid (i.e., the former representation).

The meshgrid function is useful for constructing 2-D grids from two 1-D arrays. It returns two 2-D arrays

X,Y of the same shape, where each element-wise pair specifies an underlying (x, y) point on the grid. Function

values on the grid Z can then be calculated using these X,Y element-wise pairs.

plt.figure()

X,Y = np.meshgrid(xlist, ylist)

Z = np.sqrt(X**2 + Y**2)

We also need to specify the contour levels (of Z) to plot. You can either specify a positive integer for the

number of automatically- decided contours to plot, or you can give a list of contour (function) values in the

levels argument. For example, we plot several contours below:

plt.contour(X, Y, Z, [0.5, 1.0, 1.2, 1.5], colors = k, linestyles = solid)

plt.show()

Note that we also specified the contour colors and linestyles. By default, negative contours are given by

dashed lines, hence we specified solid. Again, many properties are described in the Matplotlib specification:

http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.contour

The function considered above should actually have circular contours. Unfortunately, due to the different

scales of the axes, the figure likely turned out to be flattened and the contours appear like ellipses. This

is undesirable, for example, if we wanted to visualize 2-D Gaussian covariance contours. We can force the

aspect ratio to be equal with the following command (placed before plt.show):

plt.axes().set_aspect(equal)

Finally, suppose we want to zoom in on a particular region of the plot. We can do this by changing the

axis limits (again before plt.show). The input list to plt.axis has form [xmin, xmax, ymin, ymax].

plt.axis([-1.0, 1.0, -0.5, 0.5])

Notice that the aspect ratio is still equal after changing the axis limits. Also, the commands above only

change the properties of the current axis. If you have multiple figures you will generally have to set them

for each figure before calling plt.figure to create the next figure window.

You can find out how to set many other axis properties at:

http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.axis

http://matplotlib.sourceforge.net/api/axes_api.html#matplotlib.axes

The final link covers many things, but most functions for changing axis properties begin with set_.

2

Figures

Figure 3: Setting the aspect ratio to be equal and zooming in on the contour plot.

Code

import numpy as np

import matplotlib.pyplot as plt

xvals = np.arange(-2, 1, 0.01)

yvals = np.cos(xvals)

plt.plot(xvals, yvals)

# plt.show()

plt.title(Example plots)

plt.xlabel(Input)

plt.ylabel(Function values)

# plt.show()

plt.figure()

X,Y = np.meshgrid(xlist, ylist)

Z = np.sqrt(X**2 + Y**2)

plt.axes().set_aspect(equal)

plt.show()

- numerical_matrix_analysis_linear_systems_and_lea.pdfUploaded bymarianandrone
- Python Strings Reference CardUploaded byaltkommein
- Python Basic conceptUploaded byGuru Prasanna V
- Numerical Methods Book.pdfUploaded byCengiz Görkem Ela
- MIT Numerical PDEUploaded byKruger Joe
- Python quick referenceUploaded byRahul
- Numerical Ability for Bank ExamUploaded bycurrentaffairsquiz
- LaTeX TutorialUploaded bytcdnetsoc
- Numerical and experimental studies of nonlinear wave loads of shipsUploaded byrammi4978
- The Navier-Stokes Equations Theory And Numerical MethodsUploaded byMiguel Angel Morua Ramirez
- solar cell numerical solutionUploaded byBhargav Chaitanya
- Python for You and MeUploaded bystvx007
- Scientific Programming in PythonUploaded bystreetcribdealer
- EC 2-Manual of Numerical Methods in ConcreteUploaded byjan_matej5651
- NUMERICAL MODELLING OF TIME DEPENDENT PORE PRESSURE RESPONSE INDUCED BY HELICAL PILE INSTALLATIONUploaded byAlex
- Numerical RenaissanceUploaded bypabsrams
- Python distutilsUploaded byVasanth Kumar
- Delaunay Triangulation and Meshing - George & BorouchakiUploaded byKarthiganesbe85
- How to Help Your Child Succeed in School 2005 EditionUploaded byKarthiganesbe85
- python design patternUploaded byMeo Map
- Advanced Numerical MethodsUploaded byvanbuyten
- Numerical Analysis 8th EditionpdfUploaded byguysarkis
- Functional Programming in Python-3Uploaded bykhazarhayat
- Elsevier - Handbook of Numerical Analysis V1 Part1 - Finite Difference MethodsUploaded byKarthiganesbe85
- Multithreading in PythonUploaded byTanoy Bose
- Numerical MethodsUploaded byAnuragSh1994
- Boundary Elements an Introductory Course - Brebbia and DominguezUploaded byKarthiganesbe85
- Mathematical Techniques for Engineers and Scientists _ Publications_ SPIEUploaded byKarthiganesbe85
- Advance numerical analysisUploaded byRavi Shankar
- Creating Network Graphs With PythonUploaded byiggix

- Puce Adm Parcial2Uploaded byDavid Vivas
- EcologicalUploaded byDavid Vivas
- investigacion de operacionesUploaded byDavid Vivas
- Ejercicios CAP4Uploaded byDavid Vivas
- Estadística II Aplicada -Administración Tercer NivelUploaded byDavid Vivas
- Learning Kernel Classifiers. Theory and AlgorithmsUploaded byngelectronic5226
- PUCE_ADM_PARCIAL2.pdfUploaded byDavid Vivas
- Newton - RaphsonUploaded byDavid Vivas
- Tercer Examen Parcial de Estadística AplicadaUploaded byDavid Vivas
- Caso NetflixUploaded byDavid Vivas
- Investigación de operacionesUploaded byDavid Vivas
- reducedregression (1)Uploaded byDavid Vivas
- Resúmen Pruebas de HipótesisUploaded byDavid Vivas
- Derivada de SchwarzUploaded byDavid Vivas
- 20170507 Mat0221 Examen Final - Ejercicios de Algebra LinealUploaded byDavid Vivas
- Curso Python para Principiantes.pdfUploaded byJuan
- OPERACIONES MORFOLOGICASUploaded byDavid Vivas
- sas programmUploaded byDavid Vivas
- Mas Sobre Listas de Python-MatricesUploaded byvaleriaeh
- Python ImagingUploaded bykhaukaronaldo5176
- funciones de creencia para aplicaciones en procesamiento de imagenesUploaded byDavid Vivas
- edicion de imagenesUploaded byDavid Vivas
- Distribucion Normal MultivariadaUploaded byDavid Vivas
- Distribucion Normal MultivariadaUploaded byDavid Vivas
- Problems Solved 2Uploaded byDavid Vivas
- 104150981 Solution Functional Analysis KreyszigUploaded byrossane_g
- Entender Python (Muy Bueno)Uploaded byDavid Vivas
- Automatas Celulares y La Regla de WolframUploaded byDavid Vivas
- Notas de PythonUploaded byDavid Vivas

- Carranza Torres 2004Uploaded byPatricio Cisternas
- Definite Integration Zella.in.pdfUploaded byKAPIL SHARMA
- Mathematics W16.pdfUploaded byChidiebere Samuel Okogwu
- Um String Code 2 0 eUploaded byhydrasearch
- Functions in C++.pdfUploaded byAnonymous EpmRbw
- b 02404014Uploaded byinventionjournals
- Laplace Transform PeriodicUploaded byasbadg
- Readings in Multiple Criteria Decision Aid - Carlos a. Bana e Costa (Eds)Uploaded byDionisio
- 23.Computer ApplicationUploaded byrajchawla
- Simulink TutorialUploaded bysivakkumar14
- MA1200 Chapter 7 Techniques of DifferentiationUploaded byWai Ho Choi
- Pu Test PaperUploaded by123musiqv
- Python TuplesUploaded byshivam
- mcsimUploaded byrishipath
- CS242 Stanford HW#1Uploaded byAnsh Shukla
- Loops Addendum MATLABUploaded byemongoloide
- Unit1 TestUploaded bynadia sykes
- Medical Imaging SystemsUploaded bygeoffsn
- EASI Modeling in FocusUploaded byrglencina
- Exam.pdfUploaded byValerioNucera
- A01Uploaded bylksoo
- Lab Exp 1Uploaded byNumanAbdullah
- Gharajedaghi Systems Thinking NotesUploaded byGregory Zobel
- 2513mathpdf.pdfUploaded bymarkgalut
- micro1Uploaded byDongfang Nie
- Mathematica-mainbook.pdfUploaded byfowzi81
- fminconUploaded bymrloadmovie
- Differentiabilty ContinuityUploaded bysmtm06
- Newington 2011 3U Prelim HY & Solutions.pdfUploaded byEren Sevince
- Se-mech Cpp AtulUploaded byAtul Kumbhar