<?xml version="1.0" encoding="UTF-8"?>

<rss version="2.0"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
     xmlns:atom="http://www.w3.org/2005/Atom"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:wfw="http://wellformedweb.org/CommentAPI/"
     >
  <channel>
    <atom:link href="http://kitchingroup.cheme.cmu.edu/blog/feed/index.xml" rel="self" type="application/rss+xml" />
    <title>The Kitchin Research Group</title>
    <link>https://kitchingroup.cheme.cmu.edu/blog</link>
    <description>Chemical Engineering at Carnegie Mellon University</description>
    <pubDate>Sat, 01 Nov 2025 13:47:46 GMT</pubDate>
    <generator>Blogofile</generator>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    
    <item>
      <title>Annotating matplotlib figures</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2014/11/01/Annotating-matplotlib-figures</link>
      <pubDate>Sat, 01 Nov 2014 10:35:15 EDT</pubDate>
      <category><![CDATA[python matplotlib]]></category>
      <guid isPermaLink="false">AcY0at1sX0APriSojYzVLBLbBHY=</guid>
      <description>Annotating matplotlib figures</description>
      <content:encoded><![CDATA[




&lt;p&gt;
There is a nice picture of an ethanolamine molecule &lt;a href="http://en.wikipedia.org/wiki/Ethanolamine"&gt;here&lt;/a&gt; . The first thing we consider is embedding this figure in a matplotlib figure. It is a little tricky because we have to create a special axes to put the image in. The axes are created in a fractional coordinate systems that is defined by [left, bottom, width, height]. Placing the figure where you want it is an iterative process that involves changing those values to get the image where you want.
&lt;/p&gt;

&lt;p&gt;
So, note that (0, 0) is the bottome left corner of the figure, and (1, 1) is the upper right corner. So, to make an axes for the main figure that takes up 75% of the width and 80% of the height, and starts 20% from the left, 15% from the bottom, we use [0.2, 0.15, 0.75, 0.8]. That covers most of the space, and leaves room for labels.
&lt;/p&gt;

&lt;p&gt;
The axes for the image is about the same, but it is a little trickier to figure out the width and height. In this example these arguments appear to just rescale the image.
&lt;/p&gt;

&lt;p&gt;
Here is some code that puts the image near the upper left-corner of the plot.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; matplotlib.pyplot &lt;span style="color: #8b0000;"&gt;as&lt;/span&gt; plt
&lt;span style="color: #8b0000;"&gt;from&lt;/span&gt; scipy.misc &lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; imread
&lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; numpy &lt;span style="color: #8b0000;"&gt;as&lt;/span&gt; np

&lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;im&lt;/span&gt; = imread(&lt;span style="color: #228b22;"&gt;'images/Ethanolamine-2D-skeletal-B.png'&lt;/span&gt;)

&lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;fig&lt;/span&gt; = plt.figure(&lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;figsize&lt;/span&gt;=(&lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;3&lt;/span&gt;, &lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;4&lt;/span&gt;))
#                    &lt;span style="color: #ff0000; font-weight: bold;"&gt;left bottom width height&lt;/span&gt;
&lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;f_ax&lt;/span&gt; = fig.add_axes([&lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;0&lt;/span&gt;.&lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;2&lt;/span&gt;, &lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;0&lt;/span&gt;.&lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;15&lt;/span&gt;, &lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;0&lt;/span&gt;.&lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;75&lt;/span&gt;, &lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;0&lt;/span&gt;.&lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;8&lt;/span&gt;], &lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;zorder&lt;/span&gt;=&lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;1&lt;/span&gt;)

# &lt;span style="color: #ff0000; font-weight: bold;"&gt;plot some function&lt;/span&gt;
f_ax.plot(np.arange(&lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;10&lt;/span&gt;), &lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;4&lt;/span&gt; * np.arange(&lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;10&lt;/span&gt;))
plt.xlabel(&lt;span style="color: #228b22;"&gt;'some xlabel'&lt;/span&gt;)
plt.ylabel(&lt;span style="color: #228b22;"&gt;'Y'&lt;/span&gt;)

# &lt;span style="color: #ff0000; font-weight: bold;"&gt;axes for the image&lt;/span&gt;
&lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;i_ax&lt;/span&gt; = fig.add_axes([&lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;0&lt;/span&gt;.&lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;22&lt;/span&gt;, &lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;0&lt;/span&gt;.&lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;8&lt;/span&gt;, &lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;0&lt;/span&gt;.&lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;3&lt;/span&gt;, &lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;0&lt;/span&gt;.&lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;1&lt;/span&gt;],
                    &lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;frameon&lt;/span&gt;=&lt;span style="color: #8b0000;"&gt;False&lt;/span&gt;, &lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;xticks&lt;/span&gt;=[], &lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;yticks&lt;/span&gt;=[],
                    &lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;zorder&lt;/span&gt;=&lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;2&lt;/span&gt;)

# &lt;span style="color: #ff0000; font-weight: bold;"&gt;add the image. zorder&amp;gt;1 makes sure it is on top&lt;/span&gt;
i_ax.imshow(im)

# &lt;span style="color: #ff0000; font-weight: bold;"&gt;print dir(i_ax)&lt;/span&gt;

plt.savefig(&lt;span style="color: #228b22;"&gt;'images/fig-in-plot-2.png'&lt;/span&gt;, &lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;dpi&lt;/span&gt;=&lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;300&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;div class="figure"&gt;
&lt;p&gt;&lt;img src="/media/2014-11-01-Annotating-matplotlib-figures/fig-in-plot-2.png"&gt; 
&lt;/p&gt;
&lt;p&gt;&lt;span class="figure-number"&gt;Figure 1:&lt;/span&gt; A matplotlib figure with an embedded images.&lt;/p&gt;
&lt;/div&gt;


&lt;p&gt;
There it is.&lt;/p&gt;
&lt;p&gt;Copyright (C) 2014 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;&lt;p&gt;&lt;a href="/org/2014/11/01/Annotating-matplotlib-figures.org"&gt;org-mode source&lt;/a&gt;&lt;p&gt;&lt;p&gt;Org-mode version = 8.2.7c&lt;/p&gt;]]></content:encoded>
    </item>
  </channel>
</rss>
