简体   繁体   English

如何在 python 3 中以颜色密度作为第三个参数制作二维图

[英]How to make a 2D plot with color density as the 3rd argument in python 3

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.我想绘制一个图,其中每个点都有其 x 和 y 值,并且还有第三个值表示该点的颜色密度。 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).在 mathematica 中应用我的 python 代码我可以使用以下代码来完成,但现在我只想使用 python(最好使用 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? 如何在 matplotlib 中制作按密度着色的散点图?

How to plot a density map in python? 如何在 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总共 1,2,4,5,6,2,6 x256 列

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

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

x256 rows in total总共 x256 行

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.可以通过直接使用代码或从文件中读取数据来进行绘图,但我不知道如何将值赋给 x(这是上面代码中第一个 for 循环中的i ),以y(这是上面代码中第二个 for 循环中的j ),尤其是第三个参数,它将显示颜色密度(即上面代码中的大小[f] ),因为它取决于i和 for 循环的j

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:)提前致谢:)

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()

在此处输入图像描述

# 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()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM