Tags: , , , ,

Categories:

Updated:




If you are blogging using the popular jekyll theme Minimal Mistakes, this post will help you adding your google adsense on your site.

Google Adsense Account

First open an account in Google Adsense and create codes for your ads. Google adsense provides different custom size ad codes (horizontal/vertical advertisements units). You can follow the steps:

  1. Click on New ad unit under Ad Units from my ads
  2. Select text and display ads
  3. Name the unit after previewing how the ad will appear on your site
  4. Click on Get Code visible on your unit. The code looks like as follows
    <script async  src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>  
    <!-- Single post - 1 (yourwebsite.com) -->  
    <ins  class="adsbygoogle"  
         style="display:block"  
         data-ad-client="0123456789012345"  
         data-ad-slot="01234567"  
         data-ad-format="auto"></ins>  
    <script>  
     (adsbygoogle  =  window.adsbygoogle  ||  []).push({});  
    </script>
    

Add Ad Code to Site

I have included codes of two ad units under the _includes directory. Simply add the code you got earlier in a <filename>.html file and put that under _includes directory. If you go to my site repository directory, you will find two files named advertisements.html and vertical_advertisement.html.

Now, since I use the single.html layout for my posts, I have included these ad pages in different locations under class page__content. The code now looks like as follows

<section class="page__content" itemprop="text">

<aside class="sidebar__right">
<nav class="toc">
<header><h4 class="nav__title"><i class="fas fa-heart"></i> Table of Contents</h4></header>
<ul class="toc__menu"><li>Shanto Roy</li></ul>

</nav>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- vertical_ad -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-1099218700412995"
     data-ad-slot="2669350977"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
</aside>


  







<div class="page__hero--overlay"
  style=" background-image: url('https://live.staticflickr.com/65535/51699602895_9f512e632d_o.png');"
>
  
    <div class="wrapper">
      <h1 id="page-title" class="page__title" itemprop="headline">
        
          How to Draw Multiple Axis in Matplotlib with different Scales (Value Range)

        
      </h1>
      
        <p class="page__lead">This post includes python code snippets (and fix for the official documentation) for plotting multiple axis in matplotlib with different scales or range
</p>
      
      

  <p class="page__meta">
    

    

    
      
      

      <span class="page__meta-readtime">
        <i class="far fa-clock" aria-hidden="true"></i>
        
          2 minute read
        
      </span>
    
  </p>


      
      
    </div>
  
  
</div>





<div id="main" role="main">
  
  <div class="sidebar sticky">
  

<div itemscope itemtype="http://schema.org/Person">

  
    <div class="author__avatar">
      

      
        <img src="https://shantoroy.com/assets/images/sroy.png" alt="Shanto Roy" itemprop="image">
      
    </div>
  

  <div class="author__content">
    
      <h3 class="author__name" itemprop="name">Shanto Roy</h3>
    
    
      <p class="author__bio" itemprop="description">
        Born to Explore  :)
      </p>
    
  </div>

  <div class="author__urls-wrapper">
    <button class="btn btn--inverse">Follow</button>
    <ul class="author__urls social-icons">
      
        <li itemprop="homeLocation" itemscope itemtype="http://schema.org/Place">
          <i class="fa fa-fw fa-map-marker" aria-hidden="true"></i> <span itemprop="name">TX, USA</span>
        </li>
      
      
      
        
          
            <li><a href="http://shantoroy.me"><i class="fas fa-fw fa-link" aria-hidden="true"></i> Google Site</a></li>
          
        
          
            <li><a href="mailto:royshantoiit@gmail.com"><i class="fas fa-fw fa-envelope-square" aria-hidden="true"></i> Email</a></li>
          
        
          
            <li><a href="https://linkedin.com/in/shanto-roy"><i class="fab fa-fw fa-linkedin" aria-hidden="true"></i> LinkedIn</a></li>
          
        
          
            <li><a href="https://github.com/shantoroy"><i class="fab fa-fw fa-github" aria-hidden="true"></i> GitHub</a></li>
          
        
          
            <li><a href="https://twitter.com/royshanto9"><i class="fab fa-fw fa-twitter" aria-hidden="true"></i> Twitter</a></li>
          
        
          
            <li><a href="https://www.instagram.com/shanto.roy.9/"><i class="fab fa-fw fa-instagram" aria-hidden="true"></i> Instagram</a></li>
          
        
          
            <li><a href="https://www.youtube.com/ShantoRoy"><i class="fab fa-fw fa-youtube" aria-hidden="true"></i> Youtube</a></li>
          
        
          
            <li><a href="https://www.buymeacoffee.com/shantoroy"><i class="fab fa fa-coffee" aria-hidden="true"></i> Buy me a Coffee</a></li>
          
        
      

      

      

      

      

      

      

      

      

      

      

      

      

      

      

      

      

      

      

      

      

      

      

      

      

      

      <!--
  <li>
    <a href="http://link-to-whatever-social-network.com/user/" itemprop="sameAs">
      <i class="fa fa-fw" aria-hidden="true"></i> Custom Social Profile Link
    </a>
  </li>
-->
    </ul>
  </div>
</div>

  
  </div>



  <article class="page" itemscope itemtype="http://schema.org/CreativeWork">
    <meta itemprop="headline" content="How to Draw Multiple Axis in Matplotlib with different Scales (Value Range)">
    <meta itemprop="description" content="This post includes python code snippets (and fix for the official documentation) for plotting multiple axis in matplotlib with different scales or range">
    <meta itemprop="datePublished" content="March 09, 2020">
    

    <div class="page__inner-wrap">
      
      
      <section class="page__meta">
        
        
  


  

  <p class="page__taxonomy">
    <strong><i class="fas fa-fw fa-tags" aria-hidden="true"></i> Tags: </strong>
    <span itemprop="keywords">
    
      <a href="/tags/#matplotlib" class="page__taxonomy-item p-category" rel="tag">Matplotlib</a><span class="sep">, </span>
    
      <a href="/tags/#pandas" class="page__taxonomy-item p-category" rel="tag">Pandas</a><span class="sep">, </span>
    
      <a href="/tags/#python" class="page__taxonomy-item p-category" rel="tag">Python</a><span class="sep">, </span>
    
      <a href="/tags/#tutorial" class="page__taxonomy-item p-category" rel="tag">Tutorial</a><span class="sep">, </span>
    
      <a href="/tags/#visualization" class="page__taxonomy-item p-category" rel="tag">Visualization</a>
    
    </span>
  </p>





  


  

  <p class="page__taxonomy">
    <strong><i class="fas fa-fw fa-folder-open" aria-hidden="true"></i> Categories: </strong>
    <span itemprop="keywords">
    
      <a href="/categories/#python" class="page__taxonomy-item p-category" rel="tag">Python</a>
    
    </span>
  </p>



        
          <p class="page__date"><strong><i class="fas fa-fw fa-calendar" aria-hidden="true"></i> Updated:</strong> <time datetime="2020-03-09T00:00:00+00:00">March 09, 2020</time></p><br><br><br>
        
      </section>  
      
      <section class="page__content" itemprop="text">
        
          <aside class="sidebar__right">
            <nav class="toc">
              <header><h4 class="nav__title"><i class="fas fa-heart"></i> Table of Contents</h4></header>
              

            </nav>
            <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- vertical_ad -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-1099218700412995"
     data-ad-slot="2669350977"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
          </aside>
        
        <p>I was looking for a way to implement multiple scales in Matplotlib. Multiple scale means using different scales in the same y-axis for different line plots.</p>

<p>For example, if I have four columns in my dataframe named, x, y1, y2, and y3; I will plot y1, y2, and y3 against x in the same plot (no subplots), where all y1, y2, and y3 have different ranges.</p>

<p>The official Matplotlib gallery has a <a href="https://matplotlib.org/2.0.2/examples/axes_grid/demo_parasite_axes2.html">demo code</a> for following example,</p>

<figure>
  <a href="https://matplotlib.org/2.0.2/_images/demo_parasite_axes2.png"><img src="https://matplotlib.org/2.0.2/_images/demo_parasite_axes2.png " /></a>
</figure>
<p><br /></p>

<p>However, the code does not generate exactly the same as depicted. It misses the second (Temparature, in this example) <code class="language-plaintext highlighter-rouge">y</code> axis. There fore, I had to tweak a little to fix the issue. Therefore I added the following code for fixing the second scale.</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">new_fixed_axis</span> <span class="o">=</span> <span class="n">par1</span><span class="p">.</span><span class="n">get_grid_helper</span><span class="p">().</span><span class="n">new_fixed_axis</span>
<span class="n">par1</span><span class="p">.</span><span class="n">axis</span><span class="p">[</span><span class="s">"right"</span><span class="p">]</span> <span class="o">=</span> <span class="n">new_fixed_axis</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="s">"right"</span><span class="p">,</span> <span class="n">axes</span><span class="o">=</span><span class="n">par1</span><span class="p">,</span>
                                        <span class="n">offset</span><span class="o">=</span><span class="p">(</span><span class="n">offset</span><span class="o">-</span><span class="mi">60</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span>
                                        
<span class="n">par1</span><span class="p">.</span><span class="n">axis</span><span class="p">[</span><span class="s">"right"</span><span class="p">].</span><span class="n">toggle</span><span class="p">(</span><span class="nb">all</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</code></pre></div></div>

<p>Now, if you want to create something like this plot from the data you have in an excel file or a CSV file, the whole code should look like as follows:</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="n">plt</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="n">pd</span>

<span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="p">.</span><span class="n">read_excel</span><span class="p">(</span><span class="s">"testfile.xlsx"</span><span class="p">,</span> <span class="n">engine</span><span class="o">=</span><span class="s">'openpyxl'</span><span class="p">)</span>
<span class="c1"># df = pd.read_csv("text.csv")
</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s">"column_x"</span><span class="p">].</span><span class="n">tolist</span><span class="p">()</span>

<span class="n">y_1</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s">"column_y1"</span><span class="p">].</span><span class="n">tolist</span><span class="p">()</span>
<span class="n">y_2</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s">"column_y2"</span><span class="p">].</span><span class="n">tolist</span><span class="p">()</span>
<span class="n">y_3</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s">"column_y3"</span><span class="p">].</span><span class="n">tolist</span><span class="p">()</span>

<span class="n">host</span> <span class="o">=</span> <span class="n">host_subplot</span><span class="p">(</span><span class="mi">111</span><span class="p">,</span> <span class="n">axes_class</span><span class="o">=</span><span class="n">AA</span><span class="p">.</span><span class="n">Axes</span><span class="p">)</span>
<span class="n">plt</span><span class="p">.</span><span class="n">subplots_adjust</span><span class="p">(</span><span class="n">right</span><span class="o">=</span><span class="mf">0.85</span><span class="p">)</span>

<span class="n">par1</span> <span class="o">=</span> <span class="n">host</span><span class="p">.</span><span class="n">twinx</span><span class="p">()</span>
<span class="n">par2</span> <span class="o">=</span> <span class="n">host</span><span class="p">.</span><span class="n">twinx</span><span class="p">()</span>

<span class="n">offset</span> <span class="o">=</span> <span class="mi">50</span>

<span class="n">new_fixed_axis</span> <span class="o">=</span> <span class="n">par1</span><span class="p">.</span><span class="n">get_grid_helper</span><span class="p">().</span><span class="n">new_fixed_axis</span>
<span class="n">par1</span><span class="p">.</span><span class="n">axis</span><span class="p">[</span><span class="s">"right"</span><span class="p">]</span> <span class="o">=</span> <span class="n">new_fixed_axis</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="s">"right"</span><span class="p">,</span> <span class="n">axes</span><span class="o">=</span><span class="n">par1</span><span class="p">,</span>
                                        <span class="n">offset</span><span class="o">=</span><span class="p">(</span><span class="n">offset</span><span class="o">-</span><span class="mi">50</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span>

<span class="n">par1</span><span class="p">.</span><span class="n">axis</span><span class="p">[</span><span class="s">"right"</span><span class="p">].</span><span class="n">toggle</span><span class="p">(</span><span class="nb">all</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="n">new_fixed_axis</span> <span class="o">=</span> <span class="n">par2</span><span class="p">.</span><span class="n">get_grid_helper</span><span class="p">().</span><span class="n">new_fixed_axis</span>
<span class="n">par2</span><span class="p">.</span><span class="n">axis</span><span class="p">[</span><span class="s">"right"</span><span class="p">]</span> <span class="o">=</span> <span class="n">new_fixed_axis</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="s">"right"</span><span class="p">,</span> <span class="n">axes</span><span class="o">=</span><span class="n">par2</span><span class="p">,</span>
                                        <span class="n">offset</span><span class="o">=</span><span class="p">(</span><span class="n">offset</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span>

<span class="n">par2</span><span class="p">.</span><span class="n">axis</span><span class="p">[</span><span class="s">"right"</span><span class="p">].</span><span class="n">toggle</span><span class="p">(</span><span class="nb">all</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="n">host</span><span class="p">.</span><span class="n">set_xlim</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">150</span><span class="p">)</span>
<span class="n">host</span><span class="p">.</span><span class="n">set_ylim</span><span class="p">(</span><span class="o">-</span><span class="mi">5080</span><span class="p">,</span> <span class="o">-</span><span class="mi">5000</span><span class="p">)</span>

<span class="n">host</span><span class="p">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s">'x_label'</span><span class="p">)</span>
<span class="n">host</span><span class="p">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s">'y1 scale label'</span><span class="p">)</span>
<span class="n">par1</span><span class="p">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s">'y2 scale label'</span><span class="p">)</span>
<span class="n">par2</span><span class="p">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s">'y3 scale label'</span><span class="p">)</span>

<span class="c1"># or you can put labels like following, both works fine
</span><span class="n">p1</span><span class="p">,</span> <span class="o">=</span> <span class="n">host</span><span class="p">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y_1</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s">'Total Energy, E(ev)'</span><span class="p">,</span> <span class="n">marker</span><span class="o">=</span><span class="s">'x'</span><span class="p">)</span>
<span class="n">p2</span><span class="p">,</span> <span class="o">=</span> <span class="n">par1</span><span class="p">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y_2</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s">'Total Energy per atom (ev/atom)'</span><span class="p">,</span> <span class="n">marker</span><span class="o">=</span><span class="s">'^'</span><span class="p">)</span>
<span class="n">p3</span><span class="p">,</span> <span class="o">=</span> <span class="n">par2</span><span class="p">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y_3</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s">"E-E_best (ev/atom)"</span><span class="p">,</span> <span class="n">marker</span><span class="o">=</span><span class="s">'o'</span><span class="p">)</span>

<span class="n">par1</span><span class="p">.</span><span class="n">set_ylim</span><span class="p">(</span><span class="o">-</span><span class="mi">844</span><span class="p">,</span> <span class="o">-</span><span class="mi">830</span><span class="p">)</span>
<span class="n">par2</span><span class="p">.</span><span class="n">set_ylim</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mf">0.20</span><span class="p">)</span>

<span class="n">host</span><span class="p">.</span><span class="n">legend</span><span class="p">()</span>

<span class="n">host</span><span class="p">.</span><span class="n">axis</span><span class="p">[</span><span class="s">"left"</span><span class="p">].</span><span class="n">label</span><span class="p">.</span><span class="n">set_color</span><span class="p">(</span><span class="n">p1</span><span class="p">.</span><span class="n">get_color</span><span class="p">())</span>
<span class="n">par1</span><span class="p">.</span><span class="n">axis</span><span class="p">[</span><span class="s">"right"</span><span class="p">].</span><span class="n">label</span><span class="p">.</span><span class="n">set_color</span><span class="p">(</span><span class="n">p2</span><span class="p">.</span><span class="n">get_color</span><span class="p">())</span>
<span class="n">par2</span><span class="p">.</span><span class="n">axis</span><span class="p">[</span><span class="s">"right"</span><span class="p">].</span><span class="n">label</span><span class="p">.</span><span class="n">set_color</span><span class="p">(</span><span class="n">p3</span><span class="p">.</span><span class="n">get_color</span><span class="p">())</span>

<span class="n">plt</span><span class="p">.</span><span class="n">draw</span><span class="p">()</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span>
</code></pre></div></div>

<p>I have found another useful code (2nd best) in this <a href="https://stackoverflow.com/questions/9103166/multiple-axis-in-matplotlib-with-different-scales?newreg=92ddfbdecd494c4490bc0ea008e7fd9d">Stackoverflow Thread</a>. You can use that simpler version either. I am adding my personally modified version here,</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="n">plt</span> 
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="n">pd</span>

<span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="p">.</span><span class="n">read_excel</span><span class="p">(</span><span class="s">"testfile.xlsx"</span><span class="p">,</span> <span class="n">engine</span><span class="o">=</span><span class="s">'openpyxl'</span><span class="p">)</span>
<span class="c1"># df = pd.read_csv("text.csv")
</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s">"column_x"</span><span class="p">].</span><span class="n">tolist</span><span class="p">()</span>

<span class="n">y_1</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s">"column_y1"</span><span class="p">].</span><span class="n">tolist</span><span class="p">()</span>
<span class="n">y_2</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s">"column_y2"</span><span class="p">].</span><span class="n">tolist</span><span class="p">()</span>
<span class="n">y_3</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s">"column_y3"</span><span class="p">].</span><span class="n">tolist</span><span class="p">()</span>

<span class="n">fig</span><span class="p">,</span> <span class="n">host</span> <span class="o">=</span> <span class="n">plt</span><span class="p">.</span><span class="n">subplots</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">8</span><span class="p">,</span><span class="mi">5</span><span class="p">))</span> <span class="c1"># (width, height) in inches
</span>    
<span class="n">par1</span> <span class="o">=</span> <span class="n">host</span><span class="p">.</span><span class="n">twinx</span><span class="p">()</span>
<span class="n">par2</span> <span class="o">=</span> <span class="n">host</span><span class="p">.</span><span class="n">twinx</span><span class="p">()</span>
    
<span class="n">host</span><span class="p">.</span><span class="n">set_xlim</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">150</span><span class="p">)</span>
<span class="n">host</span><span class="p">.</span><span class="n">set_ylim</span><span class="p">(</span><span class="o">-</span><span class="mi">5080</span><span class="p">,</span> <span class="o">-</span><span class="mi">5000</span><span class="p">)</span>
<span class="n">par1</span><span class="p">.</span><span class="n">set_ylim</span><span class="p">(</span><span class="o">-</span><span class="mi">844</span><span class="p">,</span> <span class="o">-</span><span class="mi">830</span><span class="p">)</span>
<span class="n">par2</span><span class="p">.</span><span class="n">set_ylim</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mf">0.20</span><span class="p">)</span>
    
<span class="n">host</span><span class="p">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s">'x_label'</span><span class="p">)</span>
<span class="n">host</span><span class="p">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s">'y1 scale label'</span><span class="p">)</span>
<span class="n">par1</span><span class="p">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s">'y2 scale label'</span><span class="p">)</span>
<span class="n">par2</span><span class="p">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s">'y3 scale label'</span><span class="p">)</span>

<span class="n">color1</span> <span class="o">=</span> <span class="n">plt</span><span class="p">.</span><span class="n">cm</span><span class="p">.</span><span class="n">viridis</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="n">color2</span> <span class="o">=</span> <span class="n">plt</span><span class="p">.</span><span class="n">cm</span><span class="p">.</span><span class="n">viridis</span><span class="p">(</span><span class="mf">0.5</span><span class="p">)</span>
<span class="n">color3</span> <span class="o">=</span> <span class="n">plt</span><span class="p">.</span><span class="n">cm</span><span class="p">.</span><span class="n">viridis</span><span class="p">(.</span><span class="mi">9</span><span class="p">)</span>

<span class="n">p1</span><span class="p">,</span> <span class="o">=</span> <span class="n">host</span><span class="p">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y_1</span><span class="p">,</span>    <span class="n">color</span><span class="o">=</span><span class="n">color1</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s">"Density"</span><span class="p">,</span> <span class="n">marker</span><span class="o">=</span><span class="s">'x'</span><span class="p">)</span>
<span class="n">p2</span><span class="p">,</span> <span class="o">=</span> <span class="n">par1</span><span class="p">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y_2</span><span class="p">,</span>    <span class="n">color</span><span class="o">=</span><span class="n">color2</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s">"Temperature"</span><span class="p">,</span> <span class="n">marker</span><span class="o">=</span><span class="s">'^'</span><span class="p">)</span>
<span class="n">p3</span><span class="p">,</span> <span class="o">=</span> <span class="n">par2</span><span class="p">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y_3</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="n">color3</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s">"Velocity"</span><span class="p">,</span> <span class="n">marker</span><span class="o">=</span><span class="s">'o'</span><span class="p">)</span>

<span class="n">lns</span> <span class="o">=</span> <span class="p">[</span><span class="n">p1</span><span class="p">,</span> <span class="n">p2</span><span class="p">,</span> <span class="n">p3</span><span class="p">]</span>
<span class="n">host</span><span class="p">.</span><span class="n">legend</span><span class="p">(</span><span class="n">handles</span><span class="o">=</span><span class="n">lns</span><span class="p">,</span> <span class="n">loc</span><span class="o">=</span><span class="s">'best'</span><span class="p">)</span>

<span class="c1"># right, left, top, bottom
</span><span class="n">par2</span><span class="p">.</span><span class="n">spines</span><span class="p">[</span><span class="s">'right'</span><span class="p">].</span><span class="n">set_position</span><span class="p">((</span><span class="s">'outward'</span><span class="p">,</span> <span class="mi">60</span><span class="p">))</span>

<span class="c1"># no x-ticks                 
</span><span class="n">par2</span><span class="p">.</span><span class="n">xaxis</span><span class="p">.</span><span class="n">set_ticks</span><span class="p">([])</span>

<span class="n">host</span><span class="p">.</span><span class="n">yaxis</span><span class="p">.</span><span class="n">label</span><span class="p">.</span><span class="n">set_color</span><span class="p">(</span><span class="n">p1</span><span class="p">.</span><span class="n">get_color</span><span class="p">())</span>
<span class="n">par1</span><span class="p">.</span><span class="n">yaxis</span><span class="p">.</span><span class="n">label</span><span class="p">.</span><span class="n">set_color</span><span class="p">(</span><span class="n">p2</span><span class="p">.</span><span class="n">get_color</span><span class="p">())</span>
<span class="n">par2</span><span class="p">.</span><span class="n">yaxis</span><span class="p">.</span><span class="n">label</span><span class="p">.</span><span class="n">set_color</span><span class="p">(</span><span class="n">p3</span><span class="p">.</span><span class="n">get_color</span><span class="p">())</span>

<span class="c1"># Adjust spacings w.r.t. figsize
</span><span class="n">fig</span><span class="p">.</span><span class="n">tight_layout</span><span class="p">()</span>

<span class="n">plt</span><span class="p">.</span><span class="n">savefig</span><span class="p">(</span><span class="s">"pyplot_multiple_y-axis.png"</span><span class="p">,</span> <span class="n">dpi</span><span class="o">=</span><span class="mi">300</span><span class="p">)</span>
</code></pre></div></div>

<p>That’s all for today. Cheers!
<!--stackedit_data:
eyJoaXN0b3J5IjpbMTczMjYyOTg1OV19
--></p>

	      <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- horizontal_ad -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-1099218700412995"
     data-ad-slot="7013535065"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>

        
      </section>

<!--       <footer class="page__meta">
        
        
  


  

  <p class="page__taxonomy">
    <strong><i class="fas fa-fw fa-tags" aria-hidden="true"></i> Tags: </strong>
    <span itemprop="keywords">
    
      <a href="/tags/#matplotlib" class="page__taxonomy-item p-category" rel="tag">Matplotlib</a><span class="sep">, </span>
    
      <a href="/tags/#pandas" class="page__taxonomy-item p-category" rel="tag">Pandas</a><span class="sep">, </span>
    
      <a href="/tags/#python" class="page__taxonomy-item p-category" rel="tag">Python</a><span class="sep">, </span>
    
      <a href="/tags/#tutorial" class="page__taxonomy-item p-category" rel="tag">Tutorial</a><span class="sep">, </span>
    
      <a href="/tags/#visualization" class="page__taxonomy-item p-category" rel="tag">Visualization</a>
    
    </span>
  </p>





  


  

  <p class="page__taxonomy">
    <strong><i class="fas fa-fw fa-folder-open" aria-hidden="true"></i> Categories: </strong>
    <span itemprop="keywords">
    
      <a href="/categories/#python" class="page__taxonomy-item p-category" rel="tag">Python</a>
    
    </span>
  </p>



        
          <p class="page__date"><strong><i class="fa fa-fw fa-calendar" aria-hidden="true"></i> Updated:</strong> <time datetime="2020-03-09T00:00:00+00:00">March 09, 2020</time></p>
        
      </footer> -->

      <section class="page__share">
  
    <h4 class="page__share-title">Share on</h4>
  

  <a href="https://twitter.com/intent/tweet?text=How+to+Draw+Multiple+Axis+in+Matplotlib+with+different+Scales+%28Value+Range%29%20https%3A%2F%2Fshantoroy.com%2Fpython%2Fdraw-multiple-axis-in-matplotlib-with-different-scales%2F" class="btn btn--twitter" onclick="window.open(this.href, 'window', 'left=20,top=20,width=500,height=500,toolbar=1,resizable=0'); return false;" title="Share on Twitter"><i class="fab fa-fw fa-twitter" aria-hidden="true"></i><span> Twitter</span></a>

  <a href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fshantoroy.com%2Fpython%2Fdraw-multiple-axis-in-matplotlib-with-different-scales%2F" class="btn btn--facebook" onclick="window.open(this.href, 'window', 'left=20,top=20,width=500,height=500,toolbar=1,resizable=0'); return false;" title="Share on Facebook"><i class="fab fa-fw fa-facebook" aria-hidden="true"></i><span> Facebook</span></a>

  <a href="https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fshantoroy.com%2Fpython%2Fdraw-multiple-axis-in-matplotlib-with-different-scales%2F" class="btn btn--linkedin" onclick="window.open(this.href, 'window', 'left=20,top=20,width=500,height=500,toolbar=1,resizable=0'); return false;" title="Share on LinkedIn"><i class="fab fa-fw fa-linkedin" aria-hidden="true"></i><span> LinkedIn</span></a>
</section>


      
  <nav class="pagination">
    
      <a href="/travel/trip-to-San-Jacinto-Battleground-State-Historic-Site-huntsville-lake-Houston-TX-USA/" class="pagination--pager" title="Hangouts near Houston: San Jacinto Battleground and Huntsville Lake
">Previous</a>
    
    
      <a href="/jekyll/how-I-added-google-adsense-to-my-jekyll-minimal-mistakes-blog/" class="pagination--pager" title="Google Adsense in Jekyll Static Site or Minimal Mistakes Blog Theme
">Next</a>
    
  </nav>


    </div>

    
      <div class="page__comments">
  
  
      <h4 class="page__comments-title">Leave a comment</h4>
      <section class="fb-comments" data-href="https://shantoroy.com/python/draw-multiple-axis-in-matplotlib-with-different-scales/" data-mobile="true" data-num-posts="5" data-width="100%" data-colorscheme="light"></section>
    
</div>
    
  </article>

  
  
    <div class="page__related">
      <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- horizontal_ad -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-1099218700412995"
     data-ad-slot="7013535065"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>

      <h4 class="page__related-title">You may also enjoy</h4>
      <div class="grid__wrapper">
        
          



<div class="grid__item">
  <article class="archive__item" itemscope itemtype="https://schema.org/CreativeWork">
    
      <div class="archive__item-teaser">
        <img src="https://live.staticflickr.com/65535/52104303039_044542e267_o.png" alt="">
      </div>
    
    <h2 class="archive__item-title no_toc" itemprop="headline">
      
        <a href="/latex/convert-matplotlib-plot-to-latex-plot/" rel="permalink">How to Convert Python Matplotlib Plots to Latex Plots (Easiest Way)
</a>
      
    </h2>
    

  <p class="page__meta">
    

    

    
      
      

      <span class="page__meta-readtime">
        <i class="far fa-clock" aria-hidden="true"></i>
        
          3 minute read
        
      </span>
    
  </p>


    <p class="archive__item-excerpt" itemprop="description">I used to draw plots using matplotlib a lot and then add the figures in reports. Later I switched to latex plots as it is easier and works best for numerous ...</p>
  </article>
</div>

        
          



<div class="grid__item">
  <article class="archive__item" itemscope itemtype="https://schema.org/CreativeWork">
    
      <div class="archive__item-teaser">
        <img src="https://live.staticflickr.com/65535/52023019312_ca3bbdb467_o.png" alt="">
      </div>
    
    <h2 class="archive__item-title no_toc" itemprop="headline">
      
        <a href="/webscrapping/click-button-show-more-on-medium-dot-com-via-selenium/" rel="permalink">Web Scrapping: Clicking the ‘Show More’ Button Multiple times in Medium.com Blog via Selenium
</a>
      
    </h2>
    

  <p class="page__meta">
    

    

    
      
      

      <span class="page__meta-readtime">
        <i class="far fa-clock" aria-hidden="true"></i>
        
          2 minute read
        
      </span>
    
  </p>


    <p class="archive__item-excerpt" itemprop="description">This post explains the way and provides necessary code to enable clicking on the ‘show more’ button on medium blog site via selenium
</p>
  </article>
</div>

        
          



<div class="grid__item">
  <article class="archive__item" itemscope itemtype="https://schema.org/CreativeWork">
    
      <div class="archive__item-teaser">
        <img src="https://live.staticflickr.com/65535/52024571959_915c770b73_o.png" alt="">
      </div>
    
    <h2 class="archive__item-title no_toc" itemprop="headline">
      
        <a href="/webscrapping/web-scrap-a-medium-dot-com-blog-post/" rel="permalink">Web Scrapping: Finding Necessary Contents from a Medium Dot Com Blog Post
</a>
      
    </h2>
    

  <p class="page__meta">
    

    

    
      
      

      <span class="page__meta-readtime">
        <i class="far fa-clock" aria-hidden="true"></i>
        
          4 minute read
        
      </span>
    
  </p>


    <p class="archive__item-excerpt" itemprop="description">This post explains the way and provides necessary code to scrap a post from Medium dot com Blog
</p>
  </article>
</div>

        
          



<div class="grid__item">
  <article class="archive__item" itemscope itemtype="https://schema.org/CreativeWork">
    
      <div class="archive__item-teaser">
        <img src="https://live.staticflickr.com/65535/52014195409_effd1f1538_o.png" alt="">
      </div>
    
    <h2 class="archive__item-title no_toc" itemprop="headline">
      
        <a href="/latex/University-of-Houston-NSM-phd-ms-latex-template/" rel="permalink">PhD/MS Dissertation Latex Template for Students of the College of Natural Sciences and Mathematics- University of Houston
</a>
      
    </h2>
    

  <p class="page__meta">
    

    

    
      
      

      <span class="page__meta-readtime">
        <i class="far fa-clock" aria-hidden="true"></i>
        
          2 minute read
        
      </span>
    
  </p>


    <p class="archive__item-excerpt" itemprop="description">This post includes an organized dissertation template (MS/PhD UH-NSM) with required packages and examples.
</p>
  </article>
</div>

        
      </div>
    </div>
  
  
</div>

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- horizontal_ad -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-1099218700412995"
     data-ad-slot="7013535065"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>


</section>

So, you see, I have added the vertical ad unit after the table of contents and added another (horizontal ad unit) after the content. You can place ads in different places based on where you exactly want this ads to show up. You just need to add this code

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- horizontal_ad -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-1099218700412995"
     data-ad-slot="7013535065"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>

If you want to know how to track your website or blog using Google Analytics, feel free to read my other post: Google Analytics in Minimal Mistakes Blog Theme

Keep growing your audience and earning some passive money. Happy blogging!!! :sunglasses:

Leave a comment