diff options
Diffstat (limited to 'saving_plots')
-rw-r--r-- | saving_plots/quickref.tex | 5 | ||||
-rw-r--r-- | saving_plots/script.rst | 163 | ||||
-rw-r--r-- | saving_plots/slides.org | 99 | ||||
-rw-r--r-- | saving_plots/slides.tex | 185 |
4 files changed, 452 insertions, 0 deletions
diff --git a/saving_plots/quickref.tex b/saving_plots/quickref.tex new file mode 100644 index 0000000..e485ae0 --- /dev/null +++ b/saving_plots/quickref.tex @@ -0,0 +1,5 @@ +Saving a plot(png):\\ +{\ex \lstinline| savefig('filename.png')|} + +Saving a plot(pdf):\\ +{\ex \lstinline| savefig('filename.pdf')|} diff --git a/saving_plots/script.rst b/saving_plots/script.rst new file mode 100644 index 0000000..1134d48 --- /dev/null +++ b/saving_plots/script.rst @@ -0,0 +1,163 @@ +.. Objectives +.. ---------- + +.. At the end of this tutorial, you will be able to + +.. 1. Save plots using ``savefig()`` function. +.. #. Save plots in different formats. + + +.. Prerequisites +.. ------------- + +.. 1. should have ``ipython`` and ``pylab`` installed. +.. #. getting started with ``ipython``. +.. #. using plot command interactively. + +.. Author : Anoop Jacob Thomas <anoop@fossee.in> + Internal Reviewer : Puneeth + External Reviewer : + Language Reviewer : Bhanukiran + Checklist OK? : <10-11-2010, Anand, OK> [2010-10-05] + +.. #[Puneeth: Quickref missing.] + +======= +Savefig +======= + +{{{ Show the first slide }}} + +Hello and welcome to the tutorial saving plots. + +{{{ switch to next slide, outline slide }}} + +In this tutorial you will learn how to save plots using Python, saving +in different formats, and locating the file in the file system. + +.. #[Puneeth: file-system is too technical.] + +{{{ switch to next slide, a sine wave}}} + +Start your IPython interpreter with the command :: + + ipython -pylab + +As you know, it will start your IPython interpreter with the required +python modules for plotting and saving your plots. + +{{{ Open ipython }}} + +Now let us plot something, let us plot a sine wave from minus 3 pi to +3 pi. Let us start by calculating the required points for the plot. It +can be done using linspace as, :: + + x = linspace(-3*pi,3*pi,100) + +We have stored required points in x. Now let us plot the points using +the statement :: + + plot(x,sin(x)) + +{{{ Keep the plot open }}} + +Done! we have made a very basic sine plot, now let us see how to save +the plot for future use so that you can embed the plot in your +reports. + +.. #[Puneeth: All this is known stuff. You don't have to elaborate so +.. much on it. Just say, let us plot sin function from -3 pi to 3 +.. pi. Show the commands, and be done with it. ] + +{{{ switch to next slide, savefig() }}} + +{{{ Switch the focus to IPython interpreter window }}} + +For saving the plot, we will use ``savefig()`` function, and it has to be +done with the plot window open. The statement is, :: + + savefig('/home/fossee/sine.png') + +Notice that ``savefig`` function takes one argument which is the +filename. The last 3 characters after the ``.`` in the filename is the +extension or type of the file which determines the format in which you +want to save. + +.. #[Puneeth: removed mention of string] + +.. #[[Anoop: I think this treatment is better :) ]] + +{{{ Highlight the /home/fossee part using mouse movements }}} + +Also, note that we gave the full path or the absolute path to which we +want to save the file. + +{{{ Highlight the .png part using mouse movements }}} + +Here I have used an extension ``.png`` which means I want to save the +image as a PNG file. + +Now let us locate ``sine.png`` file saved. We saved the file to +``/home/fossee`` so let us navigate to ``/home/fossee`` using the +file browser. + +{{{ Open the browser, navigate to /home/fossee and highlight the file +sine.png }}} + +Yes, the file ``sine.png`` is here and let us check it. + +{{{ Open the file sine.png and show it for two-three seconds and then +close it and return to IPython interpreter, make sure the plot window +is still open, also don't close the file browser window }}} + +{{{ switch to next slide, More on savefig() }}} + +So in-order to save a plot, we use ``savefig`` function. ``savefig`` +can save the plot in many formats, such as pdf - portable document +format, ps - post script, eps - encapsulated post script, svg - +scalable vector graphics, png - portable network graphics which +support transparency etc. + +.. #[[slide must give the extensions for the files - Anoop]] + +{{{ switch to next slide, exercise 1 }}} + +Let us now try to save the plot in eps format. ``eps`` stands for +encapsulated post script, and it can be embedded in your latex +documents. Pause here and try to figure it out yourself. + +{{{ Switch focus to the already open plot window }}} + +We still have the sine plot with us, and now let us save the plot as +``sine.eps``. + +{{{ switch to next slide, solution 1 }}} + +{{{ Switch focus to IPython interpreter }}} + +Now, We will save the plot using the function ``savefig`` :: + + savefig('/home/fossee/sine.eps') + +{{{ Switch focus to file browser window }}} + +Now let us go to ``/home/fossee`` and see the new file created. + +{{{ Highlight the file sine.eps with a single mouse click for 2 +seconds and then double click and open the file }}} + +Yes! the new file ``sine.eps`` is here. + +{{{ switch to next slide, exercise 2 }}} + +Now you may try saving the same in pdf, ps, svg formats. + +{{{ Switch to summary slide }}} + +This brings us to the end of this tutorial, in this tutorial we +learned to save plots using the function ``savefig()``. Saving the +plots in different formats and locating the files in the file system. + +{{{ switch to Thank you slide }}} + +Thank you! diff --git a/saving_plots/slides.org b/saving_plots/slides.org new file mode 100644 index 0000000..4278516 --- /dev/null +++ b/saving_plots/slides.org @@ -0,0 +1,99 @@ +#+LaTeX_CLASS: beamer +#+LaTeX_CLASS_OPTIONS: [presentation] +#+BEAMER_FRAME_LEVEL: 1 + +#+BEAMER_HEADER_EXTRA: \usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent} +#+COLUMNS: %45ITEM %10BEAMER_env(Env) %10BEAMER_envargs(Env Args) %4BEAMER_col(Col) %8BEAMER_extra(Extra) +#+PROPERTY: BEAMER_col_ALL 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 :ETC + +#+LaTeX_CLASS: beamer +#+LaTeX_CLASS_OPTIONS: [presentation] + +#+LaTeX_HEADER: \usepackage[english]{babel} \usepackage{ae,aecompl} +#+LaTeX_HEADER: \usepackage{mathpazo,courier,euler} \usepackage[scaled=.95]{helvet} + +#+LaTeX_HEADER: \usepackage{listings} + +#+LaTeX_HEADER:\lstset{language=Python, basicstyle=\ttfamily\bfseries, +#+LaTeX_HEADER: commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen}, +#+LaTeX_HEADER: showstringspaces=false, keywordstyle=\color{blue}\bfseries} + +#+TITLE: Savefig +#+AUTHOR: FOSSEE +#+EMAIL: info@fossee.in +#+DATE: 2010-10-11 Mon + +#+DESCRIPTION: +#+KEYWORDS: +#+LANGUAGE: en +#+OPTIONS: H:3 num:nil toc:nil \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t +#+OPTIONS: TeX:t LaTeX:nil skip:nil d:nil todo:nil pri:nil tags:not-in-toc + +* Outline + - Saving plots. + - Plotting in different formats. + - Locating the file in the file system. + +* Creating a basic plot + Plot a sine wave from -3pi to 3pi. + #+begin_src python + In []: x = linspace(-3*pi,3*pi,100) + + In []: plot(x, sin(x)) + #+end_src +* savefig() +** savefig() - to save plots + : syntax: savefig(fname) +** example +*** savefig('/home/fossee/sine.png') + - file sine.png saved to the folder /home/fossee + - .png - file type + +* More on savefig() +** Recall + - .png - file type +** File types supported +*** .pdf - PDF(Portable Document Format) +*** .ps - PS(Post Script) +*** .eps - Encapsulated Post Script + ~to be used with~ LaTeX ~documents~ +*** .svg - Scalable Vector Graphics + ~vector graphics~ +*** .png - Portable Network Graphics + ~supports transparency~ +* Exercise 1 + Save the sine plot in the format EPS which can be embedded in LaTeX documents. +* Solution 1 + #+begin_src python + savefig('/home/fossee/sine.eps') + #+end_src +* Exercise 2 + Save the sine plot in PDF, PS and SVG formats. + +* Summary + You should now be able to + - Use ~savefig()~ function + - Save plots in different formats + - PDF + - PS + - PNG + - SVG + - EPS + - Locating the files in file system. + +* Thank you! +#+begin_latex + \begin{block}{} + \begin{center} + This spoken tutorial has been produced by the + \textcolor{blue}{FOSSEE} team, which is funded by the + \end{center} + \begin{center} + \textcolor{blue}{National Mission on Education through \\ + Information \& Communication Technology \\ + MHRD, Govt. of India}. + \end{center} + \end{block} +#+end_latex + + diff --git a/saving_plots/slides.tex b/saving_plots/slides.tex new file mode 100644 index 0000000..06d530b --- /dev/null +++ b/saving_plots/slides.tex @@ -0,0 +1,185 @@ +% Created 2010-10-11 Mon 17:08 +\documentclass[presentation]{beamer} +\usepackage[latin1]{inputenc} +\usepackage[T1]{fontenc} +\usepackage{fixltx2e} +\usepackage{graphicx} +\usepackage{longtable} +\usepackage{float} +\usepackage{wrapfig} +\usepackage{soul} +\usepackage{t1enc} +\usepackage{textcomp} +\usepackage{marvosym} +\usepackage{wasysym} +\usepackage{latexsym} +\usepackage{amssymb} +\usepackage{hyperref} +\tolerance=1000 +\usepackage[english]{babel} \usepackage{ae,aecompl} +\usepackage{mathpazo,courier,euler} \usepackage[scaled=.95]{helvet} +\usepackage{listings} +\lstset{language=Python, basicstyle=\ttfamily\bfseries, +commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen}, +showstringspaces=false, keywordstyle=\color{blue}\bfseries} +\providecommand{\alert}[1]{\textbf{#1}} + +\title{Savefig} +\author{FOSSEE} +\date{2010-10-11 Mon} + +\usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent} +\begin{document} + +\maketitle + + + + + + + + + +\begin{frame} +\frametitle{Outline} +\label{sec-1} + +\begin{itemize} +\item Saving plots. +\item Plotting in different formats. +\item Locating the file in the file system. +\end{itemize} +\end{frame} +\begin{frame}[fragile] +\frametitle{Creating a basic plot} +\label{sec-2} + + Plot a sine wave from -3pi to 3pi. +\begin{verbatim} +In []: x = linspace(-3*pi,3*pi,100) + +In []: plot(x, sin(x)) +\end{verbatim} +\end{frame} +\begin{frame}[fragile] +\frametitle{savefig()} +\label{sec-3} +\begin{itemize} + +\item savefig() - to save plots +\label{sec-3_1}% +\begin{verbatim} + syntax: savefig(fname) +\end{verbatim} + + +\item example +\label{sec-3_2}% +\begin{itemize} + +\item savefig('/home/fossee/sine.png') +\label{sec-3_2_1}% +\begin{itemize} +\item file sine.png saved to the folder /home/fossee +\item .png - file type +\end{itemize} + + +\end{itemize} % ends low level +\end{itemize} % ends low level +\end{frame} +\begin{frame} +\frametitle{More on savefig()} +\label{sec-4} +\begin{itemize} + +\item Recall +\label{sec-4_1}% +\begin{itemize} +\item .png - file type +\end{itemize} + + +\item File types supported +\label{sec-4_2}% +\begin{itemize} + +\item .pdf - PDF(Portable Document Format)\\ +\label{sec-4_2_1}% +\item .ps - PS(Post Script)\\ +\label{sec-4_2_2}% +\item .eps - Encapsulated Post Script\\ +\label{sec-4_2_3}% +\texttt{to be used with} \LaTeX{} \texttt{documents} + +\item .svg - Scalable Vector Graphics\\ +\label{sec-4_2_4}% +\texttt{vector graphics} + +\item .png - Portable Network Graphics\\ +\label{sec-4_2_5}% +\texttt{supports transparency} +\end{itemize} % ends low level +\end{itemize} % ends low level +\end{frame} +\begin{frame} +\frametitle{Exercise 1} +\label{sec-5} + + Save the sine plot in the format EPS which can be embedded in \LaTeX{} documents. +\end{frame} +\begin{frame}[fragile] +\frametitle{Solution 1} +\label{sec-6} + +\begin{verbatim} +savefig('/home/fossee/sine.eps') +\end{verbatim} +\end{frame} +\begin{frame} +\frametitle{Exercise 2} +\label{sec-7} + + Save the sine plot in PDF, PS and SVG formats. +\end{frame} +\begin{frame} +\frametitle{Summary} +\label{sec-8} + + You should now be able to +\begin{itemize} +\item Use \texttt{savefig()} function +\item Save plots in different formats + +\begin{itemize} +\item PDF +\item PS +\item PNG +\item SVG +\item EPS +\end{itemize} + +\item Locating the files in file system. +\end{itemize} + + +\end{frame} +\begin{frame} +\frametitle{Thank you!} +\label{sec-9} + + \begin{block}{} + \begin{center} + This spoken tutorial has been produced by the + \textcolor{blue}{FOSSEE} team, which is funded by the + \end{center} + \begin{center} + \textcolor{blue}{National Mission on Education through \\ + Information \& Communication Technology \\ + MHRD, Govt. of India}. + \end{center} + \end{block} +\end{frame} + +\end{document} |