{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "rm: ./GUI_command_line.py: No such file or directory\r\n" ] } ], "source": [ "!rm ./GUI_command_line.py" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing ./GUI_command_line.py\n" ] } ], "source": [ "%%writefile ./GUI_command_line.py\n", "#!/usr/bin/env python\n", "#GUI that retrieves the desired input file\n", "def get_file():\n", " \"\"\"\n", " makes a GUI that retrieves the desired file name for plotting\n", " Inputs: \n", " None\n", " Returns:\n", " path_to_file: str\n", " path to the desired file\n", " \"\"\"\n", " import PySimpleGUI as sg\n", " layout = [[sg.InputText('Filename'),sg.FileBrowse()],[sg.OK()] ] \n", " window = sg.Window('Simple data entry window').Layout(layout).Finalize()\n", " button, values = window.Read()\n", " window.Close()\n", " path_to_file=values[0]\n", " return path_to_file\n", "\n", "# make a matplotlib figure\n", "def make_topo_map(path_to_file):\n", " \"\"\" Open path_to_file in a pandas DataFrame, and make a topographic map.\n", " Inputs: \n", " path_to_file: str\n", " the path to the file with longitude, latitude, elevation\n", " Outputs:\n", " fig: matplotlib figure object\n", " \"\"\"\n", " import pandas as pd\n", " import cartopy.crs as ccrs\n", " import matplotlib.pyplot as plt\n", " import numpy as np\n", " data=pd.read_csv(path_to_file,header=None,delim_whitespace=True)\n", " data.columns=['lon','lat','elev']\n", " x=data.lon.unique()\n", " y=data.lat.unique()\n", " z=data.elev.values\n", " Z=np.reshape(z,newshape=(len(y),len(x)))\n", "\n", " proj = ccrs.LambertConformal(central_longitude=x.mean(), central_latitude=y.mean())\n", " fig = plt.figure(figsize=(6,6), frameon=True) # you need this frameon to be true\n", " ax = plt.axes(projection=proj)\n", " plt.pcolormesh(x,y,Z,cmap='RdBu_r',shading='gouraud',transform=ccrs.PlateCarree())\n", " return fig\n", "\n", "## put a figure onto a canvas\n", "def draw_figure(canvas, figure, loc=(0, 0)):\n", " \"\"\" Draw a matplotlib figure onto a Tk canvas\n", "\n", " loc: location of top-left corner of figure on canvas in pixels.\n", "\n", " Inspired by matplotlib source: lib/matplotlib/backends/backend_tkagg.py\n", " \"\"\"\n", " from matplotlib.backends.backend_tkagg import FigureCanvasAgg\n", " import matplotlib.backends.tkagg as tkagg\n", " import tkinter as Tk\n", "\n", " # make a Tkagg canvas\n", " figure_canvas_agg = FigureCanvasAgg(figure)\n", " figure_canvas_agg.draw()\n", " # get some dimensions for our figure's bounding box (bbox)\n", " figure_x, figure_y, figure_w, figure_h = figure.bbox.bounds\n", " figure_w, figure_h = int(figure_w), int(figure_h)\n", " # make a \"photo\" object with correct dimensions out of the canvas\n", " photo = Tk.PhotoImage(master=canvas, width=figure_w, height=figure_h)\n", " # put the figure on the photo\n", " canvas.create_image(loc[0] + figure_w/2, loc[1] + figure_h/2, image=photo)\n", " tkagg.blit(photo, figure_canvas_agg.get_renderer()._renderer, colormode=2)\n", " # return the photo object\n", " return photo\n", "\n", "\n", "def window_maker(fig,title=\"Embedded Matplotlib Figure\"):\n", " \"\"\" Makes a GUI window to put a matplotlib figure in\"\"\"\n", " import PySimpleGUI as sg\n", "\n", " # get the dimensions of the figure\n", " figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds\n", " # define the window layout with a title, a canvas and an OK button\n", " layout = [[sg.Text(title, font='Any 18')],\n", " [sg.Canvas(size=(figure_w, figure_h), key='canvas')],\n", " [sg.OK(pad=((figure_w / 2, 0), 3), size=(4, 2))]]\n", "\n", " # create the window and show it without the plot\n", " window = sg.Window('Figure in a Window', force_toplevel=True).Layout(layout)\n", " window.Finalize() # needed to access the canvas element prior to reading the window\n", " \n", " # get the canvas element from the window\n", " canvas=window.FindElement('canvas').TKCanvas\n", " # show it all again and retrieve buttons\n", " # add the plot to the canvas\n", " fig_photo = draw_figure(canvas, fig)\n", " event, values = window.Read()\n", " window.Close()\n", "\n", "\n", "def main():\n", " # get the file name\n", " path_to_file=get_file()\n", " # read in the data and make the mesh\n", " # make the map\n", " fig=make_topo_map(path_to_file)\n", " # make the window\n", " window_maker(fig,title=path_to_file)\n", "\n", "\n", " \n", "# make it so\n", "\n", "main()\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "! chmod a+x GUI_command_line.py" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.6" } }, "nbformat": 4, "nbformat_minor": 2 }