{"id":1232,"date":"2021-11-28T11:37:34","date_gmt":"2021-11-28T19:37:34","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=1232"},"modified":"2021-11-28T11:37:34","modified_gmt":"2021-11-28T19:37:34","slug":"210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2\/","title":{"rendered":"#220 Custom figure subclasses"},"content":{"rendered":"<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport matplotlib.pyplot as plt\r\nfrom matplotlib.figure import Figure\r\nimport numpy as np\r\nclass WatermarkFigure(Figure):\r\n    &quot;&quot;&quot;A figure with a text watermark.&quot;&quot;&quot;\r\n    def __init__(self, *args, watermark=None, **kwargs):\r\n        super().__init__(*args, **kwargs)\r\n        if watermark is not None:\r\n            bbox = dict(boxstyle='square', lw=3, ec='gray',\r\n                        fc=(0.9, 0.9, .9, .5), alpha=0.5)\r\n            self.text(0.5, 0.5, watermark,\r\n                      ha='center', va='center', rotation=30,\r\n                      fontsize=40, color='gray', alpha=0.5, bbox=bbox)\r\n\r\nx = np.linspace(-3, 3, 201)\r\ny = np.tanh(x) + 0.1 * np.cos(5 * x)\r\nplt.figure(FigureClass=WatermarkFigure, watermark='draft')\r\nplt.plot(x, y)\r\nplt.savefig('ex220.png', dpi=72)\r\nplt.show()\r\n<\/pre>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex220.png?resize=460%2C345&#038;ssl=1\" alt=\"\" width=\"460\" height=\"345\" class=\"alignnone size-full wp-image-1233\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex220.png?w=460&amp;ssl=1 460w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex220.png?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex220.png?resize=150%2C113&amp;ssl=1 150w\" sizes=\"(max-width: 460px) 100vw, 460px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>import matplotlib.pyplot as plt from matplotlib.figure import Figure import numpy as np class WatermarkFigure(Figure): &quot;&quot;&quot;A figure with a text watermark.&quot;&quot;&quot; def __init__(self, *args, watermark=None, **kwargs): super().__init__(*args, **kwargs) if watermark is not None: bbox = dict(boxstyle=&#8217;square&#8217;, lw=3, ec=&#8217;gray&#8217;, fc=(0.9, 0.9, .9, .5), alpha=0.5) self.text(0.5, 0.5, watermark, ha=&#8217;center&#8217;, va=&#8217;center&#8217;, rotation=30, fontsize=40, color=&#8217;gray&#8217;, alpha=0.5, bbox=bbox) x = np.linspace(-3, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","_lmt_disableupdate":"yes","_lmt_disable":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1232","post","type-post","status-publish","format-standard","hentry","category-python"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-jS","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1208,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/210-parametric-curve-in-3d-2-2-2\/","url_meta":{"origin":1232,"position":0},"title":"#213 Annotate Text Arrow","author":"gantovnik","date":"2021-11-27","format":false,"excerpt":"[code language=\"python\"] import numpy as np import matplotlib.pyplot as plt # Fixing random state for reproducibility np.random.seed(19680801) fig, ax = plt.subplots(figsize=(5, 5)) ax.set_aspect(1) x1 = -1 + np.random.randn(100) y1 = -1 + np.random.randn(100) x2 = 1. + np.random.randn(100) y2 = 1. + np.random.randn(100) ax.scatter(x1, y1, color=\"r\") ax.scatter(x2, y2, color=\"g\") bbox_props\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex213.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1179,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/204-mandelbrot-fractal-using-python-2\/","url_meta":{"origin":1232,"position":1},"title":"#205 Plots with annotation in matplotlib","author":"gantovnik","date":"2021-11-26","format":false,"excerpt":"[code language=\"python\"] import numpy as np import matplotlib.pyplot as plt plt.fig = plt.figure(1) plt.ax = plt.subplot(111) x = np.linspace(0,2*np.pi,100) # Function that modulates the amplitude of the sin function amod_sin = lambda x: (1.-0.1*np.sin(25*x))*np.sin(x) plt.ax.plot(x,np.sin(x),label = 'sin') plt.ax.plot(x, amod_sin(x), label = 'modsin') annot1=plt.ax.annotate('amplitude modulated\\n curve', (2.1,1.0), (3.2,0.5), arrowprops={'width':2,'color':'k','connectionstyle':'arc3,rad=+0.5','shrink':0.05},verticalalignment='bottom', horizontalalignment='left', fontsize=10,\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex205.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":505,"url":"https:\/\/gantovnik.com\/bio-tips\/2020\/05\/creating-a-better-version-of-python-3-tkinter-hello-world\/","url_meta":{"origin":1232,"position":2},"title":"#75 Creating a better version of Python 3 Tkinter Hello World","author":"gantovnik","date":"2020-05-06","format":false,"excerpt":"Creating a better version of Python 3 Tkinter Hello World [code language=\"python\"] import tkinter as tk from tkinter import ttk class HelloView(tk.Frame): \"\"\"A friendly little module\"\"\" def __init__(self, parent, *args, **kwargs): super().__init__(parent, *args, **kwargs) self.name = tk.StringVar() self.hello_string = tk.StringVar() self.hello_string.set(\"Hello World\") name_label = ttk.Label(self, text=\"Name:\") name_entry = ttk.Entry(self, textvariable=self.name)\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/05\/2020-05-06_000413.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":575,"url":"https:\/\/gantovnik.com\/bio-tips\/2020\/06\/83-application-saving-the-data-to-csv-file\/","url_meta":{"origin":1232,"position":3},"title":"#83 Application, Saving the data to csv file","author":"gantovnik","date":"2020-06-13","format":false,"excerpt":"[code language=\"python\"] from datetime import datetime import os import csv import tkinter as tk from tkinter import ttk class LabelInput(tk.Frame): \"\"\"A widget containing a label and input together.\"\"\" def __init__(self, parent, label='', input_class=ttk.Entry, input_var=None, input_args=None, label_args=None, **kwargs): super().__init__(parent, **kwargs) input_args = input_args or {} label_args = label_args or {} self.variable\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/06\/2020-06-13_025234.jpg?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/06\/2020-06-13_025234.jpg?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/06\/2020-06-13_025234.jpg?resize=525%2C300 1.5x"},"classes":[]},{"id":1215,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/210-parametric-curve-in-3d-2-2-2-2-2\/","url_meta":{"origin":1232,"position":4},"title":"#215 Placing text boxes","author":"gantovnik","date":"2021-11-28","format":false,"excerpt":"[code language=\"python\"] import numpy as np import matplotlib.pyplot as plt np.random.seed(19680801) fig, ax = plt.subplots() x = 30*np.random.randn(10000) mu = x.mean() median = np.median(x) sigma = x.std() textstr = '\\n'.join(( r'$\\mu=%.2f$' % (mu, ), r'$\\mathrm{median}=%.2f$' % (median, ), r'$\\sigma=%.2f$' % (sigma, ))) ax.hist(x, 50) # these are matplotlib.patch.Patch properties props\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex215.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2007,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/01\/401-add-labels-to-line-plot-in-python\/","url_meta":{"origin":1232,"position":5},"title":"#401 Add labels to line plot in python","author":"gantovnik","date":"2024-01-05","format":false,"excerpt":"[code language=\"python\"] import matplotlib.pyplot as plt import numpy as np plt.clf() # using some dummy data for this example n = 25 xs = np.linspace(1, 100, num=n) ys = np.random.normal(loc=3, scale=0.4, size=n) # 'bo-' means blue color, round points, solid lines plt.plot(xs,ys,'bo-') # zip joins x and y coordinates in\u2026","rel":"","context":"In &quot;matplotlib&quot;","block_context":{"text":"matplotlib","link":"https:\/\/gantovnik.com\/bio-tips\/category\/matplotlib\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex401.png?fit=640%2C480&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex401.png?fit=640%2C480&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex401.png?fit=640%2C480&ssl=1&resize=525%2C300 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/comments?post=1232"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1232\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=1232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=1232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=1232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}