关于matplotlib热图,我有两个问题。
1)是否可以在matplotlib热图上增加X和Y点的重要性?
2)是否可以通过白色绘制没有数据的坐标?
我的Python地图生成文件如下:
import sys
import os
import numpy as np
import pandas as pd
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from scipy import stats
csv_filename = 'heatmap_data.csv'
df = pd.read_csv(csv_filename)
red_df = df[df['type']=='red']
blue_df = df[df['type']=='blue']
def compute_KDE_from_pos(x_pos,y_pos):
xmin = x_pos.min()
xmax = x_pos.max()
ymin = y_pos.min()
ymax = y_pos.max()
X, Y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = np.vstack([X.ravel(), Y.ravel()])
values = np.vstack([x_pos, y_pos])
kernel = stats.gaussian_kde(values)
Z = np.reshape(kernel(positions).T, X.shape)
return Z
Z_red = compute_KDE_from_pos(red_df.x_pos,red_df.y_pos)
Z_blue = compute_KDE_from_pos(blue_df.x_pos,blue_df.y_pos)
Z_diff = Z_blue-Z_red
fig, ax = plt.subplots()
ax.imshow(np.rot90(Z_diff), cmap='RdBu',
extent=[0, 100, 0, 100])
ax.plot(red_df.x_pos, red_df.y_pos, 'r.', markersize=2)
ax.plot(blue_df.x_pos, blue_df.y_pos, 'b.', markersize=2)
ax.set_xlim([0, 100])
ax.set_ylim([100, 0])
ax.axis('equal')
plt.savefig("heatmap.png")
热图数据文件(heatmap_data.csv):
x_pos,y_pos,type,importance
74,64,blue,0.011517617893368
68,64,blue,0.0041303878348102
32,64,red,0.049788810065569
8,64,red,0.12877712212094
88,64,red,0.0050599724578342
84,64,blue,0.00052412736663743
80,64,blue,0.020183850819375
78,64,blue,0.01297132988303
72,64,red,0.080092605800612
64,64,red,0.074683215098353
62,64,blue,0.011168199648943
58,64,red,0.030129086612831
56,64,red,0.085501996502871
54,64,red,0.042576289129223
50,64,red,0.0025580057348979
48,64,red,0.055198200767828
44,64,red,0.049788810065569
38,64,blue,0.0055840998244716
34,64,red,0.092714517439217
30,64,blue,0.025593241521634
28,64,red,0.07107695463018
26,64,red,0.0099452357934559
22,64,blue,0.043624543862498
20,64,blue,0.031002632223893
18,64,red,0.038795319538838
16,64,blue,0.023790111287548
12,64,blue,0.030827923101681
94,64,red,0.044728837607734
90,64,red,0.042750998251436
82,64,blue,0.066890527783408
76,64,blue,0.015123878361541
70,64,blue,0.045427674096584
66,64,blue,0.018206011463076
60,64,red,0.060607591470087
46,64,red,0.01227249339418
14,64,red,0.0048852633356217
92,64,red,0.041122577139562
52,64,blue,0.007387230058558
36,64,blue,0.025418532399422
4,64,red,0.02326598392091
86,64,blue,0.021812271931249
42,64,red,0.0034315513459603
40,64,blue,0.0037809695903852
24,64,blue,0.092134351060618
50,24,blue,0.034552853680104
92,24,red,0.021462853686824
86,24,red,0.039319446905475
82,24,red,0.0088409420482194
74,24,red,0.0088409420482194
66,24,red,0.015878753862352
64,24,blue,0.023790111287548
60,24,red,0.017332465852014
56,24,red,0.049089973576719
54,24,red,0.10661543194527
52,24,red,0.03140808948028
48,24,red,0.051067812933018
46,24,red,0.0081421055593695
44,24,red,0.097425071652626
42,24,blue,0.043624543862498
40,24,red,0.080092605800612
36,24,blue,0.034434183569854
10,24,red,0.044728837607734
34,24,red,0.03553847731509
84,24,red,0.019659723452738
80,24,red,0.010644072282306
58,24,red,0.040423740650712
32,24,blue,0.00017470912221247
30,24,red,0.046357258719608
8,24,red,0.019659723452738
90,24,red,0.021462853686824
70,24,red,0.026697535266871
62,24,blue,0.050662355676631
38,24,blue,0.021812271931249
68,24,blue,0.01640288122899
14,68,red,0.01570404474014
18,68,red,0.058629752113788
20,68,red,0.055023491645615
38,68,red,0.082070445156911
10,68,red,0.033735347081004
24,68,blue,0.0023272576007238
28,68,red,0.044379419363309
36,68,red,0.023091274798698
50,68,red,0.018960886963888
52,68,red,0.057001331001914
56,68,blue,0.0039556787125977
68,68,blue,0.052465485910717
72,68,blue,0.016577590351202
74,68,red,0.062061303459748
76,68,blue,0.038215153160239
78,68,red,0.044379419363309
80,68,red,0.010469363160093
82,68,red,0.010294654037881
84,68,red,0.033560637958791
86,68,red,0.0086662329260069
90,68,blue,0.00017470912221247
66,68,red,0.055198200767828
70,68,blue,0.00034941824442495
8,68,red,0.0034315513459603
12,68,blue,0.0091903602926443
16,68,blue,0.018380720585289
62,68,red,0.035363768192878
26,68,blue,0.039843574272113
32,68,blue,0.021812271931249
34,68,blue,0.010993490526731
40,68,red,0.021288144564612
44,68,blue,0.0055840998244716
46,68,blue,0.010993490526731
48,68,blue,0.0091903602926443
22,68,blue,0.048859225442545
30,68,blue,0.012796620760817
58,68,red,0.0050599724578342
88,68,blue,0.0055840998244716
84,68,red,0.028151247256532
22,68,red,0.14087490639291
90,68,red,0.015878753862352
70,68,red,0.040773158895137
68,68,red,0.0050599724578342
66,68,red,0.030129086612831
64,68,red,0.037166898426964
62,68,red,0.0086662329260069
58,68,red,0.039144737783263
54,68,blue,0.0055840998244716
52,68,red,0.030129086612831
50,68,red,0.077940057322101
48,68,blue,0.012796620760817
44,68,red,0.030129086612831
40,68,red,0.066016982172346
38,68,red,0.06421385193826
34,68,red,0.037341607549176
32,68,red,0.0050599724578342
28,68,red,0.01227249339418
24,68,blue,0.025593241521634
20,68,red,0.049614100943356
18,68,blue,0.013146039005242
14,68,red,0.01570404474014
76,68,red,0.022741856554273
74,68,red,0.056826621879702
72,68,red,0.037166898426964
26,68,blue,0.0075619391807704
94,68,red,0.042925707373648
82,68,red,0.024544986788359
56,68,blue,0.021812271931249
46,68,blue,0.014599750994903
42,68,blue,0.010993490526731
8,68,red,0.096495487029602
80,68,red,0.0032568422237478
30,68,blue,0.021812271931249
16,68,blue,0.029024792867594
10,68,blue,0.0019778393562988
86,68,blue,0.007387230058558
78,68,blue,0.038040444038026
12,68,blue,0.027221662633508
4,68,red,0.02326598392091
80,64,red,0.10335858972152
14,64,red,0.026522826144658
12,64,blue,0.00034941824442495
32,64,blue,0.01297132988303
34,64,red,0.049788810065569
38,64,red,0.028325956378745
44,64,blue,0.0021525484785113
46,64,red,0.0032568422237478
50,64,blue,0.040192992516538
54,64,red,0.0086662329260069
56,64,red,0.010294654037881
58,64,red,0.01227249339418
60,64,red,0.040947868017349
68,64,red,0.013726205383841
70,64,blue,0.011168199648943
74,64,red,0.0048852633356217
78,64,blue,0.057874876612976
82,64,red,0.020938726320187
84,64,red,0.1803690624206
86,64,red,0.049788810065569
88,64,blue,0.0055840998244716
10,64,red,0.0050599724578342
16,64,red,0.060258173225662
18,64,blue,0.014774460117116
26,64,blue,0.036412022926152
30,64,blue,0.041646704506199
36,64,red,0.037341607549176
40,64,blue,0.012796620760817
52,64,blue,0.020009141697163
64,64,blue,0.032631053335767
66,64,red,0.055198200767828
72,64,red,0.012097784271967
76,64,blue,0.041821413628412
20,64,blue,0.038215153160239
22,64,red,0.017332465852014
24,64,blue,0.079512439422013
42,64,red,0.0068631026919206
62,64,blue,0.025593241521634
92,64,red,0.012447202516392
66,32,red,0.089282966093256
50,32,red,0.068400278785031
44,32,red,0.05124252205523
84,32,red,0.026697535266871
82,32,red,0.05537290989004
72,32,red,0.051766649421867
64,32,red,0.0014537119896614
62,32,red,0.03897002866105
60,32,blue,0.007387230058558
48,32,red,0.051417231177442
46,32,red,0.033560637958791
40,32,blue,0.00034941824442495
38,32,blue,0.0021525484785113
36,32,red,0.03553847731509
32,32,blue,0.0039556787125977
30,32,red,0.026697535266871
28,32,blue,0.007387230058558
22,32,red,0.030303795735043
94,32,red,0.042925707373648
76,32,red,0.037516316671389
74,32,red,0.010644072282306
68,32,red,0.0050599724578342
58,32,red,0.0032568422237478
56,32,red,0.046007840475183
52,32,blue,0.021986981053461
34,32,blue,0.01640288122899
24,32,red,0.026697535266871
20,32,red,0.0016284211118739
12,32,red,0.0034315513459603
8,32,red,0.033910056203216
80,32,red,0.010644072282306
54,32,blue,0.021986981053461
42,32,blue,0.023615402165335
18,32,blue,0.0055840998244716
16,32,red,0.0052346815800466
4,32,red,0.017856593218651
10,32,red,0.0034315513459603
46,24,red,0.056477203635277
48,24,red,0.054848782523403
50,24,red,0.12720474002103
54,24,red,0.013551496261629
26,24,red,0.012447202516392
32,24,red,0.042750998251436
36,24,red,0.06421385193826
38,24,blue,0.032631053335767
40,24,red,0.024719695910572
52,24,red,0.14610958797296
56,24,blue,0.00087354561106237
60,24,red,0.01570404474014
64,24,blue,0.027221662633508
74,24,red,0.007037811814133
78,24,red,0.0052346815800466
16,24,red,0.016053462984565
44,24,blue,0.013146039005242
58,24,blue,0.068693658017495
62,24,blue,0.03623731380394
22,24,red,0.0052346815800466
28,24,red,0.010644072282306
30,24,red,0.017681884096439
42,24,red,0.0048852633356217
70,28,red,0.042750998251436
16,28,red,0.014250332750479
20,28,red,0.05537290989004
22,28,red,0.0034315513459603
34,28,red,0.040947868017349
40,28,red,0.073054793986479
42,28,blue,0.0095397785370693
50,28,red,0.026928283401045
56,28,red,0.018960886963888
58,28,red,0.085501996502871
60,28,red,0.0032568422237478
62,28,blue,0.030827923101681
64,28,red,0.0032568422237478
76,28,red,0.040947868017349
14,28,red,0.012447202516392
46,28,red,0.012097784271967
48,28,blue,0.057874876612976
52,28,red,0.0191355960861
54,28,blue,0.031002632223893
98,28,red,0.02326598392091
8,28,red,0.014250332750479
18,28,red,0.007037811814133
28,28,red,0.015878753862352
30,28,red,0.0068631026919206
32,28,blue,0.014599750994903
38,28,blue,0.023615402165335
82,28,red,0.012447202516392
90,28,red,0.019659723452738
36,28,blue,0.027221662633508
44,28,blue,0.054268616144804
68,28,blue,0.01640288122899
78,28,red,0.019659723452738
26,44,red,0.033735347081004
6,44,red,0.012447202516392
16,44,red,0.033735347081004
20,44,blue,0.0037809695903852
34,44,red,0.042750998251436
36,44,red,0.040773158895137
44,44,red,0.010469363160093
48,44,red,0.0032568422237478
50,44,blue,0.086899669480571
54,44,blue,0.032631053335767
58,44,red,0.060607591470087
66,44,red,0.014075623628266
68,44,blue,0.0091903602926443
76,44,red,0.058804461236
80,44,red,0.024894405032784
86,44,red,0.051766649421867
92,44,red,0.042925707373648
18,44,blue,0.010993490526731
40,44,blue,0.00017470912221247
42,44,red,0.083873575390997
74,44,blue,0.0057588089466841
88,44,red,0.030303795735043
24,44,blue,0.012796620760817
30,44,blue,0.023615402165335
60,44,red,0.015878753862352
78,44,blue,0.007387230058558
46,44,blue,0.0037809695903852
52,44,blue,0.012796620760817
56,44,blue,0.029024792867594
64,44,red,0.0052346815800466
12,60,red,0.024894405032784
14,60,blue,0.0019778393562988
16,60,red,0.019485014330525
20,60,red,0.0068631026919206
22,60,red,0.026522826144658
26,60,blue,0.020009141697163
34,60,red,0.0050599724578342
36,60,red,0.053569779655954
40,60,red,0.048160388953695
44,60,blue,0.010993490526731
50,60,blue,0.0077366483029829
58,60,red,0.067820112406432
62,60,red,0.028500665500957
64,60,red,0.051766649421867
68,60,red,0.01227249339418
76,60,red,0.0084915238037944
78,60,blue,0.010993490526731
86,60,red,0.037341607549176
84,60,red,0.010469363160093
10,60,red,0.016053462984565
24,60,blue,0.020009141697163
28,60,blue,0.0039556787125977
48,60,blue,0.0055840998244716
66,60,blue,0.00017470912221247
70,60,red,0.01227249339418
90,60,red,0.0016284211118739
30,60,blue,0.020009141697163
74,60,blue,0.0021525484785113
72,60,blue,0.025418532399422
88,52,red,0.033910056203216
86,52,red,0.026697535266871
84,52,red,0.039144737783263
82,52,red,0.019485014330525
80,52,blue,0.0091903602926443
76,52,red,0.0068631026919206
68,52,red,0.042750998251436
66,52,red,0.033735347081004
64,52,red,0.042750998251436
62,52,red,0.037341607549176
56,52,red,0.015878753862352
54,52,blue,0.025418532399422
48,52,red,0.0032568422237478
46,52,red,0.051591940299655
40,52,red,0.0032568422237478
38,52,red,0.047985679831482
34,52,red,0.017681884096439
32,52,red,0.03897002866105
22,52,blue,0.007387230058558
16,52,blue,0.0055840998244716
78,52,red,0.0086662329260069
60,52,red,0.01227249339418
52,52,red,0.033735347081004
44,52,blue,0.007387230058558
42,52,blue,0.023615402165335
36,52,red,0.01227249339418
24,52,blue,0.020009141697163
18,52,blue,0.018206011463076
8,52,red,0.039319446905475
6,52,red,0.019659723452738
74,52,blue,0.018206011463076
72,52,red,0.0014537119896614
70,52,red,0.0050599724578342
50,52,blue,0.059678006847063
12,52,red,0.010469363160093
58,52,red,0.017681884096439
26,52,blue,0.007387230058558
50,28,red,0.0029634629912845
92,28,red,0.02326598392091
78,28,red,0.0088409420482194
76,28,red,0.030303795735043
70,28,red,0.023091274798698
66,28,red,0.030129086612831
58,28,red,0.040598449772924
56,28,red,0.03140808948028
54,28,blue,0.029199501989807
52,28,red,0.07450850597614
44,28,blue,0.04920864368697
42,28,blue,0.0057588089466841
40,28,red,0.026522826144658
38,28,blue,0.03280576245798
34,28,red,0.040947868017349
22,28,red,0.0088409420482194
86,28,red,0.012447202516392
72,28,blue,0.0019778393562988
64,28,blue,0.018380720585289
48,28,blue,0.02396482040976
46,28,blue,0.047230804330671
30,28,blue,0.007387230058558
18,28,red,0.0034315513459603
6,28,red,0.017856593218651
90,28,red,0.039319446905475
62,28,red,0.013900914506054
26,28,red,0.0016284211118739
74,28,red,0.012447202516392
12,28,red,0.0088409420482194
20,48,red,0.017681884096439
18,48,red,0.01227249339418
26,48,blue,0.0091903602926443
36,48,red,0.031932216846917
38,48,red,0.049788810065569
44,48,red,0.089282966093256
46,48,blue,0.0021525484785113
48,48,blue,0.0021525484785113
50,48,red,0.036642771060327
52,48,red,0.037341607549176
56,48,red,0.062236012581961
74,48,red,0.0068631026919206
82,48,red,0.017507174974226
84,48,blue,0.0055840998244716
8,48,red,0.044728837607734
12,48,red,0.024894405032784
14,48,red,0.053395070533741
22,48,red,0.024719695910572
24,48,red,0.024719695910572
28,48,blue,0.021812271931249
34,48,red,0.014075623628266
70,48,red,0.0068631026919206
76,48,blue,0.012796620760817
78,48,red,0.01227249339418
58,48,blue,0.00034941824442495
64,48,red,0.01227249339418
66,48,blue,0.0055840998244716
72,48,blue,0.007387230058558
86,48,red,0.0052346815800466
32,48,blue,0.023615402165335
66,56,red,0.021113435442399
26,56,red,0.10876798042378
88,56,red,0.03210692596913
84,56,blue,0.00017470912221247
82,56,red,0.031932216846917
80,56,blue,0.0037809695903852
76,56,red,0.057001331001914
70,56,red,0.019485014330525
64,56,red,0.01227249339418
50,56,red,0.049439391821144
38,56,blue,0.0091903602926443
36,56,red,0.060607591470087
30,56,red,0.039144737783263
20,56,red,0.030129086612831
18,56,blue,0.023615402165335
14,56,red,0.037341607549176
12,56,red,0.033910056203216
10,56,red,0.046357258719608
58,56,blue,0.00017470912221247
52,56,blue,0.007387230058558
48,56,blue,0.012796620760817
40,56,red,0.046357258719608
22,56,red,0.03553847731509
6,56,red,0.016053462984565
90,56,red,0.026697535266871
72,56,blue,0.00017470912221247
60,56,red,0.019485014330525
54,56,red,0.0016284211118739
42,56,red,0.01227249339418
34,56,red,0.040947868017349
32,56,blue,0.034434183569854
16,56,red,0.0014537119896614
68,56,red,0.019485014330525
24,56,blue,0.039843574272113
70,36,red,0.06421385193826
24,36,red,0.0032568422237478
92,36,red,0.017856593218651
90,36,red,0.03210692596913
84,36,red,0.012447202516392
82,36,red,0.0052346815800466
76,36,blue,0.0037809695903852
72,36,red,0.0016284211118739
66,36,red,0.030129086612831
50,36,red,0.040074322406287
46,36,red,0.029954377490618
40,36,red,0.037341607549176
38,36,red,0.053395070533741
36,36,red,0.033735347081004
30,36,red,0.03897002866105
88,36,red,0.017856593218651
86,36,blue,0.0055840998244716
74,36,blue,0.023615402165335
68,36,blue,0.03623731380394
60,36,blue,0.027221662633508
58,36,blue,0.010993490526731
54,36,blue,0.025593241521634
48,36,blue,0.029199501989807
44,36,red,0.040598449772924
42,36,blue,0.0021525484785113
34,36,blue,0.020009141697163
28,36,red,0.044554128485522
52,36,red,0.042401580007011
56,36,blue,0.01297132988303
32,36,blue,0.032631053335767
12,36,red,0.058979170358213
64,36,blue,0.0091903602926443
62,36,blue,0.01640288122899
22,36,blue,0.01640288122899
20,36,blue,0.00017470912221247
6,36,red,0.017856593218651
68,72,red,0.039319446905475
30,72,red,0.042925707373648
86,72,red,0.039319446905475
76,72,red,0.035713186437303
72,72,red,0.016053462984565
60,72,red,0.017856593218651
56,72,red,0.014250332750479
36,72,red,0.044728837607734
26,72,red,0.041122577139562
24,72,red,0.010644072282306
22,72,red,0.035713186437303
20,72,red,0.042925707373648
18,72,red,0.060782300592299
12,72,red,0.017856593218651
10,72,red,0.014250332750479
6,72,red,0.046531967841821
70,72,red,0.017856593218651
50,72,red,0.007037811814133
42,72,red,0.019659723452738
34,72,red,0.019659723452738
14,72,red,0.016053462984565
90,72,red,0.019659723452738
44,72,red,0.017856593218651
48,72,red,0.017856593218651
86,60,red,0.01227249339418
90,60,red,0.014250332750479
88,60,blue,0.0037809695903852
82,60,blue,0.0039556787125977
74,60,blue,0.025418532399422
70,60,red,0.0191355960861
68,60,red,0.051591940299655
64,60,red,0.019310305208313
56,60,red,0.062410721704173
48,60,red,0.067820112406432
34,60,red,0.0050599724578342
30,60,red,0.03553847731509
28,60,red,0.019310305208313
22,60,blue,0.032631053335767
18,60,red,0.026522826144658
10,60,red,0.023091274798698
94,60,red,0.014250332750479
78,60,red,0.019485014330525
72,60,blue,0.0055840998244716
60,60,red,0.017681884096439
50,60,blue,0.067065236905621
40,60,red,0.096495487029602
38,60,blue,0.0091903602926443
36,60,blue,0.020009141697163
32,60,blue,0.014599750994903
26,60,blue,0.021986981053461
8,60,red,0.041122577139562
66,60,red,0.026522826144658
16,60,red,0.024719695910572
58,60,red,0.023091274798698
52,60,red,0.0016284211118739
46,60,red,0.024894405032784
42,60,blue,0.0055840998244716
14,60,red,0.051766649421867
54,60,red,0.010469363160093
24,60,blue,0.030827923101681
44,48,red,0.089108256971044
94,48,red,0.019659723452738
90,48,red,0.0034315513459603
84,48,red,0.0052346815800466
72,48,red,0.0032568422237478
68,48,red,0.0086662329260069
62,48,blue,0.038040444038026
60,48,red,0.096320777907389
58,48,red,0.058804461236
54,48,blue,0.018206011463076
52,48,red,0.053395070533741
50,48,blue,0.0062829363133215
48,48,red,0.010469363160093
34,48,red,0.026522826144658
32,48,blue,0.029024792867594
16,48,red,0.071251663752393
14,48,blue,0.00017470912221247
6,48,red,0.039319446905475
4,48,red,0.019659723452738
86,48,blue,0.0055840998244716
70,48,blue,0.020009141697163
64,48,blue,0.0075619391807704
42,48,blue,0.023615402165335
40,48,red,0.0050599724578342
30,48,red,0.033735347081004
22,48,red,0.0088409420482194
12,48,red,0.021288144564612
10,48,blue,0.007387230058558
92,48,red,0.010644072282306
76,48,red,0.0068631026919206
66,48,red,0.033735347081004
56,48,blue,0.0091903602926443
24,48,blue,0.007387230058558
78,48,red,0.019485014330525
8,32,red,0.007037811814133
20,32,red,0.0016284211118739
28,32,blue,0.0019778393562988
30,32,red,0.0068631026919206
32,32,red,0.0086662329260069
34,32,red,0.042750998251436
36,32,red,0.030129086612831
40,32,red,0.053395070533741
42,32,red,0.024719695910572
48,32,blue,0.03623731380394
50,32,red,0.0076179781927321
52,32,red,0.053395070533741
56,32,red,0.040773158895137
58,32,blue,0.018206011463076
60,32,red,0.037166898426964
68,32,red,0.031932216846917
70,32,red,0.040947868017349
76,32,red,0.0052346815800466
84,32,red,0.026697535266871
14,32,red,0.0088409420482194
22,32,red,0.010644072282306
24,32,blue,0.0019778393562988
38,32,red,0.017681884096439
44,32,red,0.076661054454652
62,32,blue,0.012796620760817
64,32,red,0.030129086612831
66,32,blue,0.012796620760817
80,32,red,0.037516316671389
6,32,red,0.010644072282306
72,32,red,0.007037811814133
88,32,red,0.039319446905475
74,32,blue,0.010993490526731
40,52,red,0.028325956378745
58,52,red,0.046182549597396
10,52,red,0.0052346815800466
16,52,red,0.046357258719608
22,52,red,0.069623242640519
24,52,blue,0.010993490526731
28,52,red,0.040947868017349
30,52,red,0.0048852633356217
36,52,red,0.055198200767828
38,52,red,0.094517647673303
52,52,red,0.03553847731509
54,52,red,0.0086662329260069
68,52,red,0.0050599724578342
82,52,blue,0.010993490526731
84,52,red,0.033735347081004
90,52,red,0.0034315513459603
14,52,red,0.017681884096439
18,52,blue,0.00017470912221247
20,52,blue,0.0091903602926443
34,52,red,0.021288144564612
44,52,red,0.033735347081004
56,52,red,0.0032568422237478
62,52,red,0.014075623628266
72,52,red,0.024894405032784
76,52,red,0.026522826144658
96,52,red,0.019659723452738
46,52,blue,0.021812271931249
64,52,blue,0.020009141697163
74,52,red,0.019485014330525
80,52,red,0.024894405032784
92,52,red,0.014250332750479
50,52,blue,0.07247462760788
70,52,blue,0.007387230058558
58,40,red,0.012097784271967
80,40,red,0.021288144564612
74,40,blue,0.0037809695903852
70,40,red,0.024719695910572
68,40,red,0.031757507724705
62,40,red,0.031932216846917
52,40,red,0.0050599724578342
48,40,blue,0.0057588089466841
44,40,red,0.049788810065569
40,40,red,0.066016982172346
34,40,red,0.026522826144658
32,40,red,0.033735347081004
28,40,blue,0.0055840998244716
24,40,red,0.078638893810951
16,40,blue,0.007387230058558
10,40,red,0.042750998251436
50,40,blue,0.0028513849673612
90,40,red,0.0088409420482194
78,40,red,0.075032633342778
66,40,blue,0.027221662633508
54,40,red,0.076661054454652
42,40,blue,0.0091903602926443
30,40,blue,0.018206011463076
22,40,red,0.014075623628266
56,40,blue,0.021812271931249
46,40,blue,0.00034941824442495
38,40,red,0.014075623628266
86,40,red,0.014250332750479
64,40,blue,0.038040444038026
26,40,blue,0.00034941824442495
12,40,blue,0.00017470912221247
4,40,red,0.021462853686824
62,40,red,0.026522826144658
12,40,red,0.0052346815800466
24,40,red,0.030129086612831
30,40,blue,0.021812271931249
38,40,blue,0.014599750994903
44,40,red,0.051591940299655
52,40,red,0.0086662329260069
54,40,red,0.047985679831482
58,40,red,0.03553847731509
64,40,red,0.014075623628266
70,40,red,0.024719695910572
76,40,red,0.0032568422237478
88,40,red,0.021288144564612
90,40,red,0.014250332750479
92,40,red,0.016053462984565
16,40,red,0.0052346815800466
26,40,blue,0.020009141697163
32,40,red,0.0050599724578342
34,40,blue,0.01640288122899
46,40,blue,0.0057588089466841
48,40,blue,0.0021525484785113
50,40,blue,0.041996122750624
56,40,red,0.01227249339418
72,40,blue,0.0091903602926443
10,40,red,0.017856593218651
20,40,red,0.053569779655954
36,40,blue,0.007387230058558
40,40,red,0.0032568422237478
42,40,red,0.028325956378745
84,40,blue,0.0019778393562988
50,20,red,0.056826621879702
42,20,red,0.062061303459748
56,20,red,0.029604959246194
58,20,red,0.11941205270609
52,20,blue,0.0021525484785113
46,20,red,0.10155545948744
40,20,blue,0.00052412736663743
16,20,red,0.016053462984565
8,20,red,0.019659723452738
60,20,red,0.060258173225662
66,20,red,0.0016284211118739
64,20,red,0.028325956378745
54,20,blue,0.052465485910717
44,20,blue,0.043624543862498
38,20,red,0.014075623628266
32,20,red,0.0016284211118739
30,20,red,0.014250332750479
12,20,red,0.021462853686824
76,20,red,0.016053462984565
36,20,blue,0.012796620760817
56,20,red,0.058280333869363
44,20,red,0.12121518294017
58,20,red,0.022567147432061
32,20,red,0.024894405032784
42,20,red,0.047985679831482
46,20,red,0.0032568422237478
50,20,red,0.033735347081004
60,20,red,0.035363768192878
68,20,red,0.0016284211118739
28,20,red,0.012447202516392
34,20,red,0.046357258719608
40,20,blue,0.041646704506199
62,20,red,0.01227249339418
36,20,blue,0.012796620760817
54,20,blue,0.038040444038026
26,20,red,0.014250332750479
88,20,red,0.021462853686824
44,36,blue,0.0057588089466841
50,36,blue,0.0010482547332749
4,36,red,0.021462853686824
14,36,red,0.0088409420482194
22,36,red,0.028500665500957
24,36,blue,0.0091903602926443
26,36,blue,0.0091903602926443
28,36,blue,0.012796620760817
36,36,blue,0.020009141697163
46,36,red,0.0050599724578342
52,36,red,0.017507174974226
56,36,red,0.019310305208313
62,36,blue,0.012796620760817
68,36,blue,0.0093650694148568
18,36,red,0.046357258719608
66,36,red,0.017507174974226
6,36,red,0.041122577139562
8,36,red,0.030303795735043
16,36,red,0.023091274798698
32,36,blue,0.00017470912221247
34,36,red,0.0052346815800466
42,36,red,0.0032568422237478
60,36,blue,0.007387230058558
70,36,blue,0.0021525484785113
74,36,red,0.017681884096439
76,36,blue,0.0055840998244716
84,36,red,0.014250332750479
20,36,red,0.03553847731509
12,36,red,0.0088409420482194
48,36,red,0.044379419363309
54,36,blue,0.029024792867594
58,36,blue,0.020009141697163
80,36,blue,0.010993490526731
82,36,red,0.0016284211118739
86,36,red,0.0052346815800466
10,36,red,0.012447202516392
图的限制为X(0 .. 100)和Y(0 .. 100)。
现在它生成了这个热图,但是从逻辑上讲是错误的。 heatmap example
热图应显示冰球场的正负部分。每个数据都是在特定坐标下的发射率。
结果应类似于以下示例。红点应该是红色,强调点的重要性,蓝点应该是蓝色,强调点的重要性(在CSV文件中)。没有数据的点应该是透明的(白色)。 example of result heatmap
答案 0 :(得分:0)
在您要说明的问题中:
没有数据的点应该是透明的(白色)
在您附加的示例热图中的白色区域中可能包含点:实际上,对于两个点分布(红色和蓝色),它们具有相同的确切概率。他们。在示例热图所用的颜色图中,白色表示以下事实:两个Gaussian KDE之差非常接近于零。研究KDE是什么以及它代表什么很重要:它只是一种估计随机变量的概率密度函数的方法,仅此而已。
无论如何,请尝试将cmap='RdBu'
替换为cmap='bwr'
,并将Z_diff = Z_blue-Z_red
替换为Z_diff = -Z_blue+Z_red
,看看是否喜欢该结果。 Here是我所得到的。
就考虑到每一点的“重要性”而言,我认为该问题的一部分没有明确定义。在用于ML或统计数据的整洁数据集中,理想情况下,每一行应对应一个且只有一个观测值。如果列importance
对应于具有一对坐标的点的相对出现频率,则建议计算该点的绝对频率,并将数据集中的每个点重复其绝对频率。这样,您的数据集将变得整洁干净。
尽管即使不知道所收集观测值的总数N,也可以根据相对频率估算绝对频率,但是如果您知道该数字N,则您自己做起来会容易得多。
>编辑:
我重新阅读了您对importance
的评论。您想根据“重要性”列中包含的值更改每个点的标记大小,对吗?
此代码可以做到。我将importance
中的值乘以200,因为结果的比例合理,但是您可以根据需要更改它。这是代码的输出:
还:整个代码中根本没有使用os
,sys
,matplotlib
(不仅是pyplot)。不要导入它们。除此之外,我刚刚将.plot
的调用从Axes对象更改为.scatter
,并将列red_df.importance
传递给属性s=
,该属性定义了对象的大小。给定点的标记。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats
csv_filename = 'heatmap_data.csv'
df = pd.read_csv(csv_filename)
red_df = df[df['type']=='red']
blue_df = df[df['type']=='blue']
def compute_KDE_from_pos(x_pos,y_pos):
xmin = x_pos.min()
xmax = x_pos.max()
ymin = y_pos.min()
ymax = y_pos.max()
X, Y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = np.vstack([X.ravel(), Y.ravel()])
values = np.vstack([x_pos, y_pos])
kernel = stats.gaussian_kde(values)
Z = np.reshape(kernel(positions).T, X.shape)
return Z
Z_red = compute_KDE_from_pos(red_df.x_pos,red_df.y_pos)
Z_blue = compute_KDE_from_pos(blue_df.x_pos,blue_df.y_pos)
Z_diff = -Z_blue+Z_red
fig, ax = plt.subplots()
ax.imshow(np.rot90(Z_diff), cmap='bwr',
extent=[0, 100, 0, 100])
ax.scatter(red_df.x_pos, red_df.y_pos, c='r', s=red_df.importance.values*200)
ax.scatter(blue_df.x_pos, blue_df.y_pos, c='b', s=blue_df.importance.values*200)
ax.set_xlim([0, 100])
ax.set_ylim([100, 0])
ax.axis('equal')
plt.show()