<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Python on Shaka</title><link>https://shakaxi.github.io/tags/python/</link><description>Recent content in Python on Shaka</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Sun, 03 Sep 2023 00:00:00 +0000</lastBuildDate><atom:link href="https://shakaxi.github.io/tags/python/index.xml" rel="self" type="application/rss+xml"/><item><title>Matplotlib</title><link>https://shakaxi.github.io/posts/matplotlib/</link><pubDate>Sun, 03 Sep 2023 00:00:00 +0000</pubDate><guid>https://shakaxi.github.io/posts/matplotlib/</guid><description>&lt;div id="outline-container-headline-1" class="outline-2"&gt;&#10;&lt;h2 id="headline-1"&gt;&#10;Background&#10;&lt;/h2&gt;&#10;&lt;div id="outline-text-headline-1" class="outline-text-2"&gt;&#10;&lt;p&gt;&lt;a href="https://matplotlib.org"&gt;Matplotlib&lt;/a&gt; is a popular python package for figure plot and data visualization.&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-2" class="outline-2"&gt;&#10;&lt;h2 id="headline-2"&gt;&#10;Installation&#10;&lt;/h2&gt;&#10;&lt;div id="outline-text-headline-2" class="outline-text-2"&gt;&#10;&lt;div class="src src-python"&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; pip install matplotlib&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-3" class="outline-2"&gt;&#10;&lt;h2 id="headline-3"&gt;&#10;Submodules&#10;&lt;/h2&gt;&#10;&lt;div id="outline-text-headline-3" class="outline-text-2"&gt;&#10;&lt;p&gt;Matplotlib comes with a series of submodules, e.g.,&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;matplotlib.pyplot&lt;/code&gt; is the counterpart of Matlab in matplotlib, which is essentially a collection of functions similar to Matlab.&lt;/li&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;matplotlib.pylab&lt;/code&gt; is a convenience module which imports &lt;code class="verbatim"&gt;matplotlib.pyplot&lt;/code&gt; (for plotting) and &lt;code class="verbatim"&gt;numpy&lt;/code&gt; (for array related operations) in a single name space.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-4" class="outline-2"&gt;&#10;&lt;h2 id="headline-4"&gt;&#10;General Concepts&#10;&lt;/h2&gt;&#10;&lt;div id="outline-text-headline-4" class="outline-text-2"&gt;&#10;&lt;p&gt;In Matplotlib, there are some important concepts which can be illustrated below.&lt;/p&gt;&#10;&lt;figure&gt;&#10;&lt;img src="./matplotlib_concept.png" alt="./matplotlib_concept.png" title="./matplotlib_concept.png" width="300px"/&gt;&#10;&lt;figcaption&gt;&#10;Concepts in a figure in Matplotlib (from &lt;a href="https://matplotlib.org/1.5.1/faq/usage_faq.html)"&gt;https://matplotlib.org/1.5.1/faq/usage_faq.html)&lt;/a&gt;&#10;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&lt;p&gt;&#10;A typical matplotlib &lt;code class="verbatim"&gt;Figure&lt;/code&gt; is comprised of a variety of components, as show below.&lt;/p&gt;&#10;&lt;figure&gt;&#10;&lt;img src="./matplotlib_component.png" alt="./matplotlib_component.png" title="./matplotlib_component.png" width="300px"/&gt;&#10;&lt;figcaption&gt;&#10;Components of a figure in Matplotlib (from &lt;a href="https://matplotlib.org/stable/tutorials/introductory/usage.html)"&gt;https://matplotlib.org/stable/tutorials/introductory/usage.html)&lt;/a&gt;&#10;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&lt;div id="outline-container-headline-5" class="outline-3"&gt;&#10;&lt;h3 id="headline-5"&gt;&#10;&lt;code class="verbatim"&gt;Figure&lt;/code&gt;&#10;&lt;/h3&gt;&#10;&lt;div id="outline-text-headline-5" class="outline-text-3"&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;A &lt;code class="verbatim"&gt;Figure&lt;/code&gt; object is essentially a canvas on which plot operations are performed. Clearly, it consists of one or more &lt;code class="verbatim"&gt;Axes&lt;/code&gt; objects.&lt;/p&gt;&#10;&lt;div class="src src-python"&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;import&lt;/span&gt; &lt;span style="color:#24292e"&gt;matplotlib.pyplot&lt;/span&gt; &lt;span style="color:#cf222e"&gt;as&lt;/span&gt; &lt;span style="color:#24292e"&gt;plt&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#57606a"&gt;# Create an empty Figure object without any Axes.&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; fig &lt;span style="color:#0550ae"&gt;=&lt;/span&gt; plt&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;figure&lt;span style="color:#1f2328"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; fig &lt;span style="color:#0550ae"&gt;=&lt;/span&gt; plt&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;figure&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;figsize&lt;span style="color:#0550ae"&gt;=&lt;/span&gt;&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;WIDTH&lt;span style="color:#1f2328"&gt;,&lt;/span&gt; HEIGHT&lt;span style="color:#1f2328"&gt;),&lt;/span&gt; dpi&lt;span style="color:#0550ae"&gt;=&lt;/span&gt;DPI&lt;span style="color:#1f2328"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#57606a"&gt;# Create a Figure object including one Axes.&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; fig&lt;span style="color:#1f2328"&gt;,&lt;/span&gt; ax &lt;span style="color:#0550ae"&gt;=&lt;/span&gt; plt&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;subplots&lt;span style="color:#1f2328"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#57606a"&gt;# Create a Figure object including 2 x 3 Axes.&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; fig&lt;span style="color:#1f2328"&gt;,&lt;/span&gt; ax &lt;span style="color:#0550ae"&gt;=&lt;/span&gt; plt&lt;span style="color:#0550ae"&gt;.&lt;/span&gt;subplots&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;&lt;span style="color:#0550ae"&gt;2&lt;/span&gt;&lt;span style="color:#1f2328"&gt;,&lt;/span&gt; &lt;span style="color:#0550ae"&gt;3&lt;/span&gt;&lt;span style="color:#1f2328"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;matplotlib.pyplot.gcf()&lt;/code&gt; can get the current &lt;code class="verbatim"&gt;Figure&lt;/code&gt; object.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-6" class="outline-3"&gt;&#10;&lt;h3 id="headline-6"&gt;&#10;&lt;code class="verbatim"&gt;Axes&lt;/code&gt;&#10;&lt;/h3&gt;&#10;&lt;div id="outline-text-headline-6" class="outline-text-3"&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Create &lt;code class="verbatim"&gt;Axes&lt;/code&gt; objects.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;matplotlib.pyplot.subplots&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;figure.add_axes&lt;/code&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;A &lt;code class="verbatim"&gt;Axes&lt;/code&gt; object consists of a multiplicity &lt;code class="verbatim"&gt;Axis&lt;/code&gt; objects, e.g.,&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;2 &lt;code class="verbatim"&gt;Axis&lt;/code&gt; objects in a 2-dimension plot, e.g.., X-axis and Y-axis.&lt;/li&gt;&#10;&lt;li&gt;3 &lt;code class="verbatim"&gt;Axis&lt;/code&gt; objects in a 3-dimension plot, e.g.., X-axis, Y-axis, and Z-axis.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;A &lt;code class="verbatim"&gt;Figure&lt;/code&gt; object&amp;#39;s member function &lt;code class="verbatim"&gt;gca()&lt;/code&gt; can get the current &lt;code class="verbatim"&gt;Axes&lt;/code&gt; object.&lt;/li&gt;&#10;&lt;li&gt;A &lt;code class="verbatim"&gt;Figure&lt;/code&gt; object&amp;#39;s member function &lt;code class="verbatim"&gt;subplots&lt;/code&gt; can create one or more &lt;code class="verbatim"&gt;Axes&lt;/code&gt; objects. &lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Member functions&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;plot(X, Y)&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;set_xlabel(&amp;#34;XLABEL&amp;#34;)&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;set_ylabel(&amp;#34;YLABEL&amp;#34;)&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;set_title(&amp;#34;TITLE&amp;#34;)&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;set_xticks(&amp;#34;XTICKS&amp;#34;)&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;set_yticks(&amp;#34;YTICKS&amp;#34;)&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;set_xlim([XMIN, XMAX])&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;set_ylim([YMIN, YMAX])&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;legend()&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;code class="verbatim"&gt;set_prop_cycle()&lt;/code&gt; : Set the random seed of colors, e.g., &lt;code class="verbatim"&gt;None&lt;/code&gt; for reset to the default.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="outline-container-headline-7" class="outline-3"&gt;&#10;&lt;h3 id="headline-7"&gt;&#10;&lt;code class="verbatim"&gt;Axis&lt;/code&gt;&#10;&lt;/h3&gt;&#10;&lt;div id="outline-text-headline-7" class="outline-text-3"&gt;&#10;&lt;p&gt;Just as its name indicates, a &lt;code class="verbatim"&gt;Axis&lt;/code&gt; object defines the properties and attributes of a axis, e.g., limits, ticks, and ticklabels.&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;</description></item></channel></rss>