Register here: http://gg.gg/x0v3a
ENV 859 - Advanced GIS | Fall 2020 | Instructor: John Fay | Zoom Links: Mon. | Wed.
*Spyder Plot Not Show
*Spyder 4 Plots Pane Options Menu
*@Vai The code from @volcano63 is approximately equivalent to how this is done in R/R-Studio (’rm(list=ls)’). The link below has a similar code example. It also states that ’%reset’ is the right answer, but that prompts for confirmation, which seems to me one step worse than having to click on the ’Reset the namespace’ button on the ’Variable explorer’ tab.
*You can restore a pane using the View-Panes menu. There are a lot of options for configuring Spyder your way, but the first thing to remember is that you can resize these three frames by clicking and dragging. Feel free to experiment with setting the frames in Spyder up the way you like. You might close the ’help’ window until you need it again.Introduction
Python scripts are simply text files with a .py extension; you can open them and read them - even write them - in Notepad, WordPad, Notepad++, etc. However, working with Python scripts is more easily done in an integrated development environment (IDE) as these IDEs offer tools to auto format scripts as you write them, to debug them, and even to execute Python statements or chunks of scripts interactively.Learning Objectives
In this article, we would learn how to save a plot as an image in python. There are times when one needs a matplotlib figure as an image file so that we can use it for other purposes. We can save a plot as an image easily by following the steps mentioned in this article. So let us begin. How to save a matplotlib plot as an image in Python. I am new to Spyder and Python graphing but resolved this problem using Spyder menu options Tools - Preferences - Graphics Tab - and changed the setting to Automatic (from default Inline). If the compound option is set to BOTTOM, LEFT, RIGHT, or TOP, the image is drawn correspondingly to the bottom, left, right or top of the text. Colorized Labels in various fonts Some Tk widgets, like the label, text, and canvas widget, allow you to specify the fonts used to display text.SectionOn completion of this lesson, you should be able to:1. What is a Script? What is an IDE?Discuss the advantages of using an IDE over a simple text editor for writing Python scripts2. Installing SpyderInstall the Spyder IDE on a machine with ArcGIS Pro installed3. Starting SpyderOpen Spyder from the Python command prompt, in default as well as specific locations.
Describe what a Spyder project is and what a working directory is
Create a shortcut to open Spyder in a specific folder or to use a specific project4. Navigating SpyderNavigate and use the various Spyder panes as well as customize its layout5. Using Spyder’s ConsoleRun Python commands from Spyder’s console6. Using Spyder’s Editor/DebuggingCreate, edit, run, and debug Python scripts in Spyder’s editor1. What is a Script? And what is an IDE?
Up to now, we wrote Python code in a Jupyter Notebook. Jupyter Notebooks have some clear benefits and are an indispensable tool for working with Python, but some uses of Python require a barebones script. We could just write these script files in a basic text editor and call them from a Python console, as Python scripts really are just that - basic text files. However, many tools exist to help us write scripts more efficiently. To examine those tools, we’ll first run through a few demos…» Task 1.1: Write some Python code in a simple text file
*
Create a new text file, add the following it, and save it.
→ That’s Python alright, but right now the computer doesn’t recognize it as such» Task 1.2: Convert your text file to a Python script & open in NotePad++
*
Change the file’s extension from .txt to .py. (Note that on Windows machines, you’ll need to ensure file extensions are shown.)
*
Open the .py file in NotePad++
→ Note that NotePad++ recognizes the file as a script and adds formatting: some text appears in color and you can fold lines…» Task 1.3: Open the Python script in IDLE
*
Right-click the Python script file file and open it in IDLE (ArcGIS Pro)
→ This application also shows Python as formatted text
*
Select Run Module (F5) from the Run menu.
→ This application can also execute the commands typed in our Python file!
*
At the >>> prompt in the Python 3.6.9 Shell window that appears, type: print(colors[0])
→ This application also allows us to run Python commands typed interactively!
This last application, IDLE, is an integrated development environment or IDE (and there’s a funny reason behind its name). These apps are designed to help us write, run, and debug Python scripts. IDLE is installed along side ArcGIS Pro. However, I have yet to meet a person who actually writes scripts using IDLE. There are so many better IDEs out there. The one we’ll explore next, Spyder, is one of the more popular ones – especially if you are familiar with working with R-Studio…2. Installing the Spyder IDE
Before we can use the Spyder IDE, we have to install it. (The copy of Python installed with ArcGIS Pro includes Jupyter Notebook, but not Spyder.) To install it, we’ll be getting a preview of Python environments, which we’ll discuss in more detail later. For now, just follow these directions to install Spyder on your machine…» Task 2.1: Install Spyder
*
Open the Python Command Prompt, found in the Windows Start Menu, within the ArcGIS sub-menu.
*
Type and run the following command to clone the default ArcGIS Pro Python environment.
You may get some “DEBUG menuinst_win32” warnings which you can ignore. Eventually, the command prompt should appear. However, sometimes hitting enter is needed to nudge it along.
*
Next, type the following command to activate this environment (this is specific to ArcGIS Pro installs of Python):
*
Now, install Spyder with the following command:
Again, you may get some warnings, but you should be able to ignore these. The process may appear hung for a while, but give it time (~5 min. if necessary - perhaps occasionally hitting the enter key to prod it along…
Spyder is now installed on the machine you are currently logged into, within your personal directory. It will stay there, only accessible to you, until someone removes your account profile. Of course, if you move to a machine you’ve not logged into before (or someone has wiped your profile), you’ll need to repeat these steps to re-install Spyder.3. Starting Spyder
Starting the Spyder IDE, like starting Jupyter Notebooks, is not as simple as most applications on your machine. Here we’ll examine a few more steps on getting the application started via command line, but then also by a shortcut we can create.» Task 3.1 - Starting Spyder via Python Command Prompt:
After installing Spyder, you might notice see a Spyder start button listed in your Windows Start menu’s Anaconda submenu. However, as in the case with Jupyter Notebooks, you’re better off starting Spyder from the command line so you can control in which folder it starts. Here are the steps:
*
Open a Python Command Prompt (or use the one that’s open).
*
Ensure that the active Python environment is env859-py3: it will be shown in the command prompt, preceding the path.
*If it is not the active environment, set it to be the active environment using the proswap env859-py3 command.
*
Next, you can start Spyder just by typing the command:
If all goes well, Spyder will open up with your Windows user folder set to the default workspace - this will appear in the upper right corner of the Spyder window. NOTE: Spyder working directories
Your Python scripts may read and write files, so it’s good to keep track of where you are in your file system. The location where Spyder will look is your Spyder session’s working directory. By default this is set to your Windows user directory, but you may want to avoid that and instead set Spyder to use your mapped class drive.
You can set your working directory in Spyder itself by clicking the icon in the upper right and navigating to a different location. Alternatively, you can set the working directory when you open Spyder using the -w option:
*
To start Spyder with a specific folder set as the default workspace (here we’ll use “V:SpyderFolder”, which you’d need to create prior to running this command:» Task 3.2 - Creating a shortcut (a Windows batch file ) to start Spyder:
Now, we’ll ease the process by creating a windows shortcut that will open Spyder, setting the default workspace to the folder in which the shortcut is placed. This is a bit trickier than the Jupyter shortcut we created because it has to activate the Python workspace we created when we installed Spyder, i.e., the env859-py3 workspace.
*
Create a new text file in your SpyderFolder folder, and rename this file to “OpenSpyder.bat”.
*
Open this OpenSpyder.bat file in a text editor (e.g. Notepad++).
*
Paste the text below into this file:
*
Close the text editor.
*
Run the batch file by double-clicking it.
A black command window should appear and eventually the Spyder app will also appear. If you’re curious, this batch file ensure the Python environment in which we installed Spyder is the active Python environment. Then it runs the Spyder.exe command from the correct environment which opens Spyder. And finally, it deactivates the Python environment once you’ve ended your Spyder session.» Task 3.3 - Creating a new Spyder Project
A Spyder project one-ups setting the working directory as it also includes specific settings like how your windows are arranged and other defaults. (Read more here). Again, you can create/open Spyder projects from within the application, or you can use a command line option when opening Spyder.
*
Open Spyder in the V:SpyderFolder folder, either via the command line or batch file method used above.
*
From the Projects menu, select New Project.
*
In the Create new project, create a new project in your V: drive named “MySpyderProject”
*
Note the default working directory is set to the folder created when we created the project.
*
Open the V:MySpyderProject folder in Windows. There you’ll see a folder named .spyproject. This holds the settings pertaining to this project.» Task 3.4 - Opening an existing Spyder project
Jumping back into a Spyder coding session is best done by re-opening the project where you left off. Here are some ways to do that.
*Option 1: If you open Spyder, you can open an existing project via the Projects > Open Project option. (Note the helpful “Recent Projects” option…)
*Option 2: You can modify your command to open in a specific project by changing the -w to -p.
*Option 3: You can modify your batch file by replacing the -w to -p.
If ever you want to wipe all your Spyder global settings, type: spyder --reset from your Python command prompt. Alternatively, delete the .spyder-py3 folder from your user directory.4. Navigating SpyderSpyder’s default layout
If all goes well, Spyder will have opened and you will see it’s default layout which includes multiple separate panes, each of which may have additional tabs. You can modify the arrangement of these windows, but we’ll stick with the defaults for now. These windows include:» Task 4.1: Navigate Spyder
*On the far left is the Project Explorer displays the files in our current workspace.
*Next from the left is the Editor Pane where you write the sequence of commands that comprise your script.
*On the right top, you toggle between your Variable Explorer , a File Explorer , Plots, and a Help Window.
*The Variable Explorer reveals all the variables we’ve assigned in our session and their value.
*The File Explorer displays the files in our workspace.
*The Plots window can display graphics output the console window is unable to show.
*And the Help displays help on a command.
*On the right bottom, you use the tabs to toggle between your IPython Console and the History Log.
*The IPython Console is used to type Python commands outside of our script. It works just like a Python code cell in our familiar Jupyter notebook.
*The History Log stores the last 100 commands you’ve typed into the Console.
Within the View menu you’ll find options to toggle other panes and views. Spyder is quite a customizable program.» Task 4.2: Change the layout to mimic R-StudioSpyder Plot Not Show
Many of you have used RStudio and are familiar with its layout. Or perhaps you will someday want to learn R, in which case you’ll likely use RStudio in that effort. Either way, it may be useful to switch Spyder’s layout to one that mimics R-Studio so that work translates well between the two platforms. To do this:
*Select RStudio Layout from the View>Windows Layouts menu.
It’s not exactly like RStudio, but it may help. You are, of course, welcome to chose any layout you wish, but you should understand what each pane does. We’ll run through some exercises to dig deeper into what each does.5. Using Spyder’s Console» Task 5.1: Entering code in Spyder’s console
Entering Data
*Type “x=5” in the Console – this is the command to create a variable named x and give it the value 5.
*If you raise the “Variable explorer” tab you will see that x has been added to the list of variables in Python’s memory.
*You can also type “print (x)” or even just “x” in the Console to see the value of x.
*Now type “y=4” and then “x+y”. Notice that this last command does not create a variable, although it does produce an output from the calculation.
Arrow Keys
If you use the arrow keys in the Console, you can bring back a previous command so that you can edit and re-execute it.
*
Go back to the command “x+y” and change it to “junk=x+y”. You’ve now created the variable junk.
→ What can you type to see its value in the Console?6. Using Spyder’s Editor» Task 6.1: Creating and running a script
*
Create a new script file: File>New File. This will add a new file untitled1.py next to the default temp.py.
*Note where each of these is configured to be saved…
*Close the temp.py file.
*
Add these commands to your script file:
*
Run the entire script by clicking the green arrow - or - hitting F5 - or - Selecting Run> Run.
→ This prompts you to save your script. Spyder will always save your script before running.
*Save your script into your project folder as script1.py and run the script.
*Check that the variables are added to your session in the Variable Explorer.
*Where is the result (“Dog”) displayed?» Task 6.2: Debugging a script
*
Create a new script file in your Spyder session.
*
Add the following code to this script and then save it as script2.py:
*
Note that Spyder reveals possible errors in your code. You should see a red next to line 1. Hover your mouse over it and it will reveal what the complaint is, at least in general terms.
→What is the error? Can you fix it?
*
Another error appears on line 5. Note that the word “class” is blue. *This indicates that the word is a reserved word that cannot be used as a variable name.*
→ Rename all instances of “class” to “class_name”.
*
Spyder can’t find any more errors! Try running the script…
You’ll get another error showing up in your console. In this case, the error message states where the error occurs. Some error messages are not as kind and don’t reveal exactly where the error occurs. For this we need debugging tools.
*Run the script one line at a time to see exactly where the error occurs. To do this, click the first line and hit F9 (or the ■► button).
→ At what line does the error occur?
*
Fix the error by coercing the age variable to be a string, not an integer.
*
Run the model again.
*Close Spyder. No need to save your script as it saves each time you run your script!Recap
Integrated Development Environments, or IDEs, help us write scripts. Fundamentally, they are text editors linked to a Python kernel that can execute our Python scripts, but they also provide code formatting, code completion, a Python shell, debugging tools, and many other features that enable us to interactively write and debug our code.
Contact: compwiki@physics.utoronto.ca

Last updated around: 2018-07-24
1. Introduction
This tutorial will take you through the basics of using Python 3 with the integrated development environment Spyder.
Here is an overview of the software we are documenting on this website:
*Python is a general-purpose programming language that we will use to teach you the use of computers in physics.
*Python has two major versions, Python 2 and Python 3. We use Python 3 but the legacy part of this website documents Python 2.
*Python software is organized in distributions. The distribution we are using is Anaconda Python 3.
*We document how to run Python in an integrated development environment (IDE), which is a software package that lets you test Python commands and edit and run your Python programs. The IDE we are using is Spyder.
*The switch to Anaconda Python 3 and Spyder was done in September 2017. Please read the background page for more information.
We are assuming that Anaconda Python 3 has already been installed on your computer. If this is not the case, go to the ’Getting Started’ with Anaconda Python 3 page and come back to this tutorial once you’ve installed the packages.

We use a few formatting conventions to guide you.
*Python commands and output statements are often set in bold, as in print(’Hello World!’).
*Blocks of code are shaded in grey boxes like this:
*Code written in these boxes includes colored highlighting for different parts of the Python language (commands, variables, comments, etc.). If you don’t understand the color highlighting, don’t worry about it for now.
*Activities and exercises for you to try are labelled as Activity 1, Activity 2, etc
*Because different IDEs have different prompts at the console, we sometimes omit prompts like In [1]: or >>> for simplicity.2. Starting Python in the Spyder IDE
During a typical Python session, you will try Python commands interactively, and edit and run Python programs. In these activities, we will start a Python session and run a couple of simple scripts. These scripts are also used in the installation documentation.

Caution about opening .py files: Files that have the extension .py are known as Python scripts. On your computer, these files will appear to be ’clickable’, i.e. will appear to be files that you can open by clicking them with the mouse. We DO NOT recommend that you open these files by clicking on them. Why? Because quite often the result can be unpredictable. Instead, use the method described below: start Spyder and open Python scripts inside a Spyder session.

Activity 1: Now do the following:

To start Spyder:
*On Windows, a link to Spyder can be found in Start Menu -> Programs -> Anaconda3 (64-bit)
*You can also search for the Spyder app using the Search window on the Start Menu
*The simplest way to find it is to search for ’Spyder’ and click on the icon for the Spyder application.
*

https://diarynote-jp.indered.space

コメント

最新の日記 一覧

<<  2025年6月  >>
1234567
891011121314
15161718192021
22232425262728
293012345

お気に入り日記の更新

テーマ別日記一覧

まだテーマがありません

この日記について

日記内を検索