0

I have just started using python and tried writing my small program for making simple 2d plots. I am trying to plot 2D matrix (60x60). It all works fine except for the extra element at (x=28,y=60) included in my plot.

I had quite some trouble figuring out what might have gone wrong. Unfortunately due to my modest knowledge of python I didn't really find any meaningful explanation.

The code I am using is

         import matplotlib.pyplot as plt
         import matplotlib as mpl
         import numpy as np
         from subprocess import call

         from math import pi

         import sys

         x = []
         y = []
         z = []

         if len(sys.argv) > 1:
            fname = sys.argv[1]
         else:
            fname = 'data.txt'

         print 'reading data from {}'.format(fname)
         print 'This is the correct verssion trimming the blanks'
         for l in open(fname, 'r'):
            try:
                xx, yy, zz  =  map(float, l.split())
                if zz > 0. :
                    x.append(int(xx))
                    y.append(int(yy))
                    z.append(zz)
            except ValueError:
                pass

         plt.scatter(x, y, c=z ,s=35, marker='s',cmap=mpl.cm.spectral,linewidths=0)
         plt.clim(0,max(z))
         cbar=plt.colorbar()
         cbar.ax.set_visible(False)
         s=18

         plt.xlim([min(x)-3,max(x)+3])
         plt.ylim([min(y)-3,max(y)+3])
         plt.axis('off')

         finalname = fname + '.png'
         pdfname = fname + '.png'

         plt.savefig(pdfname,dpi=100,bbox_inches='tight')

I used it to plot f(x,y)=x*y/(x+y). The plot I got is:

Note that I have an unwanted extra element in my plot indicated by the pointer. I manipulated my data to produce the cut in the upper right part of the plot. The x,y,z values I used can be found here:

http://pastebin.com/snyvGD2q

Perhaps what I am doing is not the best solution. Therefore, I would appreciate any better alternative solutions.

Thank you in advance Alex

1 Answer 1

2

Your problem is derived from your original data. If you check lines around 1773 in your text file, you have:

    29         50 18.4 
    29         51 18.5 
    29         52 18.6 
    29         60 19.6 
    30          1 1.0 
    30          2 1.9 
    30          3 2.7 
    30          4 3.5

The jump from y=52 to y=60 is causing the dot in the figure

Sign up to request clarification or add additional context in comments.

1 Comment

Oh, yes. How did this end up there... Thanks, it is much simpler than I thought. And finally one last question, is setting per hand the square size in my scatter plot necessary. I has to be changed with changing the matrix dimension, which is quite inconvenient when done by simply hard coding it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.