matplotlib: Don't compress plot in the horizontal direction
Question: I am very new to matplotlib and I am having some difficulty with this figure:

[![enter image description
here](http://i.stack.imgur.com/CDuGd.png)](http://i.stack.imgur.com/CDuGd.png)

I have a text file with x y point groups that I should plot. However, the x
points overlap in each group, so I add an offset to each x axis point for each
group.

Usually, the single groups look like this:

[![enter image description
here](http://i.stack.imgur.com/6vzDa.png)](http://i.stack.imgur.com/6vzDa.png)
[![enter image description
here](http://i.stack.imgur.com/lJ9lb.png)](http://i.stack.imgur.com/lJ9lb.png)

Note that the x axis in the first image ends where the x-axis in the second
image begins.

My problem is that the resulting image is squelched/compressed and not really
"readable".

I tried increasing the value that is added to the x-axis for each group/image,
but it just compresses each group even more.

I tried suggestions to use `rcParams` or set the `dpi` value of the resulting
image, but nothing does the job:

    
    
    from pylab import rcParams
    rcParams['figure.figsize'] = 50, 100
    plt.savefig('result.png', dpi=200,pad_inches=5)
    

What am I doing wrong or looking for?

PS: The data and code is [here](https://github.com/ctfs/write-
ups-2015/tree/master/volgactf-quals-2015/stego/strange-text). To see what my
problem is, call `python2.7 plotit.py text.txt`


Answer: If I understand your question, you don't like your image being too compressed
in the horizontal direction. It happens because by default `matplotlib`
chooses the aspect ratio necessary to fill the given figure size. You were on
the right track with changing changing `figsize`, but if you want to change it
in `rcParams`, you have to put this call somewhere **before you start
plotting.** The other approach is to use the stateless API, that is `fig =
plt.figure(figsize=(8,2)); s = fig.add_subplot(111); s.plot(...)`. That's what
you get:

    
    
    from pylab import rcParams
    rcParams['figure.figsize'] = 8, 2
    

[![enter image description
here](http://i.stack.imgur.com/uRUQ7.png)](http://i.stack.imgur.com/uRUQ7.png)

Note that I shrunk the circle sizes to make the lines more distinguishable:

    
    
    plt.scatter(x,y,s=1)
    if px!='':
        plt.plot([px,x],[py,y],'-o',markersize=1)
    

For a more accurate control you can actually set the aspect ratio directly:

    
    
    plt.axes().set_aspect(1)
    

[![enter image description
here](http://i.stack.imgur.com/tc2Ui.png)](http://i.stack.imgur.com/tc2Ui.png)

or use some of the predefined modes, e.g.

    
    
    plt.axis('equal')
    plt.tight_layout()
    

[![enter image description
here](http://i.stack.imgur.com/DbLJh.png)](http://i.stack.imgur.com/DbLJh.png)

_Edit:_ for the reference, full code for the final picture:

    
    
    import numpy as np
    import matplotlib
    matplotlib.use('Agg')
    import matplotlib.pyplot as plt
    import sys
    
    from pylab import rcParams
    rcParams['figure.figsize'] = 8, 2
    
    def parsexydata(fdata):
        keys=[]
        xy=[]
        with open(fdata,'r') as f:
            pre=''
            for idx, i in enumerate(f.read().replace('\n','').split(',')[2:]):
                if idx%2==0:
                    pre=i
                    continue
                tmp = pre.split('.')
                if len(tmp)!=3: continue
                [before,key,after] = pre.split('.')
                pre = before+'.'+after
                if key not in keys: keys.append(key)
                xy.append([pre,i,key])
        return [xy,keys]
    
    [xydata, keys] = parsexydata(sys.argv[1])
    
    for idx, k in enumerate(keys):
        px=py=''
        for [x,y,key] in xydata:
            if key!=k: continue
            x=float(x)+float(k)
            if key=='01': print(x)
            plt.scatter(x,y,s=1)
            if px!='':
                plt.plot([px,x],[py,y],'-o',markersize=1)
            px,py=x,y
    
    plt.axis('equal')
    plt.tight_layout()
    plt.savefig('result.png', dpi=200)
    

