0

I'd like to make a plot where each point it has its x&y value and it also has a third value expressing the color density at that point. Applying my python code in mathematica I am able to do it using the following code, but now I want to do it only using python(preferably using matlibplot).

def printMath2DTableMethod():
    print('{', end="")
    for i in range(0, lines, 1):
        print('{', end="")
        for j in range(0, columns, 1):
            f = int(columns * rearrange_.rearrangeMethod(i) + rearrange_.rearrangeMethod(j))
            print('%d' % size[f], end = '')
            if (j < columns - 1):
                print(',', end='')
        if (i < lines - 1):
            print('},')
        else:
            print('}}')

The plotting should look something similar to the images of these two questions

How can I make a scatter plot colored by density in matplotlib?

How to plot a density map in python?

it should have a colorbar at the side and the points with the biggest density should be on the top of the other points(if they overlap).

The data that this method produces I append it to some file and it looks like:

1,2,4,5,6,2,6 x256 columns in total

3,2,4,5,1,6,4

4,2,5,6,1,7,5

x256 rows in total

The plotting can be made by using the code directly or by reading the data from the file, but what I don't know is how to assign values to x(which is the i at the 1st for loop at the code above), to y(which is the j at the 2nd for loop at the code above) and especially to the 3rd argument, the one which will show the color density(which is the size[f] at the code above) since it is depended on i and j of the for loops.

I have been trying to research and solve it myself all these days, but not much success, so any help would be highly appreciated. Thanks in advance :)

Ivan Kolesnikov
  • 1,787
  • 1
  • 29
  • 45
Green Green
  • 43
  • 1
  • 8
  • So what is your question exactly? How to plot the three-column array with x, y and color? Or how to process your data to prepare it for plotting? In any way - some sample data will help just to understand what you are dealing with. If it is just three columns then one of the links you posted has the answer. – Oleg Medvedyev Jul 31 '17 at 01:43
  • are you looking for [something like this?](https://matplotlib.org/examples/shapes_and_collections/scatter_demo.html) – grg rsr Jul 31 '17 at 07:27
  • @omdv my question is 1.how to change my code to assign values to x,y and color(as explained at the paragraph above) or 2.or how to read and process my data so that the number of rows to be the value for x-axis, the number of columns to be the value for y-axis, and the values presented by the list that I gave to be the values for color argument. The data is similar to the one that I gave so, only integers separated with each other with a semicolon, every row has 256 values in total and there are 256 columns in total as well. – Green Green Jul 31 '17 at 13:04
  • @grg rsr, I am looking for something similar to the images that I presented to the 2 links above – Green Green Jul 31 '17 at 13:05

1 Answers1

0

Here are examples for both plots you linked

import matplotlib.pyplot as plt
import scipy as sp

# scatterplot as link 1
Data = sp.randn(1000,3)
plt.scatter(Data[:,0],Data[:,1],c=Data[:,2],cmap='magma')
plt.colorbar()

enter image description here

# density matrix as link 2
Nbins = 50
M = sp.zeros((Nbins+1,Nbins+1))
xinds = sp.digitize(Data[:,0],sp.linspace(-3,3,Nbins)) # chose limits accordingly
yinds = sp.digitize(Data[:,1],sp.linspace(-3,3,Nbins))


# to account for the highest density drawn over the others
sort_inds = sp.argsort(Data[:,2])[::-1]
Data = Data[sort_inds,:]
xinds = xinds[sort_inds]
yinds = yinds[sort_inds]

for i in range(Data.shape[0]):
    M[xinds[i],yinds[i]] = Data[i,2]

plt.matshow(M,cmap='magma',
            extent=(Data[:,0].min(),Data[:,0].max(),Data[:,1].max(),Data[:,1].min()),
            aspect='equal')
plt.colorbar()

enter image description here

grg rsr
  • 558
  • 5
  • 13
  • At your 1st scatter plot you create some random data, could you try using my own data, or could you tell me how to assign the x, y and the color argument according to my own code that I have presented. For ex: x={something}, y={something}, colorParameter={something which includes size[f]}. Again, as given in the question the x should be the i in the 1st for loop of my code; the y should be the j in the 2nd for loop of my code, and colorParameter should be size[f], but size[f] itself is depended by i and j as you can see at the code. – Green Green Jul 31 '17 at 22:41
  • @GreenGreen what your code does quite obscure to me. It used some global variables and methods not defined, so it's impossible to know. It for sure has nothing to do with your question. Please present a clear question with some data. – grg rsr Aug 01 '17 at 07:37