Plot two graphs on top of each other - python

I'm using matplotlib and I wish to plot a contourf on top of a pcolormesh, displaying both at the same time as described. Here is the code:
"""space for module inclusion"""
fig, ax = plt.subplots()
ax.pcolormesh(*arguments here*)
ax.contour(*arguments here*)
plt.show()
However, when I try to display the data, only contourf is shown (overrides pcolormesh). Any Ideas on this one?
EDIT_1:
So basically the values I'm using are the following:
X:
[ 63.37480164 63.37519836 63.37559891 63.37599945 63.37639999
63.37680054 63.37720108 63.37760162 63.37799835 63.3783989
63.37879944 63.37919998 63.37960052 63.38000107 63.38040161
63.38079834 63.38119888 63.38159943 63.38199997 63.38240051
63.38280106 63.3832016 63.38359833 63.38399887 63.38439941
63.38479996 63.3852005 63.38560104 63.38600159 63.38639832
63.38679886 63.3871994 63.38759995 63.38800049 63.38840103
63.38880157 63.3891983 63.38959885 63.38999939 63.39039993
63.39080048 63.39120102 63.39160156 63.39199829 63.39239883
63.39279938 63.39319992 63.39360046 63.39400101 63.39440155
63.39479828 63.39519882 63.39559937 63.39599991 63.39640045
63.39680099 63.39720154 63.39759827 63.39799881 63.39839935
63.3987999 63.39920044 63.39960098 63.40000153 63.40039825
63.4007988 63.40119934 63.40159988 63.40200043 63.40240097
63.40280151 63.40319824 63.40359879 63.40399933 63.40439987
63.40480042 63.40520096 63.4056015 63.40599823 63.40639877
63.40679932 63.40719986 63.4076004 63.40800095 63.40840149
63.40879822 63.40919876 63.4095993 63.40999985 63.41040039
63.41080093 63.41120148 63.41159821 63.41199875 63.41239929
63.41279984 63.41320038 63.41360092 63.41400146 63.41439819
63.41479874 63.41519928 63.41559982 63.41600037 63.41640091
63.41680145 63.41719818 63.41759872 63.41799927 63.41839981
63.41880035 63.4192009 63.41960144 63.41999817 63.42039871
63.42079926 63.4211998 63.42160034 63.42200089 63.42240143
63.42279816 63.4231987 63.42359924 63.42399979 63.42440033
63.42480087 63.42520142 63.42559814 63.42599869 63.42639923
63.42679977 63.42720032 63.42760086 63.4280014 63.42839813
63.42879868 63.42919922 63.42959976 63.43000031 63.43040085
63.43080139 63.43119812 63.43159866 63.43199921 63.43239975
63.43280029 63.43320084 63.43360138 63.43399811 63.43439865
63.43479919 63.43519974 63.43560028 63.43600082 63.43640137
63.4367981 63.43719864 63.43759918 63.43799973 63.43840027
63.43880081 63.43920135 63.4396019 63.43999863 63.44039917
63.44079971 63.44120026 63.4416008 63.44200134 63.44240189
63.44279861 63.44319916 63.4435997 63.44400024 63.44440079
63.44480133 63.44520187 63.4455986 63.44599915 63.44639969
63.44680023 63.44720078 63.44760132 63.44800186 63.44839859
63.44879913 63.44919968 63.44960022 63.45000076 63.45040131
63.45080185 63.45119858 63.45159912 63.45199966 63.45240021
63.45280075 63.45320129 63.45360184 63.45399857 63.45439911
63.45479965 63.4552002 63.45560074 63.45600128 63.45640182
63.45679855 63.4571991 63.45759964 63.45800018 63.45840073
63.45880127 63.45920181 63.45959854 63.45999908 63.46039963
63.46080017 63.46120071 63.46160126 63.4620018 63.46239853
63.46279907 63.46319962 63.46360016 63.4640007 63.46440125
63.46480179 63.46519852 63.46559906 63.4659996 63.46640015
63.46680069 63.46720123 63.46760178 63.4679985 63.46839905
63.46879959 63.46920013 63.46960068 63.47000122 63.47040176
63.47079849 63.47119904 63.47159958 63.47200012 63.47240067
63.47280121 63.47320175 63.47359848 63.47399902 63.47439957
63.47480011 63.47520065 63.4756012 63.47600174 63.47639847
63.47679901 63.47719955 63.4776001 63.47800064 63.47840118
63.47880173 63.47919846 63.479599 63.47999954 63.48040009
63.48080063 63.48120117 63.48160172 63.48199844 63.48239899
63.48279953 63.48320007 63.48360062 63.48400116 63.4844017
63.48479843 63.48519897 63.48559952 63.48600006 63.4864006
63.48680115 63.48720169 63.48759842 63.48799896 63.48839951
63.48880005 63.48920059 63.48960114 63.49000168 63.49039841
63.49079895 63.49119949 63.49160004 63.49200058 63.49240112
63.49280167 63.49319839 63.49359894 63.49399948 63.49440002
63.49480057 63.49520111 63.49560165 63.49599838 63.49639893
63.49679947 63.49720001 63.49760056 63.4980011 63.49840164
63.49879837 63.49919891 63.49959946 63.5 63.50040054
63.50080109 63.50120163 63.50159836 63.5019989 63.50239944
63.50279999 63.50320053 63.50360107 63.50400162 63.50439835
63.50479889 63.50519943 63.50559998 63.50600052 63.50640106
63.50680161 63.50719833 63.50759888 63.50799942 63.50839996
63.50880051 63.50920105 63.50960159 63.50999832 63.51039886
63.51079941 63.51119995 63.51160049 63.51200104 63.51240158
63.51279831 63.51319885 63.5135994 63.51399994 63.51440048
63.51480103 63.51520157 63.5155983 63.51599884 63.51639938
63.51679993 63.51720047 63.51760101 63.51800156 63.51839828
63.51879883 63.51919937 63.51959991 63.52000046 63.520401
63.52080154 63.52119827 63.52159882 63.52199936 63.5223999
63.52280045 63.52320099 63.52360153 63.52399826 63.5243988
63.52479935 63.52519989 63.52560043 63.52600098 63.52640152
63.52679825 63.52719879 63.52759933 63.52799988 63.52840042
63.52880096 63.52920151 63.52959824 63.52999878 63.53039932
63.53079987 63.53120041 63.53160095 63.5320015 63.53239822
63.53279877 63.53319931 63.53359985 63.5340004 63.53440094
63.53480148 63.53519821 63.53559875 63.5359993 63.53639984
63.53680038 63.53720093 63.53760147 63.5379982 63.53839874
63.53879929 63.53919983 63.53960037 63.54000092 63.54040146
63.54079819 63.54119873 63.54159927 63.54199982 63.54240036
63.5428009 63.54320145 63.54359818 63.54399872 63.54439926
63.5447998 63.54520035 63.54560089 63.54600143 63.54639816
63.54679871 63.54719925 63.54759979 63.54800034 63.54840088
63.54880142 63.54919815 63.54959869 63.54999924 63.55039978
63.55080032 63.55120087 63.55160141 63.55199814 63.55239868
63.55279922 63.55319977 63.55360031 63.55400085 63.5544014
63.55479813 63.55519867 63.55559921 63.55599976 63.5564003
63.55680084 63.55720139 63.55759811 63.55799866 63.5583992
63.55879974 63.55920029 63.55960083 63.56000137 63.5603981
63.56079865 63.56119919 63.56159973 63.56200027 63.56240082
63.56280136 63.5632019 63.56359863 63.56399918 63.56439972
63.56480026 63.56520081 63.56560135 63.56600189 63.56639862
63.56679916 63.56719971 63.56760025 63.56800079 63.56840134
63.56880188 63.56919861 63.56959915 63.56999969 63.57040024
63.57080078 63.57120132 63.57160187 63.5719986 63.57239914
63.57279968 63.57320023 63.57360077 63.57400131 63.57440186
63.57479858 63.57519913 63.57559967 63.57600021 63.57640076
63.5768013 63.57720184 63.57759857 63.57799911 63.57839966
63.5788002 63.57920074 63.57960129 63.58000183 63.58039856
63.5807991 63.58119965 63.58160019 63.58200073 63.58240128
63.58280182 63.58319855 63.58359909 63.58399963 63.58440018
63.58480072 63.58520126 63.58560181 63.58599854 63.58639908
63.58679962 63.58720016 63.58760071 63.58800125 63.58840179
63.58879852 63.58919907 63.58959961 63.59000015 63.5904007
63.59080124 63.59120178 63.59159851 63.59199905 63.5923996
63.59280014 63.59320068 63.59360123 63.59400177 63.5943985
63.59479904 63.59519958 63.59560013 63.59600067 63.59640121
63.59680176 63.59719849 63.59759903 63.59799957 63.59840012
63.59880066 63.5992012 63.59960175 63.59999847 63.60039902
63.60079956 63.6012001 63.60160065 63.60200119 63.60240173
63.60279846 63.60319901 63.60359955 63.60400009 63.60440063
63.60480118 63.60520172 63.60559845 63.60599899 63.60639954
63.60680008 63.60720062 63.60760117 63.60800171 63.60839844
63.60879898 63.60919952 63.60960007 63.61000061 63.61040115
63.6108017 63.61119843 63.61159897 63.61199951 63.61240005
63.6128006 63.61320114 63.61360168 63.61399841 63.61439896
63.6147995 63.61520004 63.61560059 63.61600113 63.61640167
63.6167984 63.61719894 63.61759949 63.61800003 63.61840057
63.61880112 63.61920166 63.61959839 63.61999893 63.62039948
63.62080002 63.62120056 63.6216011 63.62200165 63.62239838
63.62279892 63.62319946 63.62360001 63.62400055 63.62440109
63.62480164 63.62519836 63.62559891 63.62599945 63.62639999
63.62680054 63.62720108 63.62760162 63.62799835 63.6283989
63.62879944 63.62919998 63.62960052 63.63000107 63.63040161
63.63079834 63.63119888 63.63159943 63.63199997 63.63240051
63.63280106 63.6332016 63.63359833 63.63399887 63.63439941
63.63479996 63.6352005 63.63560104 63.63600159 63.63639832
63.63679886 63.6371994 63.63759995 63.63800049 63.63840103
63.63880157 63.6391983 63.63959885 63.63999939 63.64039993
63.64080048 63.64120102 63.64160156 63.64199829 63.64239883
63.64279938 63.64319992 63.64360046 63.64400101 63.64440155
63.64479828 63.64519882 63.64559937 63.64599991 63.64640045
63.64680099 63.64720154 63.64759827 63.64799881 63.64839935
63.6487999 63.64920044 63.64960098 63.65000153 63.65039825
63.6507988 63.65119934 63.65159988 63.65200043 63.65240097
63.65280151 63.65319824 63.65359879 63.65399933 63.65439987
63.65480042 63.65520096 63.6556015 63.65599823 63.65639877
63.65679932 63.65719986 63.6576004 63.65800095 63.65840149
63.65879822 63.65919876 63.6595993 63.65999985 63.66040039
63.66080093 63.66120148 63.66159821 63.66199875 63.66239929
63.66279984]
Y:
[[ 2.49405336 2.49405479 2.49405622 ..., 2.49506283 2.49506426
2.49506545]
[ 2.50891113 2.50891256 2.50891399 ..., 2.50994205 2.50994349
2.50994492]
[ 2.52241206 2.52241349 2.52241492 ..., 2.52346492 2.52346635
2.52346778]
...,
[ 3.86142349 3.8614254 3.86142731 ..., 3.86282992 3.86283183
3.86283374]
[ 3.87396121 3.87396312 3.87396502 ..., 3.87538362 3.87538576
3.87538767]
[ 3.88761282 3.88761473 3.88761663 ..., 3.8890295 3.88903141
3.88903356]]
Z:
[[ 1.17835083e+03 1.18324207e+03 1.19204639e+03 ..., -5.34249163e+00
5.41835642e+00 3.46183872e+00]
[ 1.25111658e+03 1.24381042e+03 1.25598743e+03 ..., 1.37508184e-01
2.57289410e+00 -3.10967302e+00]
[ 1.31315002e+03 1.30969629e+03 1.30394031e+03 ..., 3.06716180e+00
7.64734685e-01 -9.59618759e+00]
...,
[ 3.51441078e+01 3.44107513e+01 3.44107513e+01 ..., -5.19034863e+00
-1.89025664e+00 -4.45699453e+00]
[ 2.77138577e+01 2.72105122e+01 2.67071648e+01 ..., -1.48025024e+00
5.33136606e-01 -9.76903498e-01]
[ 2.52477798e+01 2.67249184e+01 2.82020588e+01 ..., 1.36409312e-01
-2.32875556e-01 -2.07929993e+00]]
and the code is the following:
fig,ax = plt.subplots()
ax.pcolormesh(x,y,z, cmap='gist_rainbow', linewidth=0, zorder=1)
#plt.set_xlabel('Time [s]')
#plt.set_ylabel('R [m]; ECE')
#plt.view_init(90,-90)
#plt.set_axis_off()
ax.contour(z,zorder=2)
plt.show()
which outputs: http://www.image-upload.net/di/4T7EQ1AJ/Capture.png
another code:
fig,ax = plt.subplots()
#ax.pcolormesh(x,y,z, cmap='gist_rainbow', linewidth=0, zorder=1)
#plt.set_xlabel('Time [s]')
#plt.set_ylabel('R [m]; ECE')
#plt.view_init(90,-90)
#plt.set_axis_off()
print x
print "\n\n"
print y
print "\n\n"
print z
ax.contour(z,zorder=2)
plt.show()
which outputs: http://www.image-upload.net/di/4T7EQ1AJ/Capture.png
and finally, this code
fig,ax = plt.subplots()
ax.pcolormesh(x,y,z, cmap='gist_rainbow', linewidth=0, zorder=1)
#plt.set_xlabel('Time [s]')
#plt.set_ylabel('R [m]; ECE')
#plt.view_init(90,-90)
#plt.set_axis_off()
print x
print "\n\n"
print y
print "\n\n"
print z
#ax.contour(z,zorder=2)
plt.show()
which outputs: http://www.image-upload.net/di/JEKL/Capture.png
What I want is this contour http://www.image-upload.net/di/4T7EQ1AJ/Capture.png on top of http://www.image-upload.net/di/JEKL/Capture.png without the messy white background! :)

Contourf is a filled contour. So it is covering up everything underneath it. There are two options then:
1) You could try setting an alpha in contourf... i.e. alpha=0.25. This will make the filled contour partially see through.
2) Better option: use contour instead of contourf, which will then just plot contour lines.

You just need to use zorder which controls what order artists are rendered (which elements with high zorder 'above' elements with lower zorder)
fig, ax = plt.subplots()
ax.pcolormesh(..., zorder=1)
ax.contour(..., zorder=2)
plt.show()

Related

How to calculate histogram intersection?

I have two datasets:
data1 = np.array([11.59877469, 12.52525819, 11.30371594, 9.09666023, 9.69178239,
15.50122531, 16.73578474, 11.59361365, 9.33284219, 10.24323511,
12.39877469, 14.22419388, 11.18491025, 9.38917885, 9.56598452,
13.40122531, 13.58126828, 11.66021816, 9.90587666, 10.09661371,
12.69877469, 13.36333954, 11.40602325, 9.04926015, 9.58116652,
14.30122531, 13.41946488, 11.50330911, 9.70846369, 11.57496747,
13.09877469, 15.59128195, 12.38077981, 9.24187201, 9.4187133 ,
14.50122531, 14.00030896, 10.91137571, 10.09368741, 10.59655608,
12.19877469, 12.66493842, 11.151608 , 10.04291564, 10.75044566,
13.90122531, 13.80879138, 12.01213962, 9.77266764, 10.6047969 ,
11.29877469, 11.91469995, 10.49547065, 9.54128679, 10.70684913,
15.80122531, 15.69100383, 11.00648136, 10.26949539, 11.05984311,
13.29877469, 14.36322429, 10.39796445, 9.15126197, 10.19866594,
13.90122531, 13.22035631, 11.47720851, 10.43120555, 11.41316408,
13.09877469, 12.39251915, 11.18971136, 9.9574122 , 10.25744447,
14.20122531, 13.27219366, 11.60801697, 9.72976318, 12.07620948,
13.69877469, 16.08363333, 12.54008941, 9.38917885, 10.24985347,
14.20122531, 13.84544674, 11.29659942, 10.29799654, 10.78807365,
12.39877469, 13.80385357, 11.79124697, 9.85270434, 10.47867621,
15.30122531, 12.68375859, 11.88853282, 10.36460104, 11.52377995,
12.09877469, 12.36280497, 11.03280231, 8.76603826, 11.57464465,
15.70122531, 16.69782975, 11.86483278, 9.75586377, 11.00865558,
14.39877469, 14.31138694, 10.65958136, 10.03571398, 9.75056092,
13.30122531, 13.52878112, 12.46156728, 10.58571404, 12.04584549,
12.69877469, 13.2230095 , 11.41802602, 9.82180264, 9.27339153,
14.50122531, 13.09520827, 10.91617682, 9.59895472, 12.33344676,
13.29877469, 16.05391916, 12.32137696, 9.41767999, 9.83146261,
14.00122531, 14.13174836, 11.67702203, 9.43724456, 11.17869979,
12.19877469, 13.48024677, 10.98060112, 10.22832584, 10.64047961,
14.90122531, 13.34485454, 11.90053559, 10.23379258, 11.59774047,
13.19877469, 12.95059023, 10.88549547, 9.31057157, 11.71497469,
15.40122531, 16.38875513, 12.68748139, 10.10058357, 12.50913252,
14.09877469, 14.37710664, 11.30162088, 10.18782192, 10.10388188,
14.30122531, 13.33032237, 11.20389433, 10.76632313, 12.05343649,
13.09877469, 12.8918117 , 11.89595483, 9.50078288, 10.53745474,
14.70122531, 13.50665794, 11.46071013, 9.62505531, 12.11286483,
14.19877469, 16.84775416, 13.15122724, 10.29252979, 10.48626721,
14.60122531, 14.15322172, 11.65332199, 10.40960057, 12.80367496,
13.99877469, 13.09027045, 10.75948812, 9.74799647, 11.09682545,
14.70122531, 14.32261612, 12.15495084, 11.38675768, 12.20135753])
data2 = np.array([ 8.29102276, 8.81896403, 8.34976878, 6.42352427, 7.60717448,
11.70897724, 12.33303488, 8.46800077, 5.99812922, 7.01110385,
9.19102276, 10.2088251 , 7.81963878, 5.4700672 , 5.9739585 ,
8.70897724, 8.96487767, 7.99967417, 6.66146823, 6.83476828,
7.69102276, 8.39992335, 8.16675914, 6.29511636, 6.39364899,
10.30897724, 8.45040312, 8.19228603, 6.36654906, 8.26193451,
8.59102276, 11.18658667, 8.97980554, 5.79348751, 6.00367267,
9.60897724, 8.72976357, 7.91417073, 6.37615127, 6.71721123,
7.49102276, 8.6868748 , 7.18480092, 6.0359 , 5.92277097,
9.60897724, 8.545837 , 8.47760298, 5.90062301, 6.21032767,
7.39102276, 7.81907928, 7.17999982, 5.40106214, 6.25461858,
10.80897724, 11.00889351, 7.81186342, 5.67230835, 5.96003158,
8.19102276, 8.73112115, 6.52866357, 5.52706949, 4.4671902 ,
8.10897724, 8.19316585, 6.86320746, 6.08603321, 6.16608132,
6.79102276, 7.77483293, 7.52712017, 5.8504898 , 6.12882072,
8.50897724, 8.6267387 , 6.7990035 , 6.30474566, 7.22539441,
8.19102276, 10.96600474, 7.90754278, 5.22765471, 4.40776186,
8.50897724, 8.12679632, 6.83230576, 5.4844976 , 5.91578523,
6.69102276, 8.20840578, 7.03269298, 5.21565194, 5.05562527,
7.30897724, 7.55289343, 7.27693232, 5.92672361, 5.96003158,
6.99102276, 6.87038206, 6.68557263, 4.45240618, 5.20354631,
9.70897724, 10.90586864, 7.84036457, 5.95522475, 6.16543151,
6.89102276, 8.53201258, 5.92952852, 5.11574518, 4.783206 ,
7.70897724, 6.74452625, 6.08106276, 5.64620776, 5.95244058,
6.99102276, 7.40697979, 6.33365116, 4.83522934, 4.65111677,
8.50897724, 7.28871498, 6.29977521, 5.09927389, 6.87966444,
6.99102276, 10.09820922, 7.30360661, 5.13735016, 4.28326363,
8.40897724, 7.5750166 , 6.05496217, 5.00656879, 4.58470269,
5.59102276, 6.14985776, 6.17674211, 4.13858807, 4.790797 ,
6.60897724, 7.14838493, 6.35917805, 5.61290551, 5.48221238,
6.79102276, 6.94369276, 6.13863875, 4.45480673, 5.46078358,
8.90897724, 9.97235342, 7.59315097, 4.81395693, 4.91590048,
6.39102276, 7.53212783, 5.79631951, 4.70922198, 4.02602635,
7.50897724, 7.39109003, 6.52568932, 5.16827895, 5.08529488,
6.09102276, 7.06884081, 7.31560938, 4.59041629, 3.87181395,
7.40897724, 6.81783696, 6.42578256, 4.81875804, 5.69585312,
6.69102276, 10.16457875, 6.46205907, 4.9708389 , 3.51849298,
6.50897724, 7.2665918 , 5.5437311 , 4.37173093, 5.07011288,
5.19102276, 7.60543854, 6.30755057, 4.07198357, 3.76878908,
7.30897724, 6.88420648, 6.77290291, 5.13017559, 6.2691062 ])
and I'd like to plot two histograms:
x1,y1,_ = plt.hist(data1, alpha=0.4, bins=20)
x2,y2,_ = plt.hist(data2, alpha=0.4, bins=20)
the result is the following:
How can I calculate the area of intersecting region?
I tried to find similar bins centers (to find zero-crossing), but I wasn't succeeded, I also tried to find the minimum between two bins with the same index, but it doesn't work for negative data.
Make sure you use the same range and number of bins in both histograms:
>>> rng = min(data1.min(), data2.min()), max(data1.max(), data2.max())
>>> n1, bins1, _ = plt.hist(data1, alpha=0.4, bins=30, range=rng)
>>> n2, bins2, _ = plt.hist(data2, alpha=0.4, bins=30, range=rng)
Then the intersection can be easily calculated:
>>> intersection = np.minimum(n1, n2)
>>> area = intersection.sum()
>>> area
20.0
>>> plt.bar(bins1[:-1], intersection, width=bins1[1]- bins1[0])
You can use:
bin_edges = np.histogram_bin_edges(np.concatenate([data1, data2]))
hist1, _ = np.histogram(data1, bins=bin_edges)
hist2, _ = np.histogram(data2, bins=bin_edges)
np.minimum(hist1, hist2).sum()
Output:
20

Plot wont display in python

I'm using the following bit of code to plot two arrays of the same length -
import matplotlib.pyplot as plt
from scipy import stats
from scipy.stats import linregress
G_mag_values = [11.436, 11.341, 11.822, 11.646, 11.924, 12.057, 11.884, 11.805, 12.347, 12.662, 12.362, 12.555, 12.794, 12.819, 12.945, 12.733, 12.789, 12.878, 12.963, 13.094, 13.031, 12.962, 13.018, 12.906, 13.016, 13.088, 13.04, 13.035, 13.094, 13.032, 13.216, 13.062, 13.083, 13.126, 13.101, 13.089, 13.073, 13.182, 13.116, 13.145, 13.235, 13.161, 13.154, 13.383, 13.315, 13.429, 13.461, 13.287, 13.494, 13.459, 13.478, 13.534, 13.536, 13.536, 13.483, 13.544, 13.564, 13.544, 13.608, 13.655, 13.665, 13.668, 13.697, 13.649, 13.742, 13.756, 13.671, 13.701, 13.788, 13.723, 13.697, 13.713, 13.708, 13.765, 13.847, 13.992, 13.706, 13.79, 13.783, 13.844, 13.945, 13.928, 13.936, 13.956, 13.898, 14.059, 13.945, 14.039, 13.999, 14.087, 14.05, 14.083, 14.136, 14.124, 14.189, 14.149, 14.182, 14.281, 14.177, 14.297, 14.268, 14.454, 14.295]
G_cal_values = [-8.553610547563503, -8.085853602272588, -7.98491731861732, -7.852060056526794, -7.550944423333883, -7.569289687795749, -7.547088847268468, -7.544445036682168, -7.480698829329534, -7.184407435874912, -7.382606680295108, -7.2231275160942054, -7.093385973539046, -7.0473097125206685, -6.775012624594927, -6.814667514017907, -6.719898703328146, -6.741699011193633, -6.483121454948265, -6.320533066162438, -6.216044707275117, -6.037365656714626, -6.058593802250578, -6.0203190345840865, -6.036176430437363, -5.817887798572345, -5.838439347527171, -5.864922270102037, -5.755152671040021, -5.7709095683554725, -5.729226240967218, -5.606533007538604, -5.5817719334376905, -5.578993138005095, -5.62616747769538, -5.648413591916503, -5.611676700504294, -5.557722166623976, -5.5584623064502825, -5.425878164810264, -5.582204334985258, -5.529395790688368, -5.560750195967957, -5.433224654816512, -5.4751198268734385, -5.4592032005417215, -5.514591770369543, -5.580278698184566, -5.520695348050357, -5.501615700174841, -5.578645415877418, -5.692203332547151, -5.569497861450115, -5.335209902666812, -5.470963349023013, -5.44265375533589, -5.538541653702721, -5.355732832969871, -5.318164588926453, -5.376154615199398, -5.372133774215322, -5.361689907587619, -5.37608154921679, -5.412657572197508, -5.454613589602333, -5.339384591430104, -5.367511403407703, -5.258069473329993, -5.347580031901464, -4.9905279263992, -5.445096880253789, -5.192885553786512, -5.2983352094538505, -5.3930571447307365, -5.057910469318639, -5.32585105504838, -5.238649399637653, -5.122431894813153, -5.084559296025157, -5.139042420486851, -4.9919273140342915, -5.103619454431522, -5.017946144298159, -4.98136832081144, -5.084355565584671, -5.048634391386494, -4.887073481359435, -5.074683293771264, -5.050703776716202, -5.104772289705188, -4.9597601680524415, -4.971489935988787, -4.895283369236485, -4.9859511256778974, -4.840717539517584, -4.815665714699117, -4.937635861879118, -4.887219819695687, -4.813729758415283, -4.82667464608015, -4.865176481168528, -4.885105289124561, -4.887072278243732]
fig, ax = plt.subplots()
plt.scatter(G_mag_values,G_cal_values)
ax.minorticks_on()
ax.grid(which='major', linestyle='-', linewidth='0.5')
ax.grid(which='minor', linestyle='-', linewidth='0.5')
fig.set_size_inches(10,7)
best_fit_Y_G = []
slope_G, intercept_G, r_value_G, p_value_G, std_err_G = stats.linregress(G_mag_values,G_cal_values)
for value_G in G_mag_values:
best_fit_Y_G.append(intercept_G + slope_G*value_G)
plt.plot(G_mag_values, best_fit_Y_G, 'r', label = 'Best fit')
plt.title('M67 Calibration graph for G filter')
plt.xlabel('Real magnitude')
plt.ylabel('Measured magnitude')
plt.show()
curve_G = np.polyfit(G_mag_values,G_cal_values,1)
print('G filter polyfit line: slope {}; intercept = {}'.format(curve_G[0],curve_G[1]))
print('G filter linregress: slope {}; intercept = {}'.format(slope_G,intercept_G))
When I run this, it prints the values for slope and intercept from the best_fit_Y_G and curve_G, but it doesnt display the plot at all. Where am I going wrong?
I copy/pasted and run your code.
curve_G = np.polyfit(G_mag_values,G_cal_values,1)
That line gives me error. Then I imported numpy as np and problem solved.
output figure

Matplotlib and cartopy contours coming out completely wrong

I'm trying to plot latency maps of the world (for the moment, from London).
I have a database with a series of latencies between GPS points. I'd like to plot these on a map of the world with contour lines at various levels of latency, eg 50ms, 100ms, 200ms+ etc.
Right now I'm working with some sample code from the cartopy project for drawing labelled contours.
#!/usr/bin/env python3
from collections import defaultdict
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import numpy as np
from cartopy.examples.waves import sample_data
def main():
# lat, long and then latency from london
zs = [
(55.30927, 25.07725, 123.822),
(19.81889, 41.3275, 61.154),
(-58.37723, -34.61315, 250.024),
(16.37208, 48.20849, 26.987),
(15.45, 47.06667, 49.47),
(115.8614, -31.95224, 273.459),
(138.59863, -34.92866, 257.056),
(151.20732, -33.86785, 258.382),
(144.96332, -37.814, 259.542),
(149.12807, -35.28346, 291.766),
(153.02809, -27.46794, 283.675),
(49.89201, 40.37767, 89.569),
(90.40744, 23.7104, 232.381),
(5.56749, 50.63373, 11.093),
(4.34878, 50.85045, 9.644),
(3.22424, 51.20892, 10.713),
(4.40026, 51.22047, 10.905),
(27.91667, 43.21667, 47.367),
(23.32415, 42.69751, 52.414),
(-34.86306, -7.115, 208.491),
(-46.63611, -23.5475, 180.227),
(-47.92972, -15.77972, 227.893),
(-113.46871, 53.55014, 138.725),
(-73.58781, 45.50884, 84.456),
(-75.69812, 45.41117, 83.592),
(-106.66892, 52.13238, 108.197),
(-79.4163, 43.70011, 90.885),
(-123.11934, 49.24966, 130.308),
(-97.14704, 49.8844, 159.187),
(-63.57291, 44.6464, 74.226),
(-71.21454, 46.81228, 85.994),
(8.55, 47.36667, 22.503),
(8.96004, 46.01008, 33.557),
(6.63282, 46.516, 27.375),
(7.44744, 46.94809, 18.838),
(7.57327, 47.55839, 27.376),
(-70.64827, -33.45694, 195.647),
(114.0683, 22.54554, 233.257),
(121.45806, 31.22222, 191.371),
(120.16142, 30.29365, 237.881),
(114.87944, 40.81, 206.936),
(-75.56359, 6.25184, 150.812),
(-74.08175, 4.60971, 153.555),
(-84.11651, 10.00236, 140.832),
(33.03794, 34.68406, 64.037),
(14.42076, 50.08804, 27.247),
(16.60796, 49.19522, 29.384),
(11.07752, 49.45421, 17.739),
(11.57549, 48.13743, 20.171),
(9.99302, 53.55073, 43.907),
(8.68417, 50.11552, 13.733),
(6.77616, 51.22172, 12.371),
(12.10466, 47.7044, 19.85),
(12.56553, 55.67594, 16.867),
(3.08746, 36.73225, 40.629),
(-78.52495, -0.22985, 161.671),
(24.75353, 59.43696, 34.33),
(31.24967, 30.06263, 63.373),
(-0.37739, 39.46975, 32.652),
(-3.70256, 40.4165, 23.838),
(2.15899, 41.38879, 29.685),
(24.93545, 60.16952, 33.84),
(7.74553, 48.58392, 13.785),
(3.17456, 50.69421, 4.4),
(2.3488, 48.85341, 8.154),
(4.84671, 45.74846, 21.794),
(-1.61396, 54.97328, 5.601),
(-2.23743, 53.48095, 7.15),
(0.51667, 51.26667, 1.404),
(-1.12902, 50.79509, 3.552),
(-3.19648, 55.95206, 10.398),
(-1.51217, 52.40656, 6.462),
(-2.07972, 51.90006, 4.167),
(-3.18, 51.48, 6.116),
(-2.59665, 51.45523, 4.497),
(-5.92541, 54.59682, 10.33),
(44.83368, 41.69411, 88.784),
(-0.1969, 5.55602, 97.088),
(23.72784, 37.98376, 56.074),
(22.93086, 40.64361, 61.766),
(-90.51327, 14.64072, 342.363),
(114.17469, 22.27832, 219.061),
(15.97798, 45.81444, 31.988),
(19.04045, 47.49835, 28.312),
(106.84513, -6.21462, 191.802),
(-6.24889, 53.33306, 11.926),
(-6.92611, 52.84083, 12.585),
(35.21633, 31.76904, 63.795),
(34.78057, 32.08088, 111.049),
(-4.48333, 54.15, 14.732),
(78.15538, 11.65376, 70.362),
(73.85535, 18.51957, 274.087),
(77.22445, 28.63576, 150.969),
(80.27847, 13.08784, 144.037),
(75.8333, 22.71792, 137.159),
(78.45636, 17.38405, 187.213),
(72.88261, 19.07283, 187.64),
(77.59369, 12.97194, 131.413),
(-21.89541, 64.13548, 38.428),
(13.33561, 38.13205, 52.313),
(12.33265, 45.43713, 38.471),
(12.51133, 41.89193, 31.075),
(9.18951, 45.46427, 30.8),
(13.34109, 41.63976, 41.74),
(11.88068, 43.46276, 34.63),
(139.69171, 35.6895, 215.214),
(135.50218, 34.69374, 233.671),
(141.35, 43.06667, 242.554),
(36.81667, -1.28333, 174.557),
(104.91601, 11.56245, 208.175),
(126.9784, 37.566, 266.233),
(73.10211, 49.80187, 123.06),
(35.50157, 33.89332, 64.207),
(25.2798, 54.68916, 38.825),
(23.31667, 55.93333, 42.406),
(6.13, 49.61167, 15.731),
(24.10589, 56.946, 38.292),
(-5.00028, 34.03313, 77.605),
(28.8575, 47.00556, 51.354),
(14.5148, 35.89968, 48.426),
(-103.39182, 20.66682, 132.319),
(3.39467, 6.45407, 96.877),
(4.29861, 52.07667, 8.78),
(4.47917, 51.9225, 6.952),
(6.56667, 53.21917, 8.917),
(5.47778, 51.44083, 8.77),
(5.71806, 52.525, 7.052),
(4.88969, 52.37403, 6.88),
(4.66111, 51.86583, 9.274),
(10.74609, 59.91273, 28.073),
(5.32415, 60.39299, 28.998),
(174.77557, -41.28664, 282.141),
(172.63333, -43.53333, 273.846),
(174.76349, -36.84853, 279.023),
(-79.51973, 8.9936, 150.216),
(-77.02824, -12.04318, 163.241),
(120.9822, 14.6042, 220.693),
(120.33325, 16.04313, 245.787),
(74.35071, 31.558, 150.221),
(21.01178, 52.22977, 31.504),
(18.64912, 54.35227, 38.029),
(-66.10572, 18.46633, 106.983),
(-9.13333, 38.71667, 50.867),
(26.10626, 44.43225, 47.648),
(21.90333, 43.32472, 48.541),
(20.46513, 44.80401, 38.377),
(30.31413, 59.93863, 36.871),
(37.61556, 55.75222, 49.041),
(49.12214, 55.78874, 58.591),
(82.9346, 55.0415, 127.4),
(131.87353, 43.10562, 147.576),
(46.72185, 24.68773, 88.155),
(18.06871, 59.32938, 26.838),
(11.96679, 57.70716, 56.335),
(103.85007, 1.28967, 251.641),
(14.50513, 46.05108, 36.656),
(17.10674, 48.14816, 27.031),
(-55.16682, 5.86638, 166.626),
(100.50144, 13.75398, 265.619),
(10.16579, 36.81897, 52.035),
(27.13838, 38.41273, 67.678),
(32.85427, 39.91987, 60.337),
(28.94966, 41.01384, 51.519),
(29.06013, 40.19559, 72.622),
(121.53185, 25.04776, 261.82),
(39.26951, -6.82349, 150.264),
(30.5238, 50.45466, 41.29),
(36.25272, 49.98081, 58.363),
(32.58219, 0.31628, 159.655),
(-77.03637, 38.89511, 78.045),
(-81.65565, 30.33218, 94.374),
(-80.19366, 25.77427, 99.16),
(-81.37924, 28.53834, 97.119),
(-82.45843, 27.94752, 101.628),
(-84.38798, 33.749, 87.471),
(-81.09983, 32.08354, 119.433),
(-86.15804, 39.76838, 91.394),
(-90.07507, 29.95465, 116.938),
(-76.61219, 39.29038, 77.512),
(-94.57857, 39.09973, 98.802),
(-90.19789, 38.62727, 90.391),
(-90.18481, 32.29876, 105.299),
(-82.55402, 35.60095, 87.855),
(-80.84313, 35.22709, 92.297),
(-78.63861, 35.7721, 83.355),
(-84.51439, 39.12711, 96.657),
(-82.99879, 39.96118, 85.065),
(-97.51643, 35.46756, 108.011),
(-75.16379, 39.95233, 70.659),
(-83.92074, 35.96064, 111.042),
(-90.04898, 35.14953, 106.578),
(-97.74306, 30.26715, 117.634),
(-96.80667, 32.78306, 113.133),
(-95.36327, 29.76328, 105.161),
(-98.49363, 29.42412, 117.229),
(-93.60911, 41.60054, 101.56),
(-87.65005, 41.85003, 89.144),
(-88.30535, 41.88753, 26.006),
(-86.25001, 41.68338, 89.071),
(-71.05977, 42.35843, 75.733),
(-83.04575, 42.33143, 92.257),
(-84.55553, 42.73253, 97.811),
(-93.26384, 44.97997, 97.597),
(-96.66696, 40.8, 97.95),
(-74.39904, 40.49927, 69.437),
(-74.05653, 40.78955, 71.265),
(-73.75623, 42.65258, 74.285),
(-78.87837, 42.88645, 79.13),
(-73.96625, 40.78343, 68.356),
(-74.00597, 40.71427, 71.41),
(-76.14742, 43.04812, 76.038),
(-81.8418, 41.23811, 83.061),
(-81.69541, 41.4995, 93.716),
(-83.55521, 41.66394, 97.349),
(-79.99589, 40.44062, 87.723),
(-75.6649, 41.40916, 73.833),
(-88.01983, 44.51916, 102.133),
(-112.07404, 33.44838, 130.966),
(-121.98857, 37.54827, 134.523),
(-118.24368, 34.05223, 136.396),
(-121.4944, 38.58157, 136.928),
(-117.16472, 32.71571, 146.006),
(-122.41942, 37.77493, 141.373),
(-121.89496, 37.33939, 133.461),
(-104.82136, 38.83388, 108.739),
(-104.9847, 39.73915, 121.039),
(-106.65114, 35.08449, 164.648),
(-115.13722, 36.17497, 146.236),
(-122.39168, 40.58654, 156.6),
(-122.67621, 45.52345, 130.65),
(-123.34174, 43.2165, 147.008),
(-111.89105, 40.76078, 151.18),
(-122.33207, 47.60621, 129.576),
(-157.85833, 21.30694, 186.991),
(-56.18816, -34.90328, 228.291),
(-66.87919, 10.48801, 148.292),
(106.62965, 10.82302, 305.353),
(105.84117, 21.0245, 240.46),
(21.16688, 42.67272, 48.995),
(28.04363, -26.20227, 165.392),
(18.42322, -33.92584, 143.62),
]
z = np.array(zs, dtype="float")
breakpoint()
fig = plt.figure()
# Setup a global EckertIII map with faint coastlines.
ax = fig.add_subplot(1, 1, 1, projection=ccrs.EckertIII())
ax.set_global()
ax.coastlines("110m", alpha=0.1)
# Add colourful filled contours.
filled_c = ax.contourf(z, transform=ccrs.PlateCarree())
# And black line contours.
line_c = ax.contour(
z, levels=filled_c.levels, colors=["black"], transform=ccrs.PlateCarree()
)
# Uncomment to make the line contours invisible.
# plt.setp(line_c.collections, visible=False)
# Add a colorbar for the filled contour.
fig.colorbar(filled_c, orientation="horizontal")
# Use the line contours to place contour labels.
ax.clabel(
line_c, # Typically best results when labelling line contours.
colors=["black"],
manual=False, # Automatic placement vs manual placement.
inline=True, # Cut the line where the label will be placed.
fmt=" {:.0f} ".format, # Labes as integers, with some extra space.
)
plt.show()
if __name__ == "__main__":
main()
Instead of getting nice contours, I get this image:
What's my problem?
Am I passing the wrong args to the matplotlib functions?
Is numpy somehow interpreting my floats wrong?
You need to separate data into lat, long, and height. Then use tricontour type functions to generate the plot. Here is the relevant part of the modified code that handles the data and generate the plot.
# more code before this (see the question)
zs = np.array(zs)
# get `lat`, `long`, and `height` separately
lats, lons, zzz = zs[:,0], zs[:,1], zs[:,2]
fig = plt.figure()
# Setup a global EckertIII map with faint coastlines.
ax = fig.add_subplot(1, 1, 1, projection=ccrs.EckertIII())
ax.set_global()
ax.coastlines("110m", alpha=0.1)
# Add colourful filled contours.
filled_c = ax.tricontourf(lats, lons, zzz, transform=ccrs.PlateCarree())
# And black line contours.
line_c = ax.tricontour(
lats, lons, zzz, levels=filled_c.levels, colors=["black"], transform=ccrs.PlateCarree()
)
# Uncomment to make the line contours invisible.
# plt.setp(line_c.collections, visible=False)
# Add a colorbar for the filled contour.
fig.colorbar(filled_c, orientation="horizontal")
# Use the line contours to place contour labels.
ax.clabel(
line_c, # Typically best results when labelling line contours.
colors=["black"],
manual=False, # Automatic placement vs manual placement.
inline=True, # Cut the line where the label will be placed.
fmt=" {:.0f} ".format, # Labes as integers, with some extra space.
)
plt.show()

plt.tricontourf(x,y,z) creating color values outside of data bounds

I am attempting to make compressor and turbine maps colored by efficiency. I have achieved this, but the tricontourf I am attempting leads to level colors outside of where my data even exists. I need to make sure the contour ends at the bounds of my data. Is there a way to achieve this?
My code:
import numpy as np
import matplotlib.pyplot as plt
alphaMap = np.array([0.000, 90.000])
NcMap = np.array([0.300, 0.400, 0.500, 0.600, 0.700, 0.750, 0.800, 0.850, 0.900, 0.950, 1.000, 1.050, 1.100, 1.150])
RlineMap = np.array([1.000, 1.200, 1.400, 1.600, 1.800, 2.000, 2.200, 2.400, 2.600, 2.800, 3.000])
WCmap = np.array([[[17.907, 19.339, 20.749, 22.136, 23.498, 24.833, 26.141, 27.420, 28.669, 29.887, 31.011],
[24.951, 26.742, 28.485, 30.177, 31.815, 33.397, 34.921, 36.385, 37.788, 39.128, 40.405],
[32.682, 34.715, 36.662, 38.520, 40.286, 41.958, 43.533, 45.011, 46.390, 47.669, 48.848],
[40.927, 43.115, 45.168, 47.083, 48.858, 50.492, 51.983, 53.331, 54.539, 55.607, 56.537],
[49.850, 52.122, 54.195, 56.068, 57.741, 59.215, 60.494, 61.580, 62.479, 63.197, 63.739],
[54.798, 57.066, 59.099, 60.897, 62.463, 63.800, 64.913, 65.810, 66.497, 66.983, 67.278],
[60.051, 62.252, 64.185, 65.851, 67.255, 68.405, 69.307, 69.973, 70.413, 70.638, 70.675],
[65.313, 67.427, 69.262, 70.824, 72.118, 73.153, 73.938, 74.484, 74.803, 74.907, 74.907],
[70.995, 72.902, 74.542, 75.920, 77.043, 77.920, 78.560, 78.974, 79.174, 79.198, 79.198],
[77.441, 78.904, 80.155, 81.199, 82.042, 82.690, 83.151, 83.434, 83.545, 83.548, 83.548],
[84.344, 85.211, 85.952, 86.572, 87.074, 87.460, 87.735, 87.903, 87.967, 87.968, 87.968],
[89.305, 89.687, 90.025, 90.320, 90.572, 90.783, 90.953, 91.083, 91.174, 91.227, 91.243],
[93.626, 93.712, 93.793, 93.868, 93.939, 94.004, 94.064, 94.120, 94.170, 94.216, 94.257],
[95.978, 95.989, 96.000, 96.012, 96.022, 96.033, 96.044, 96.054, 96.064, 96.074, 96.084]],
[[17.907, 19.339, 20.749, 22.136, 23.498, 24.833, 26.141, 27.420, 28.669, 29.887, 31.011],
[24.951, 26.742, 28.485, 30.177, 31.815, 33.397, 34.921, 36.385, 37.788, 39.128, 40.405],
[32.682, 34.715, 36.662, 38.520, 40.286, 41.958, 43.533, 45.011, 46.390, 47.669, 48.848],
[40.927, 43.115, 45.168, 47.083, 48.858, 50.492, 51.983, 53.331, 54.539, 55.607, 56.537],
[49.850, 52.122, 54.195, 56.068, 57.741, 59.215, 60.494, 61.580, 62.479, 63.197, 63.739],
[54.798, 57.066, 59.099, 60.897, 62.463, 63.800, 64.913, 65.810, 66.497, 66.983, 67.278],
[60.051, 62.252, 64.185, 65.851, 67.255, 68.405, 69.307, 69.973, 70.413, 70.638, 70.675],
[65.313, 67.427, 69.262, 70.824, 72.118, 73.153, 73.938, 74.484, 74.803, 74.907, 74.907],
[70.995, 72.902, 74.542, 75.920, 77.043, 77.920, 78.560, 78.974, 79.174, 79.198, 79.198],
[77.441, 78.904, 80.155, 81.199, 82.042, 82.690, 83.151, 83.434, 83.545, 83.548, 83.548],
[84.344, 85.211, 85.952, 86.572, 87.074, 87.460, 87.735, 87.903, 87.967, 87.968, 87.968],
[89.305, 89.687, 90.025, 90.320, 90.572, 90.783, 90.953, 91.083, 91.174, 91.227, 91.243],
[93.626, 93.712, 93.793, 93.868, 93.939, 94.004, 94.064, 94.120, 94.170, 94.216, 94.257],
[96.084, 96.074, 96.064, 96.054, 96.044, 96.033, 96.022, 96.012, 96.000, 95.989, 95.978]]])
effMap = np.array([[[.8070, .8291, .8461, .8566, .8586, .8497, .8170, .7410, .6022, .3674, .0000],
[.8230, .8454, .8628, .8741, .8775, .8708, .8419, .7732, .6477, .4372, .0916],
[.8411, .8631, .8805, .8921, .8966, .8918, .8671, .8065, .6959, .5124, .2168],
[.8565, .8783, .8957, .9077, .9131, .9099, .8883, .8338, .7340, .5696, .3083],
[.8662, .8879, .9055, .9179, .9239, .9219, .9024, .8520, .7600, .6096, .3739],
[.8699, .8917, .9093, .9218, .9281, .9265, .9080, .8598, .7721, .6297, .4089],
[.8743, .8957, .9130, .9253, .9316, .9304, .9131, .8678, .7858, .6538, .4519],
[.8836, .9026, .9179, .9287, .9342, .9331, .9183, .8804, .8128, .7065, .5485],
[.8943, .9103, .9230, .9319, .9362, .9351, .9231, .8930, .8406, .7602, .6442],
[.9060, .9169, .9253, .9310, .9334, .9321, .9236, .9036, .8703, .8211, .7529],
[.9170, .9224, .9264, .9288, .9293, .9280, .9231, .9127, .8962, .8730, .8423],
[.9159, .9171, .9176, .9177, .9171, .9159, .9136, .9097, .9042, .8968, .8876],
[.9061, .9059, .9055, .9052, .9047, .9042, .9036, .9028, .9018, .9007, .8994],
[.8962, .8964, .8965, .8966, .8967, .8968, .8969, .8970, .8971, .8972, .8973]],
[[.8070, .8291, .8461, .8566, .8586, .8497, .8170, .7410, .6022, .3674, .0714],
[.8230, .8454, .8628, .8741, .8775, .8708, .8419, .7732, .6477, .4372, .0916],
[.8411, .8631, .8805, .8921, .8966, .8918, .8671, .8065, .6959, .5124, .2168],
[.8565, .8783, .8957, .9077, .9131, .9099, .8883, .8338, .7340, .5696, .3083],
[.8662, .8879, .9055, .9179, .9239, .9219, .9024, .8520, .7600, .6096, .3739],
[.8699, .8917, .9093, .9218, .9281, .9265, .9080, .8598, .7721, .6297, .4089],
[.8743, .8957, .9130, .9253, .9316, .9304, .9131, .8678, .7858, .6538, .4519],
[.8836, .9026, .9179, .9287, .9342, .9331, .9183, .8804, .8128, .7065, .5485],
[.8943, .9103, .9230, .9319, .9362, .9351, .9231, .8930, .8406, .7602, .6442],
[.9060, .9169, .9253, .9310, .9334, .9321, .9236, .9036, .8703, .8211, .7529],
[.9170, .9224, .9264, .9288, .9293, .9280, .9231, .9127, .8962, .8730, .8423],
[.9159, .9171, .9176, .9177, .9171, .9159, .9136, .9097, .9042, .8968, .8876],
[.9061, .9059, .9055, .9052, .9047, .9042, .9036, .9028, .9018, .9007, .8994],
[.8962, .8964, .8965, .8966, .8967, .8968, .8969, .8970, .8971, .8972, .8973]]])
PRmap = np.array([[[1.0678, 1.0649, 1.0613, 1.0571, 1.0522, 1.0468, 1.0402, 1.0322, 1.0227, 1.0117, 1.0000],
[1.1239, 1.1186, 1.1122, 1.1047, 1.0962, 1.0865, 1.0751, 1.0611, 1.0445, 1.0257, 1.0045],
[1.1994, 1.1910, 1.1809, 1.1691, 1.1558, 1.1409, 1.1233, 1.1020, 1.0771, 1.0488, 1.0173],
[1.2981, 1.2855, 1.2706, 1.2533, 1.2339, 1.2122, 1.1869, 1.1563, 1.1210, 1.0811, 1.0370],
[1.4289, 1.4111, 1.3899, 1.3655, 1.3380, 1.3076, 1.2720, 1.2295, 1.1804, 1.1254, 1.0654],
[1.5118, 1.4909, 1.4661, 1.4375, 1.4052, 1.3695, 1.3278, 1.2779, 1.2205, 1.1565, 1.0868],
[1.6070, 1.5827, 1.5538, 1.5205, 1.4831, 1.4417, 1.3934, 1.3358, 1.2697, 1.1962, 1.1165],
[1.7160, 1.6881, 1.6555, 1.6183, 1.5767, 1.5312, 1.4785, 1.4160, 1.3448, 1.2660, 1.1808],
[1.8402, 1.8086, 1.7724, 1.7318, 1.6869, 1.6381, 1.5824, 1.5170, 1.4430, 1.3615, 1.2736],
[1.9930, 1.9587, 1.9206, 1.8788, 1.8336, 1.7852, 1.7309, 1.6685, 1.5988, 1.5225, 1.4405],
[2.1593, 2.1257, 2.0899, 2.0518, 2.0117, 1.9695, 1.9235, 1.8724, 1.8163, 1.7557, 1.6909],
[2.2764, 2.2510, 2.2248, 2.1978, 2.1701, 2.1416, 2.1118, 2.0801, 2.0464, 2.0108, 1.9735],
[2.3771, 2.3664, 2.3557, 2.3448, 2.3339, 2.3229, 2.3118, 2.3004, 2.2887, 2.2768, 2.2646],
[2.4559, 2.4538, 2.4516, 2.4495, 2.4473, 2.4452, 2.443, 2.4409, 2.4387, 2.4365, 2.4343]],
[[1.0678, 1.0649, 1.0613, 1.0571, 1.0522, 1.0468, 1.0402, 1.0322, 1.0227, 1.0117, 1.0000],
[1.1239, 1.1186, 1.1122, 1.1047, 1.0962, 1.0865, 1.0751, 1.0611, 1.0445, 1.0257, 1.0045],
[1.1994, 1.1910, 1.1809, 1.1691, 1.1558, 1.1409, 1.1233, 1.1020, 1.0771, 1.0488, 1.0173],
[1.2981, 1.2855, 1.2706, 1.2533, 1.2339, 1.2122, 1.1869, 1.1563, 1.1210, 1.0811, 1.0370],
[1.4289, 1.4111, 1.3899, 1.3655, 1.3380, 1.3076, 1.2720, 1.2295, 1.1804, 1.1254, 1.0654],
[1.5118, 1.4909, 1.4661, 1.4375, 1.4052, 1.3695, 1.3278, 1.2779, 1.2205, 1.1565, 1.0868],
[1.6070, 1.5827, 1.5538, 1.5205, 1.4831, 1.4417, 1.3934, 1.3358, 1.2697, 1.1962, 1.1165],
[1.7160, 1.6881, 1.6555, 1.6183, 1.5767, 1.5312, 1.4785, 1.4160, 1.3448, 1.2660, 1.1808],
[1.8402, 1.8086, 1.7724, 1.7318, 1.6869, 1.6381, 1.5824, 1.5170, 1.4430, 1.3615, 1.2736],
[1.9930, 1.9587, 1.9206, 1.8788, 1.8336, 1.7852, 1.7309, 1.6685, 1.5988, 1.5225, 1.4405],
[2.1593, 2.1257, 2.0899, 2.0518, 2.0117, 1.9695, 1.9235, 1.8724, 1.8163, 1.7557, 1.6909],
[2.2764, 2.2510, 2.2248, 2.1978, 2.1701, 2.1416, 2.1118, 2.0801, 2.0464, 2.0108, 1.9735],
[2.3771, 2.3664, 2.3557, 2.3448, 2.3339, 2.3229, 2.3118, 2.3004, 2.2887, 2.2768, 2.2646],
[2.4343, 2.4365, 2.4387, 2.4409, 2.4430, 2.4452, 2.4473, 2.4495, 2.4516, 2.4538, 2.4559]]])
label = []
for x in NcMap:
label.append(x*100)
for i in range(0,14):
plt.annotate('{0}%'.format(round(label[i],2)),xy = ((flowmax[i],PRmax[i])), textcoords='offset points', xytext=(0,6), ha = 'center', color = 'b')
plt.xlim(0,1)
plt.ylim(1,8)
plt.ylabel(r'$PR_{off}$', fontsize=16)
plt.xlabel(r'$\.m_{c,off} [kg/s]$', fontsize=16)
x = WCmap[0,:14,:]
x = x.flatten().tolist()
y = PRmap[0,:14,:]
y = y.flatten().tolist()
z = effMap[0,:14,:]
z = z.flatten().tolist()
plt.tricontourf(x,y,z, cmap = 'jet')
cbar = plt.colorbar()
cbar.set_label(r'$\eta_{off}$', fontsize=16)
plt.show()
Compressor Map Plot

How to plot two or more overlapping 3-D Gaussian surfaces in the same graph in Python?

How can I plot two or more overlapping Gaussian surfaces in the same graph, as below?
This is the code I have written, But the first surface is being covered by the second one. They are overlapping , But i want them to be displayed transparently
result obtained: https://i.stack.imgur.com/5LSsW.png
code :https://pastebin.com/embed_iframe/ms8cngXm
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def loaddata(filename,label):
file = open(filename, 'r')
text = file.read()
text=text.split('\n')
file.close()
dataset = list()
for line in text:
if len(line)>0:
value = line.split()
dataset.append([float(value[0]), float(value[1]), label])
return dataset
def multivariate_gaussian(pos, mu, Sigma):
n = mu.shape[0]
Sigma_det = np.linalg.det(Sigma)
Sigma_inv = np.linalg.inv(Sigma)
N = np.sqrt((2*np.pi)**n * Sigma_det)
fac = np.einsum('...k,kl,...l->...', pos-mu, Sigma_inv, pos-mu)
return np.exp(-fac / 2) / N
#this is just for maptype ignore this
_viridis_data = [[0.267004, 0.004874, 0.329415],
[0.268510, 0.009605, 0.335427],
[0.269944, 0.014625, 0.341379],
[0.271305, 0.019942, 0.347269],
[0.272594, 0.025563, 0.353093],
[0.273809, 0.031497, 0.358853],
[0.274952, 0.037752, 0.364543],
[0.276022, 0.044167, 0.370164],
[0.277018, 0.050344, 0.375715],
[0.277941, 0.056324, 0.381191],
[0.278791, 0.062145, 0.386592],
[0.279566, 0.067836, 0.391917],
[0.280267, 0.073417, 0.397163],
[0.280894, 0.078907, 0.402329],
[0.281446, 0.084320, 0.407414],
[0.281924, 0.089666, 0.412415],
[0.282327, 0.094955, 0.417331],
[0.282656, 0.100196, 0.422160],
[0.282910, 0.105393, 0.426902],
[0.283091, 0.110553, 0.431554],
[0.283197, 0.115680, 0.436115],
[0.283229, 0.120777, 0.440584],
[0.283187, 0.125848, 0.444960],
[0.283072, 0.130895, 0.449241],
[0.282884, 0.135920, 0.453427],
[0.282623, 0.140926, 0.457517],
[0.282290, 0.145912, 0.461510],
[0.281887, 0.150881, 0.465405],
[0.281412, 0.155834, 0.469201],
[0.280868, 0.160771, 0.472899],
[0.280255, 0.165693, 0.476498],
[0.279574, 0.170599, 0.479997],
[0.278826, 0.175490, 0.483397],
[0.278012, 0.180367, 0.486697],
[0.277134, 0.185228, 0.489898],
[0.276194, 0.190074, 0.493001],
[0.275191, 0.194905, 0.496005],
[0.274128, 0.199721, 0.498911],
[0.273006, 0.204520, 0.501721],
[0.271828, 0.209303, 0.504434],
[0.270595, 0.214069, 0.507052],
[0.269308, 0.218818, 0.509577],
[0.267968, 0.223549, 0.512008],
[0.266580, 0.228262, 0.514349],
[0.265145, 0.232956, 0.516599],
[0.263663, 0.237631, 0.518762],
[0.262138, 0.242286, 0.520837],
[0.260571, 0.246922, 0.522828],
[0.258965, 0.251537, 0.524736],
[0.257322, 0.256130, 0.526563],
[0.255645, 0.260703, 0.528312],
[0.253935, 0.265254, 0.529983],
[0.252194, 0.269783, 0.531579],
[0.250425, 0.274290, 0.533103],
[0.248629, 0.278775, 0.534556],
[0.246811, 0.283237, 0.535941],
[0.244972, 0.287675, 0.537260],
[0.243113, 0.292092, 0.538516],
[0.241237, 0.296485, 0.539709],
[0.239346, 0.300855, 0.540844],
[0.237441, 0.305202, 0.541921],
[0.235526, 0.309527, 0.542944],
[0.233603, 0.313828, 0.543914],
[0.231674, 0.318106, 0.544834],
[0.229739, 0.322361, 0.545706],
[0.227802, 0.326594, 0.546532],
[0.225863, 0.330805, 0.547314],
[0.223925, 0.334994, 0.548053],
[0.221989, 0.339161, 0.548752],
[0.220057, 0.343307, 0.549413],
[0.218130, 0.347432, 0.550038],
[0.216210, 0.351535, 0.550627],
[0.214298, 0.355619, 0.551184],
[0.212395, 0.359683, 0.551710],
[0.210503, 0.363727, 0.552206],
[0.208623, 0.367752, 0.552675],
[0.206756, 0.371758, 0.553117],
[0.204903, 0.375746, 0.553533],
[0.203063, 0.379716, 0.553925],
[0.201239, 0.383670, 0.554294],
[0.199430, 0.387607, 0.554642],
[0.197636, 0.391528, 0.554969],
[0.195860, 0.395433, 0.555276],
[0.194100, 0.399323, 0.555565],
[0.192357, 0.403199, 0.555836],
[0.190631, 0.407061, 0.556089],
[0.188923, 0.410910, 0.556326],
[0.187231, 0.414746, 0.556547],
[0.185556, 0.418570, 0.556753],
[0.183898, 0.422383, 0.556944],
[0.182256, 0.426184, 0.557120],
[0.180629, 0.429975, 0.557282],
[0.179019, 0.433756, 0.557430],
[0.177423, 0.437527, 0.557565],
[0.175841, 0.441290, 0.557685],
[0.174274, 0.445044, 0.557792],
[0.172719, 0.448791, 0.557885],
[0.171176, 0.452530, 0.557965],
[0.169646, 0.456262, 0.558030],
[0.168126, 0.459988, 0.558082],
[0.166617, 0.463708, 0.558119],
[0.165117, 0.467423, 0.558141],
[0.163625, 0.471133, 0.558148],
[0.162142, 0.474838, 0.558140],
[0.160665, 0.478540, 0.558115],
[0.159194, 0.482237, 0.558073],
[0.157729, 0.485932, 0.558013],
[0.156270, 0.489624, 0.557936],
[0.154815, 0.493313, 0.557840],
[0.153364, 0.497000, 0.557724],
[0.151918, 0.500685, 0.557587],
[0.150476, 0.504369, 0.557430],
[0.149039, 0.508051, 0.557250],
[0.147607, 0.511733, 0.557049],
[0.146180, 0.515413, 0.556823],
[0.144759, 0.519093, 0.556572],
[0.143343, 0.522773, 0.556295],
[0.141935, 0.526453, 0.555991],
[0.140536, 0.530132, 0.555659],
[0.139147, 0.533812, 0.555298],
[0.137770, 0.537492, 0.554906],
[0.136408, 0.541173, 0.554483],
[0.135066, 0.544853, 0.554029],
[0.133743, 0.548535, 0.553541],
[0.132444, 0.552216, 0.553018],
[0.131172, 0.555899, 0.552459],
[0.129933, 0.559582, 0.551864],
[0.128729, 0.563265, 0.551229],
[0.127568, 0.566949, 0.550556],
[0.126453, 0.570633, 0.549841],
[0.125394, 0.574318, 0.549086],
[0.124395, 0.578002, 0.548287],
[0.123463, 0.581687, 0.547445],
[0.122606, 0.585371, 0.546557],
[0.121831, 0.589055, 0.545623],
[0.121148, 0.592739, 0.544641],
[0.120565, 0.596422, 0.543611],
[0.120092, 0.600104, 0.542530],
[0.119738, 0.603785, 0.541400],
[0.119512, 0.607464, 0.540218],
[0.119423, 0.611141, 0.538982],
[0.119483, 0.614817, 0.537692],
[0.119699, 0.618490, 0.536347],
[0.120081, 0.622161, 0.534946],
[0.120638, 0.625828, 0.533488],
[0.121380, 0.629492, 0.531973],
[0.122312, 0.633153, 0.530398],
[0.123444, 0.636809, 0.528763],
[0.124780, 0.640461, 0.527068],
[0.126326, 0.644107, 0.525311],
[0.128087, 0.647749, 0.523491],
[0.130067, 0.651384, 0.521608],
[0.132268, 0.655014, 0.519661],
[0.134692, 0.658636, 0.517649],
[0.137339, 0.662252, 0.515571],
[0.140210, 0.665859, 0.513427],
[0.143303, 0.669459, 0.511215],
[0.146616, 0.673050, 0.508936],
[0.150148, 0.676631, 0.506589],
[0.153894, 0.680203, 0.504172],
[0.157851, 0.683765, 0.501686],
[0.162016, 0.687316, 0.499129],
[0.166383, 0.690856, 0.496502],
[0.170948, 0.694384, 0.493803],
[0.175707, 0.697900, 0.491033],
[0.180653, 0.701402, 0.488189],
[0.185783, 0.704891, 0.485273],
[0.191090, 0.708366, 0.482284],
[0.196571, 0.711827, 0.479221],
[0.202219, 0.715272, 0.476084],
[0.208030, 0.718701, 0.472873],
[0.214000, 0.722114, 0.469588],
[0.220124, 0.725509, 0.466226],
[0.226397, 0.728888, 0.462789],
[0.232815, 0.732247, 0.459277],
[0.239374, 0.735588, 0.455688],
[0.246070, 0.738910, 0.452024],
[0.252899, 0.742211, 0.448284],
[0.259857, 0.745492, 0.444467],
[0.266941, 0.748751, 0.440573],
[0.274149, 0.751988, 0.436601],
[0.281477, 0.755203, 0.432552],
[0.288921, 0.758394, 0.428426],
[0.296479, 0.761561, 0.424223],
[0.304148, 0.764704, 0.419943],
[0.311925, 0.767822, 0.415586],
[0.319809, 0.770914, 0.411152],
[0.327796, 0.773980, 0.406640],
[0.335885, 0.777018, 0.402049],
[0.344074, 0.780029, 0.397381],
[0.352360, 0.783011, 0.392636],
[0.360741, 0.785964, 0.387814],
[0.369214, 0.788888, 0.382914],
[0.377779, 0.791781, 0.377939],
[0.386433, 0.794644, 0.372886],
[0.395174, 0.797475, 0.367757],
[0.404001, 0.800275, 0.362552],
[0.412913, 0.803041, 0.357269],
[0.421908, 0.805774, 0.351910],
[0.430983, 0.808473, 0.346476],
[0.440137, 0.811138, 0.340967],
[0.449368, 0.813768, 0.335384],
[0.458674, 0.816363, 0.329727],
[0.468053, 0.818921, 0.323998],
[0.477504, 0.821444, 0.318195],
[0.487026, 0.823929, 0.312321],
[0.496615, 0.826376, 0.306377],
[0.506271, 0.828786, 0.300362],
[0.515992, 0.831158, 0.294279],
[0.525776, 0.833491, 0.288127],
[0.535621, 0.835785, 0.281908],
[0.545524, 0.838039, 0.275626],
[0.555484, 0.840254, 0.269281],
[0.565498, 0.842430, 0.262877],
[0.575563, 0.844566, 0.256415],
[0.585678, 0.846661, 0.249897],
[0.595839, 0.848717, 0.243329],
[0.606045, 0.850733, 0.236712],
[0.616293, 0.852709, 0.230052],
[0.626579, 0.854645, 0.223353],
[0.636902, 0.856542, 0.216620],
[0.647257, 0.858400, 0.209861],
[0.657642, 0.860219, 0.203082],
[0.668054, 0.861999, 0.196293],
[0.678489, 0.863742, 0.189503],
[0.688944, 0.865448, 0.182725],
[0.699415, 0.867117, 0.175971],
[0.709898, 0.868751, 0.169257],
[0.720391, 0.870350, 0.162603],
[0.730889, 0.871916, 0.156029],
[0.741388, 0.873449, 0.149561],
[0.751884, 0.874951, 0.143228],
[0.762373, 0.876424, 0.137064],
[0.772852, 0.877868, 0.131109],
[0.783315, 0.879285, 0.125405],
[0.793760, 0.880678, 0.120005],
[0.804182, 0.882046, 0.114965],
[0.814576, 0.883393, 0.110347],
[0.824940, 0.884720, 0.106217],
[0.835270, 0.886029, 0.102646],
[0.845561, 0.887322, 0.099702],
[0.855810, 0.888601, 0.097452],
[0.866013, 0.889868, 0.095953],
[0.876168, 0.891125, 0.095250],
[0.886271, 0.892374, 0.095374],
[0.896320, 0.893616, 0.096335],
[0.906311, 0.894855, 0.098125],
[0.916242, 0.896091, 0.100717],
[0.926106, 0.897330, 0.104071],
[0.935904, 0.898570, 0.108131],
[0.945636, 0.899815, 0.112838],
[0.955300, 0.901065, 0.118128],
[0.964894, 0.902323, 0.123941],
[0.974417, 0.903590, 0.130215],
[0.983868, 0.904867, 0.136897],
[0.993248, 0.906157, 0.143936]]
from matplotlib.colors import ListedColormap
viridis = ListedColormap(_viridis_data, name='viridis')
plt.register_cmap(name='viridis', cmap=viridis)
plt.set_cmap(viridis)
filename=r"C:/Users/santhoskumar/Desktop/random/pattern/class1_rw.txt"
label=0
dataset1= loaddata(filename,label)
print('Loaded data file {0} with {1} rows'.format(filename, len(dataset1)))
filename = r"C:/Users/santhoskumar/Desktop/random/pattern/class2_rw.txt"
label=1
dataset2 = loaddata(filename,label)
print('Loaded data file {0} with {1} rows'.format(filename, len(dataset2)))
filename = r'C:/Users/santhoskumar/Desktop/random/pattern/class3_rw.txt'
label=2
dataset3 = loaddata(filename,label)
print('Loaded data file {0} with {1} rows'.format(filename, len(dataset3)))
N = 600
X = np.linspace(200, 800, N)
Y = np.linspace(300, 1200, N)
X, Y = np.meshgrid(X, Y)
dataset=np.array(dataset1)
x,y,label=dataset.T
dat=x,y
dat=np.array(dat)
cov=np.cov(dat)
mu=np.mean(dat,axis=1)
print(mu)
# Pack X and Y into a single 3-dimensional array
pos = np.empty(X.shape + (2,))
pos[:, :, 0] = X
pos[:, :, 1] = Y
# The distribution on the variables X, Y packed into pos.
Z = multivariate_gaussian(pos, mu, cov)
minn=1e-15
for i in range(len(Z)):
for j in range(len(Z[i])):
Z[i][j]*=1e4
fig = plt.figure()
ax = fig.gca(projection='3d')
ax1=fig.gca(projection='3d')
ax.plot_surface(X, Y, Z,rstride=30,cstride=30, linewidth=1,antialiased=True,cmap=viridis)
cset = ax.contourf(X, Y, Z,zdir='z',offset=-0.4,cmap=viridis)
ax.set_zlim(-0.4,0.40)
ax.set_zticks(np.linspace(0,0.40,5))
ax.view_init(27, -21)
dataset=np.array(dataset2)
x,y,label=dataset.T
dat=x,y
dat=np.array(dat)
cov=np.cov(dat)
mu=np.mean(dat,axis=1)
print(mu)
# Pack X and Y into a single 3-dimensional array
pos = np.empty(X.shape + (2,))
pos[:, :, 0] = X
pos[:, :, 1] = Y
# The distribution on the variables X, Y packed into pos.
Z = multivariate_gaussian(pos, mu, cov)
minn=1e-15
for i in range(len(Z)):
for j in range(len(Z[i])):
Z[i][j]*=1e4
ax.plot_surface(X, Y, Z,rstride=20,cstride=20, linewidth=1,antialiased=True,color='red',cmap=viridis)
cset1 = ax.contourf(X, Y, Z,zdir='z',offset=-0.4,cmap=viridis)
ax.set_zlim(-0.4,0.40)
ax.set_zticks(np.linspace(0,0.40,5))
ax.view_init(27, -21)
plt.subplots_adjust(hspace=0.5)
plt.show()
If I understand your question correctly, you just have to call the plotting method multiple times such as:
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(x1, y1, z1,cmap='viridis',linewidth=0)
ax.plot_surface(x2, y2, z2,cmap='viridis',linewidth=0)
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
plt.show()

Categories

Resources