New Matplotlib Demo Programs for PySimpleGUIWeb
This commit is contained in:
		
							parent
							
								
									9435bb6393
								
							
						
					
					
						commit
						97061b3614
					
				
					 4 changed files with 78 additions and 14 deletions
				
			
		|  | @ -142,25 +142,35 @@ def create_pyplot_scales(): | ||||||
|                         wspace=0.35) |                         wspace=0.35) | ||||||
|     return plt.gcf() |     return plt.gcf() | ||||||
| 
 | 
 | ||||||
|  | # ----------------------------- The draw figure helpful function ----------------------------- | ||||||
| 
 | 
 | ||||||
| def draw_figure(fig, element): | def draw_figure(fig, element): | ||||||
|  |     """ | ||||||
|  |     Draws the previously created "figure" in the supplied Image Element | ||||||
|  | 
 | ||||||
|  |     :param fig: a Matplotlib figure | ||||||
|  |     :param element: an Image Element | ||||||
|  |     :return: The figure canvas | ||||||
|  |     """ | ||||||
|  | 
 | ||||||
|  |     plt.close('all')        # erases previously drawn plots | ||||||
|     canv = FigureCanvasAgg(fig) |     canv = FigureCanvasAgg(fig) | ||||||
|     buf = io.BytesIO() |     buf = io.BytesIO() | ||||||
|     canv.print_figure(buf, format='png') |     canv.print_figure(buf, format='png') | ||||||
| 
 |  | ||||||
|     if buf is None: |     if buf is None: | ||||||
|         return None |         return None | ||||||
|     buf.seek(0) |     buf.seek(0) | ||||||
|     data = buf.read() |     element.update(data=buf.read()) | ||||||
|     element.update(data=data) |     return canv | ||||||
| 
 | 
 | ||||||
|  | # ----------------------------- The GUI Section ----------------------------- | ||||||
|  | 
 | ||||||
|  | def main(): | ||||||
|     dictionary_of_figures = {'Axis Grid': create_axis_grid, |     dictionary_of_figures = {'Axis Grid': create_axis_grid, | ||||||
|                              'Subplot 3D': create_subplot_3d, |                              'Subplot 3D': create_subplot_3d, | ||||||
|                              'Scales': create_pyplot_scales, |                              'Scales': create_pyplot_scales, | ||||||
|                              'Basic Figure': create_figure} |                              'Basic Figure': create_figure} | ||||||
| 
 | 
 | ||||||
| def main(): |  | ||||||
| 
 |  | ||||||
|     layout = [ |     layout = [ | ||||||
|                 [sg.T('Matplotlib Example', font='Any 20')], |                 [sg.T('Matplotlib Example', font='Any 20')], | ||||||
|                 [sg.Listbox(dictionary_of_figures.keys(), size=(15, 5), key='-LB-'), sg.Image(key='-IMAGE-')], |                 [sg.Listbox(dictionary_of_figures.keys(), size=(15, 5), key='-LB-'), sg.Image(key='-IMAGE-')], | ||||||
|  |  | ||||||
|  | @ -846,16 +846,15 @@ def draw_figure(fig, element): | ||||||
|     :param element: an Image Element |     :param element: an Image Element | ||||||
|     :return: The figure canvas |     :return: The figure canvas | ||||||
|     """ |     """ | ||||||
|     plt.close('all') | 
 | ||||||
|  |     plt.close('all')            # erases previously drawn plots | ||||||
|     canv = FigureCanvasAgg(fig) |     canv = FigureCanvasAgg(fig) | ||||||
|     buf = io.BytesIO() |     buf = io.BytesIO() | ||||||
|     canv.print_figure(buf, format='png') |     canv.print_figure(buf, format='png') | ||||||
| 
 |  | ||||||
|     if buf is None: |     if buf is None: | ||||||
|         return None |         return None | ||||||
|     buf.seek(0) |     buf.seek(0) | ||||||
|     data = buf.read() |     element.update(data=buf.read()) | ||||||
|     element.update(data=data) |  | ||||||
|     return canv |     return canv | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -2,6 +2,7 @@ import PySimpleGUIWeb as sg | ||||||
| import numpy as np | import numpy as np | ||||||
| from matplotlib.backends.backend_tkagg import FigureCanvasAgg | from matplotlib.backends.backend_tkagg import FigureCanvasAgg | ||||||
| import matplotlib.figure | import matplotlib.figure | ||||||
|  | import matplotlib.pyplot as plt | ||||||
| import io | import io | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | @ -15,15 +16,24 @@ def create_figure(): | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def draw_figure(fig, element): | def draw_figure(fig, element): | ||||||
|  |     """ | ||||||
|  |     Draws the previously created "figure" in the supplied Image Element | ||||||
|  | 
 | ||||||
|  |     :param fig: a Matplotlib figure | ||||||
|  |     :param element: an Image Element | ||||||
|  |     :return: The figure canvas | ||||||
|  |     """ | ||||||
|  | 
 | ||||||
|  |     plt.close('all')  # erases previously drawn plots | ||||||
|     canv = FigureCanvasAgg(fig) |     canv = FigureCanvasAgg(fig) | ||||||
|     buf = io.BytesIO() |     buf = io.BytesIO() | ||||||
|     canv.print_figure(buf, format='png') |     canv.print_figure(buf, format='png') | ||||||
| 
 |  | ||||||
|     if buf is None: |     if buf is None: | ||||||
|         return None |         return None | ||||||
|     buf.seek(0) |     buf.seek(0) | ||||||
|     data = buf.read() |     element.update(data=buf.read()) | ||||||
|     element.update(data=data) |     return canv | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| def main(): | def main(): | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
							
								
								
									
										45
									
								
								PySimpleGUIWeb/Demo Programs/Web_Matplotlib_Simple_PyPlot.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								PySimpleGUIWeb/Demo Programs/Web_Matplotlib_Simple_PyPlot.py
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,45 @@ | ||||||
|  | import PySimpleGUIWeb as sg | ||||||
|  | import numpy as np | ||||||
|  | from matplotlib.backends.backend_tkagg import FigureCanvasAgg | ||||||
|  | import matplotlib.pyplot as plt | ||||||
|  | import io | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def draw_figure(fig, element): | ||||||
|  |     """ | ||||||
|  |     Draws the previously created "figure" in the supplied Image Element | ||||||
|  | 
 | ||||||
|  |     :param fig: a Matplotlib figure | ||||||
|  |     :param element: an Image Element | ||||||
|  |     :return: The figure canvas | ||||||
|  |     """ | ||||||
|  | 
 | ||||||
|  |     plt.close('all')  # erases previously drawn plots | ||||||
|  |     canv = FigureCanvasAgg(fig) | ||||||
|  |     buf = io.BytesIO() | ||||||
|  |     canv.print_figure(buf, format='png') | ||||||
|  |     if buf is None: | ||||||
|  |         return None | ||||||
|  |     buf.seek(0) | ||||||
|  |     element.update(data=buf.read()) | ||||||
|  |     return canv | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def main(): | ||||||
|  |     layout = [[sg.Text('Matplotlib Simple Plot', font='Any 20')], | ||||||
|  |               [sg.Image(key='-IMAGE-')], | ||||||
|  |               [sg.Button('Exit')]] | ||||||
|  | 
 | ||||||
|  |     window = sg.Window('Matplotlib Example', layout, finalize=True) | ||||||
|  | 
 | ||||||
|  |     fig = plt.figure() | ||||||
|  |     x = np.arange(0, 5, 0.1) | ||||||
|  |     y = np.sin(x) | ||||||
|  |     plt.plot(x, y) | ||||||
|  |     draw_figure(fig, window['-IMAGE-']) | ||||||
|  | 
 | ||||||
|  |     window.read(close=True) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | if __name__ == "__main__": | ||||||
|  |     main() | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue