{
  "id": 67820,
  "title": "pyplot.subplots: different behavior in python and jupyter notebook.",
  "url": "/competitions/quickdraw-doodle-recognition/discussion/67820",
  "author_name": "Vitaliy",
  "post_date": "2018-10-05T19:35:46.427000",
  "votes": -1,
  "comment_count": 0,
  "views": 0,
  "content": "<p>Hi *,</p>\n\n<p>While taking part in kaggle competition, i got some weird problem.\nBasically, I am trying to convert vector representation of am image to png file. It worked perfectly in iPython, code below:</p>\n\n<pre><code>def drawing_to_np_prepare_data(drawing):\n    drawing = eval(drawing)\n    fig, ax = plt.subplots()\n    plt.close(fig)\n    print('[debug] ax=',ax)\n    for x,y in drawing:\n        ax.plot(x, y, marker='.')\n        ax.axis('off')\n    fig.canvas.draw()\n\n    # Convert images to numpy array\n    np_drawing = np.array(fig.canvas.renderer._renderer)\n\n    print('[debug] fig_size=',fig.get_size_inches())\n    print('[debug] dpi=',fig.dpi)\n\n    print('[debug] shape=',np_drawing.shape)\n    print('[debug] size=',np_drawing.size)\n    print('[debug] shape=',np_drawing.shape)\n\n    im = cv2.cvtColor(np_drawing.astype(np.uint8), cv2.COLOR_BGR2RGB)\n\n    # compress\n    compressed_array = io.BytesIO()\n    np.savez_compressed(compressed_array, im)\n    compressed_array.seek(0)\n    print('[debug] size=',np_drawing.shape)\n    return compressed_array\n</code></pre>\n\n<p>The result shows:</p>\n\n<pre><code>[debug] ax=AxesSubplot(0.125,0.125;0.775x0.755)\n[debug] fig_size= [6. 4.]\n[debug] dpi= 72.0\n[debug] np_drawing.size= 497664\n[debug] shape= (288, 432, 4)\n[debug] size= 1880\n</code></pre>\n\n<p>which satisfy my needs: i am getting an image with compressed size &lt; 2Kb</p>\n\n<p>However, when I run this code in python from CLI, I am getting quite different result:</p>\n\n<pre><code>[debug] ax=AxesSubplot(0.125,0.11;0.775x0.77)\n[debug] fig_size= [6.4 4.8]\n[debug] dpi= 100.0\n[debug] np_drawing.size= 1228800\n[debug] shape= (480, 640, 4)\n[debug] size= 13096\n</code></pre>\n\n<p>as you can see, figure size, dpi, axes are different and as a result, size at the end are also different.</p>\n\n<p>I can pass arguments to subplots:</p>\n\n<pre><code>  plt.subplots(figsize=(6.,4.), dpi=72)\n</code></pre>\n\n<p>which corrects parameters except axes (and size, I guess because of different axes):</p>\n\n<pre><code>[debug] ax=AxesSubplot(0.125,0.11;0.775x0.77)\n[debug] fig_size= [6. 4.]\n[debug] dpi= 72.0\n[debug] np_drawing.size= 497664\n[debug] shape= (288, 432, 4)\n[debug] size= 8214\n</code></pre>\n\n<p>Note: I've checked library versions and they are the same.</p>\n\n<p>So, multiple questions arise:</p>\n\n<ol>\n<li><p>Why subplots give different axes, shape and resolution?</p></li>\n<li><p>How to correct axes?</p></li>\n<li><p>How to get the same behaviors in python?</p></li>\n</ol>\n\n<p>I want to understand the what is going on. Thanks!</p>",
  "messages": [
    {
      "id": 399443,
      "postDate": "2018-10-05T19:35:46.427Z",
      "content": "<p>Hi *,</p>\n\n<p>While taking part in kaggle competition, i got some weird problem.\nBasically, I am trying to convert vector representation of am image to png file. It worked perfectly in iPython, code below:</p>\n\n<pre><code>def drawing_to_np_prepare_data(drawing):\n    drawing = eval(drawing)\n    fig, ax = plt.subplots()\n    plt.close(fig)\n    print('[debug] ax=',ax)\n    for x,y in drawing:\n        ax.plot(x, y, marker='.')\n        ax.axis('off')\n    fig.canvas.draw()\n\n    # Convert images to numpy array\n    np_drawing = np.array(fig.canvas.renderer._renderer)\n\n    print('[debug] fig_size=',fig.get_size_inches())\n    print('[debug] dpi=',fig.dpi)\n\n    print('[debug] shape=',np_drawing.shape)\n    print('[debug] size=',np_drawing.size)\n    print('[debug] shape=',np_drawing.shape)\n\n    im = cv2.cvtColor(np_drawing.astype(np.uint8), cv2.COLOR_BGR2RGB)\n\n    # compress\n    compressed_array = io.BytesIO()\n    np.savez_compressed(compressed_array, im)\n    compressed_array.seek(0)\n    print('[debug] size=',np_drawing.shape)\n    return compressed_array\n</code></pre>\n\n<p>The result shows:</p>\n\n<pre><code>[debug] ax=AxesSubplot(0.125,0.125;0.775x0.755)\n[debug] fig_size= [6. 4.]\n[debug] dpi= 72.0\n[debug] np_drawing.size= 497664\n[debug] shape= (288, 432, 4)\n[debug] size= 1880\n</code></pre>\n\n<p>which satisfy my needs: i am getting an image with compressed size &lt; 2Kb</p>\n\n<p>However, when I run this code in python from CLI, I am getting quite different result:</p>\n\n<pre><code>[debug] ax=AxesSubplot(0.125,0.11;0.775x0.77)\n[debug] fig_size= [6.4 4.8]\n[debug] dpi= 100.0\n[debug] np_drawing.size= 1228800\n[debug] shape= (480, 640, 4)\n[debug] size= 13096\n</code></pre>\n\n<p>as you can see, figure size, dpi, axes are different and as a result, size at the end are also different.</p>\n\n<p>I can pass arguments to subplots:</p>\n\n<pre><code>  plt.subplots(figsize=(6.,4.), dpi=72)\n</code></pre>\n\n<p>which corrects parameters except axes (and size, I guess because of different axes):</p>\n\n<pre><code>[debug] ax=AxesSubplot(0.125,0.11;0.775x0.77)\n[debug] fig_size= [6. 4.]\n[debug] dpi= 72.0\n[debug] np_drawing.size= 497664\n[debug] shape= (288, 432, 4)\n[debug] size= 8214\n</code></pre>\n\n<p>Note: I've checked library versions and they are the same.</p>\n\n<p>So, multiple questions arise:</p>\n\n<ol>\n<li><p>Why subplots give different axes, shape and resolution?</p></li>\n<li><p>How to correct axes?</p></li>\n<li><p>How to get the same behaviors in python?</p></li>\n</ol>\n\n<p>I want to understand the what is going on. Thanks!</p>",
      "rawMarkdown": "Hi *,\n\nWhile taking part in kaggle competition, i got some weird problem.\nBasically, I am trying to convert vector representation of am image to png file. It worked perfectly in iPython, code below:\n\n    def drawing_to_np_prepare_data(drawing):\n        drawing = eval(drawing)\n        fig, ax = plt.subplots()\n        plt.close(fig)\n        print('[debug] ax=',ax)\n        for x,y in drawing:\n            ax.plot(x, y, marker='.')\n            ax.axis('off')\n        fig.canvas.draw()\n    \n        # Convert images to numpy array\n        np_drawing = np.array(fig.canvas.renderer._renderer)\n    \n        print('[debug] fig_size=',fig.get_size_inches())\n        print('[debug] dpi=',fig.dpi)\n    \n        print('[debug] shape=',np_drawing.shape)\n        print('[debug] size=',np_drawing.size)\n        print('[debug] shape=',np_drawing.shape)\n    \n        im = cv2.cvtColor(np_drawing.astype(np.uint8), cv2.COLOR_BGR2RGB)\n     \n        # compress\n        compressed_array = io.BytesIO()\n        np.savez_compressed(compressed_array, im)\n        compressed_array.seek(0)\n        print('[debug] size=',np_drawing.shape)\n        return compressed_array\n\nThe result shows:\n\n    [debug] ax=AxesSubplot(0.125,0.125;0.775x0.755)\n    [debug] fig_size= [6. 4.]\n    [debug] dpi= 72.0\n    [debug] np_drawing.size= 497664\n    [debug] shape= (288, 432, 4)\n    [debug] size= 1880\n\nwhich satisfy my needs: i am getting an image with compressed size &lt; 2Kb\n\nHowever, when I run this code in python from CLI, I am getting quite different result:\n\n    [debug] ax=AxesSubplot(0.125,0.11;0.775x0.77)\n    [debug] fig_size= [6.4 4.8]\n    [debug] dpi= 100.0\n    [debug] np_drawing.size= 1228800\n    [debug] shape= (480, 640, 4)\n    [debug] size= 13096\n\nas you can see, figure size, dpi, axes are different and as a result, size at the end are also different.\n\nI can pass arguments to subplots:\n\n  \n\n      plt.subplots(figsize=(6.,4.), dpi=72)\n\nwhich corrects parameters except axes (and size, I guess because of different axes):\n\n    [debug] ax=AxesSubplot(0.125,0.11;0.775x0.77)\n    [debug] fig_size= [6. 4.]\n    [debug] dpi= 72.0\n    [debug] np_drawing.size= 497664\n    [debug] shape= (288, 432, 4)\n    [debug] size= 8214\n\n\nNote: I've checked library versions and they are the same.\n\nSo, multiple questions arise:\n\n1. Why subplots give different axes, shape and resolution?\n\n2. How to correct axes?\n\n3. How to get the same behaviors in python?\n\nI want to understand the what is going on. Thanks!\n\n",
      "votes": -1
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "399443": "Hi *,\n\nWhile taking part in kaggle competition, i got some weird problem.\nBasically, I am trying to convert vector representation of am image to png file. It worked perfectly in iPython, code below:\n\n    def drawing_to_np_prepare_data(drawing):\n        drawing = eval(drawing)\n        fig, ax = plt.subplots()\n        plt.close(fig)\n        print('[debug] ax=',ax)\n        for x,y in drawing:\n            ax.plot(x, y, marker='.')\n            ax.axis('off')\n        fig.canvas.draw()\n    \n        # Convert images to numpy array\n        np_drawing = np.array(fig.canvas.renderer._renderer)\n    \n        print('[debug] fig_size=',fig.get_size_inches())\n        print('[debug] dpi=',fig.dpi)\n    \n        print('[debug] shape=',np_drawing.shape)\n        print('[debug] size=',np_drawing.size)\n        print('[debug] shape=',np_drawing.shape)\n    \n        im = cv2.cvtColor(np_drawing.astype(np.uint8), cv2.COLOR_BGR2RGB)\n     \n        # compress\n        compressed_array = io.BytesIO()\n        np.savez_compressed(compressed_array, im)\n        compressed_array.seek(0)\n        print('[debug] size=',np_drawing.shape)\n        return compressed_array\n\nThe result shows:\n\n    [debug] ax=AxesSubplot(0.125,0.125;0.775x0.755)\n    [debug] fig_size= [6. 4.]\n    [debug] dpi= 72.0\n    [debug] np_drawing.size= 497664\n    [debug] shape= (288, 432, 4)\n    [debug] size= 1880\n\nwhich satisfy my needs: i am getting an image with compressed size &lt; 2Kb\n\nHowever, when I run this code in python from CLI, I am getting quite different result:\n\n    [debug] ax=AxesSubplot(0.125,0.11;0.775x0.77)\n    [debug] fig_size= [6.4 4.8]\n    [debug] dpi= 100.0\n    [debug] np_drawing.size= 1228800\n    [debug] shape= (480, 640, 4)\n    [debug] size= 13096\n\nas you can see, figure size, dpi, axes are different and as a result, size at the end are also different.\n\nI can pass arguments to subplots:\n\n  \n\n      plt.subplots(figsize=(6.,4.), dpi=72)\n\nwhich corrects parameters except axes (and size, I guess because of different axes):\n\n    [debug] ax=AxesSubplot(0.125,0.11;0.775x0.77)\n    [debug] fig_size= [6. 4.]\n    [debug] dpi= 72.0\n    [debug] np_drawing.size= 497664\n    [debug] shape= (288, 432, 4)\n    [debug] size= 8214\n\n\nNote: I've checked library versions and they are the same.\n\nSo, multiple questions arise:\n\n1. Why subplots give different axes, shape and resolution?\n\n2. How to correct axes?\n\n3. How to get the same behaviors in python?\n\nI want to understand the what is going on. Thanks!\n\n"
  }
}