Artifacts in a filled contour plot on 3D axes - python
I have a frustrating problem that only manifests itself when plotting filled contour plots on 3D axes and only in certain situations.
Here is an example of the issue I am experiencing:
and
These are the same data at different contouring intervals.
You'll notice on the left side of the domain there is mis-filling occurring. This is a plot with the Z points squished into the Z=0 plane, via a plotting command like
ax3d.contourf(X, Y, dbz[z25,:,:], zdir='z', offset=0, levels=levels, cmap='pymeteo_radar', alpha=0.50)
The miscontouring happens regardless of alpha level or colormap used, but is sensitive to the number of levels. The use of zdir and offset do not effect the mis-contouring (the artifact just occurs on the Z surface. If I do not fill the contour, there is no mis-contouring. I can also alter the domain to sometimes make the issue better (or worse), but I have many plots to make within the same domain so that is not a fix.
This issue does not occur when the same data is plotted on 2D axes, e.g.:
This plot has some extra data on it, but you can see that the filled contouring does not have the same artifact from mis-filling the contour that occurs on the 3d axes.
Below is a script you can run to reproduce the issue.
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
data=np.array([[53.9751, 51.5681, 50.7119, 51.1049, 51.5339, 51.4977, 51.2387,50.761, 50.1732, 49.8218, 49.5442, 48.936, 47.4498, 46.6484, 45.8542, 45.136, 44.5268, 44.071, 43.7665, 43.5928, 43.5269, 43.5385, 43.6053, 45.565, 47.0071, 46.8664, 47.372, 47.8324, 48.295, 48.731, 49.0522, 49.4001, 49.7111, 49.9919, 50.2527, 50.4928, 50.7135, 50.8831, 51.0806, 51.2683 ],
[55.6671, 52.53, 50.7764, 50.5632, 51.2095, 51.5659, 51.521, 51.2143, 50.653, 50.2371, 49.989, 49.8089, 49.6058, 47.8355, 47.3124, 46.7346, 46.1616, 45.6498, 45.2462, 44.967, 44.8005, 44.7284, 44.7295, 44.7869, 46.959, 45.0194, 46.73, 48.0766, 48.9395, 49.5325, 49.8498, 50.1887, 50.4798, 50.7406, 50.9808, 51.2003, 51.4074, 51.555, 51.7429, 51.9218 ],
[56.6513, 53.5919, 51.2774, 50.3133, 50.7705, 51.533, 51.8287, 51.7083, 51.2816, 50.7933, 50.4806, 50.2671, 50.1009, 50.0096, 49.9052, 49.4698, 47.4655, 47.0717, 46.6849, 46.3583, 46.1122, 45.952, 45.8678, 45.8485, 45.8811, 45.956, 46.0634, 47.2225, 49.4363, 50.2482, 50.527, 50.8558, 51.1358, 51.3809, 51.607, 51.8179, 52.0161, 52.1454, 52.3263, 52.497 ],
[57.078, 54.3224, 52.0759, 50.4679, 50.4677, 51.297, 52.0284, 52.1594, 51.9395, 51.5518, 51.1419, 50.8765, 50.6686, 50.5101, 50.4078, 50.3473, 50.3592, 50.3813, 49.7504, 47.55, 47.324, 47.1365, 46.9978, 46.9119, 46.8743, 46.8811, 46.9257, 47.0013, 50.0148, 50.9106, 51.1133, 51.4282, 51.7064, 51.943, 52.1587, 52.3597, 52.4789, 52.6631, 52.8359, 52.9966 ],
[57.3835, 54.9025, 52.8571, 50.9842, 50.5197, 51.1494, 52.0599, 52.4732, 52.4716, 52.2656, 51.9535, 51.6068, 51.3466, 51.1513, 50.9708, 50.8321, 50.7639, 50.7944, 50.8817, 49.8122, 48.2038, 48.086, 47.9704, 47.8735, 47.8035, 47.7644, 47.7574, 47.7803, 50.8194, 51.5486, 51.6645, 51.9745, 52.2349, 52.4508, 52.6481, 52.8317, 52.9412, 53.1097, 53.2699, 53.4171 ],
[57.9157, 55.6092, 53.6306, 51.8011, 50.9372, 51.2615, 52.1406, 52.7436, 52.8528, 52.7829, 52.6322, 52.403, 52.1149, 51.866, 51.6624, 51.4773, 51.317, 51.2183, 51.2153, 51.1367, 48.5913, 48.6216, 48.6218, 48.5951, 48.5589, 48.527, 48.5081, 50.5185, 51.6998, 51.905, 52.2258, 52.4891, 52.7062, 52.8926, 53.0655, 53.2251, 53.3262, 53.4755, 53.6169, 53.7471 ],
[58.6093, 56.432, 54.307, 52.6277, 51.584, 51.6482, 52.3762, 53.0685, 53.2545, 53.217, 53.1356, 53.0351, 52.8481, 52.6154, 52.39, 52.177, 51.9977, 51.843, 51.7172, 51.4587, 48.7481, 48.7984, 48.864, 48.9291, 48.9843, 49.0228, 50.496, 51.8667, 52.3404, 52.4759, 52.6889, 52.8851, 53.0525, 53.2072, 53.354, 53.4576, 53.5925, 53.7217, 53.8432, 53.956 ],
[58.9719, 56.9885, 54.8768, 53.3526, 52.3025, 52.2089, 52.7762, 53.4444, 53.6768, 53.6706, 53.5692, 53.5162, 53.4373, 53.2886, 53.1113, 52.9065, 52.6988, 52.5193, 52.3544, 52.0384, 48.9624, 48.9653, 49.0005, 49.0574, 49.1258, 50.692, 51.9726, 52.4309, 52.699, 52.8194, 52.9845, 53.1336, 53.2669, 53.393, 53.5118, 53.6086, 53.7213, 53.8293, 53.9308, 54.026 ],
[58.5754, 56.945, 55.068, 53.7798, 52.9469, 52.854, 53.3136,53.8929, 54.1205, 54.1178, 54.0128, 53.9289, 53.8906, 53.8239,53.717, 53.5724, 53.3818, 53.1892, 53.009, 49.3078, 49.2524,49.2165, 49.2032, 49.2187, 50.463, 51.9497, 52.4487, 52.7041,52.8358, 52.9776, 53.1101, 53.2293, 53.3419, 53.4487, 53.5401,53.6365, 53.7301, 53.8205, 53.9062, 53.9869 ],
[57.623, 56.547, 55.0117, 54.0512, 53.5372, 53.5246, 53.927,54.3868, 54.5828, 54.5811, 54.4501, 54.3235, 54.2626, 54.2334,54.1802, 54.1137, 53.9897, 53.8202, 49.796, 49.6864, 49.5946,49.5216, 49.4703, 49.4432, 51.8479, 52.5574, 52.8359, 52.9722,53.0827, 53.1826, 53.2747, 53.3597, 53.4405, 53.5138, 53.5944,53.6751, 53.7536, 53.829, 53.9019, 53.9721 ],
[56.902, 56.0005, 54.9159, 54.3352, 54.123, 54.2014, 54.5659,54.8917, 55.0307, 55.0139, 54.8838, 54.7044, 54.5863, 54.5548,54.5258, 54.4957, 54.4633, 51.4821, 50.1897, 50.0758, 49.9683,49.8704, 49.7842, 51.5064, 52.7625, 53.0724, 53.1926, 53.2682,53.3404, 53.4119, 53.4831, 53.5517, 53.6169, 53.6763, 53.7383,53.8009, 53.8644, 53.9281, 53.9905, 54.0517 ],
[56.3455, 55.5524, 54.9336, 54.6836, 54.703, 54.8657, 55.1749,55.3844, 55.4521, 55.4019, 55.2622, 55.0281, 54.8981, 54.6591,54.7866, 54.7678, 54.7654, 54.0436, 54.2302, 52.2533, 50.3305,50.2276, 50.1268, 52.9617, 53.4395, 53.5504, 53.5481, 53.5524,53.5699, 53.6014, 53.644, 53.6931, 53.7445, 53.7996, 53.8548,53.9097, 53.9655, 54.0229, 54.0813, 54.1393 ],
[55.7493, 55.3019, 55.1012, 55.0906, 55.234, 55.4751, 55.7134,55.8462, 55.8461, 55.7425, 55.5725, 55.3535, 55.1612, 54.958,55.0193, 54.9584, 54.9531, 54.8886, 54.8256, 54.2211, 50.6477,50.5564, 53.0546, 53.8592, 54.08, 54.0288, 53.9509, 53.8796,53.8307, 53.8073, 53.8034, 53.8142, 53.8383, 53.8725, 53.9128,53.9558, 54.0013, 54.0497, 54.103, 54.1597 ],
[55.2575, 55.1664, 55.3165, 55.5004, 55.7345, 55.9901, 56.1852,56.2599, 56.2027, 56.0454, 55.818, 55.5754, 55.302, 55.2083,55.0224, 55.1415, 55.0656, 55.0446, 55.0263, 54.7728, 50.8924,53.4671, 54.2587, 54.5146, 54.6171, 54.519, 54.3857, 54.2497,54.1355, 54.0509, 53.9932, 53.9584, 53.941, 53.939, 53.9527,53.9798, 54.0111, 54.0465, 54.0868, 54.1339 ],
[54.8665, 55.1533, 55.5095, 55.8512, 56.1541, 56.3995, 56.5593,56.6009, 56.5079, 56.3001, 56.0178, 55.7187, 55.448, 55.063,55.2016, 55.2116, 55.1817, 55.112, 55.1099, 55.0299, 54.3358,54.6966, 54.9199, 55.0156, 55.0728, 54.975, 54.8299, 54.6609,54.493, 54.3475, 54.2349, 54.1517, 54.0928, 54.0516, 54.0245,54.013, 54.0206, 54.0404, 54.0667, 54.0989 ],
[54.2676, 55.1132, 55.6112, 56.09, 56.428, 56.6661, 56.8056,56.8374, 56.7339, 56.4923, 56.1474, 55.7977, 55.4805, 55.2341,54.8999, 55.2662, 55.2927, 55.185, 55.1237, 55.1268, 54.9772,55.1418, 55.2612, 55.3333, 55.379, 55.3244, 55.2153, 55.0629,54.881, 54.6926, 54.523, 54.3866, 54.2855, 54.2118, 54.1583,54.1191, 54.0935, 54.0834, 54.0885, 54.1057 ],
[54.1771, 55.0795, 55.7075, 56.1772, 56.5183, 56.7522, 56.8898,56.9315, 56.8427, 56.6056, 56.2317, 55.8095, 55.4436, 55.183,55.0284, 54.9504, 55.2833, 55.2563, 55.1498, 55.1342, 55.1331,55.259, 55.3705, 55.4452, 55.4955, 55.5087, 55.4697, 55.3766,55.2324, 55.049, 54.8485, 54.6578, 54.4995, 54.3822, 54.3002,54.2427, 54.2022, 54.1749, 54.1598, 54.1561 ],
[53.9112, 54.85, 55.6641, 56.0844, 56.4062, 56.6232, 56.757,56.8149, 56.7669, 56.5754, 56.2311, 55.785, 55.366, 55.0104,54.812, 54.8845, 55.1273, 55.2339, 55.1976, 55.1049, 55.0913,55.1843, 55.3048, 55.4076, 55.4709, 55.518, 55.5455, 55.5329,55.4636, 55.3349, 55.1595, 54.9529, 54.7462, 54.5681, 54.4342,54.3439, 54.2848, 54.2446, 54.2222, 54.2135 ],
[53.9368, 54.9196, 55.4408, 55.7999, 56.0652, 56.2423, 56.348,56.4106, 56.4114, 56.3028, 56.0519, 55.6779, 55.2493, 54.8836,54.6592, 54.6347, 54.8341, 55.0606, 55.1396, 55.0967, 55.0325,55.0501, 55.1451, 55.2627, 55.3559, 55.4216, 55.4789, 55.5183,55.5245, 55.4779, 55.3701, 55.2072, 55.0029, 54.7876, 54.5915,54.4378, 54.3368, 54.2787, 54.2415, 54.2271 ],
[53.9325, 54.6506, 55.0421, 55.2926, 55.4603, 55.5679, 55.6285,55.6792, 55.7234, 55.731, 55.639, 55.3923, 55.043, 54.6845,54.4188, 54.3242, 54.4606, 54.7449, 54.9548, 55.0171, 55.0047,54.9454, 54.9666, 55.0651, 55.1828, 55.2677, 55.3308, 55.3914,55.438, 55.4544, 55.4277, 55.3385, 55.1907, 54.9981, 54.7786,54.5691, 54.4013, 54.2898, 54.233, 54.1994 ] ])
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
X,Y = np.meshgrid(np.arange(-30.0,-20.0,0.25), np.arange(20.0,25,0.25))
ax.contourf(X,Y,data,zdir='z',offset=0, levels=np.arange(0,75,1))
ax.set_zlim(0.0,2.0)
plt.savefig('testfig.png')
plt.close()
This code will produce the plot:
In all of the cases I have observed this mis-contouring the bad triangle always has a vertex near the bottom left of the domain. My data is regularly gridded and for the domain in question is uniform in X and Y. In this case the mis-filling will go away if the number of contour levels is reduced. In some other cases this does not always help or just changes the visual appearance of the error. In any case, even at very coarse contouring I still get errors in a subset of my plots.
Has anyone seen this before and found a fix for it? Am I overlooking something? I'm open to workarounds that don't involve lowering my contouring level (which does reduce the errors overall). If others are in agreement that this could be a bug in the mplot3d, I will file a bug report with them (Issue opened here). I have a feeling the problem lies with contouring very strong gradients when the levels option causes dense contours, but oddly only on 3d axes.
Relevant version information:
Python 3.4.1
matplotlib 1.4.3
numpy 1.9.0
This turned out to be a longstanding bug in matplotlib.mplot3d that ignores path information when taking 2D contourf sets and extending them into 3D. This causes, under certain circumstances, paths with holes to render improperly when a path segment intended as a "move" is instead "drawn".
I contributed a fix for this issue to matplotlib and this bug is fixed in the matplotlib 1.5.0 stable release.
The same test code as in the question produces a correct plot with matplotlib 1.5, as seen below:
The problem is most probably in matplotlib itself and you're not doing anything wrong.
By experimenting a bit I found that if you multiply the input data by 1.01 or 0.999 the plot comes out right, but 1.001 or 0.9999 is not enough to fix the issue.
Adding or subtracting a constant instead shifts the color but keeps the problem evident.
As a wild guess some internal computation falls in a singularity (even if I cannot think what formula would be in danger in this case).
You should submit a bug to their tracker.
EDIT
On a second thought may be matplotlib is trying to compute contour polygons instead of just computing a background texture on a texel-by-texel basis and this could result in annoying accuracy problems that depend on the value. Drawing contour lines is instead much easier because you can just compute the segments in a marching-square approach without worrying about rebuilding the full contour line topology (and for example if a very tiny segment is missing from the line contour plot you're not going to notice anyway).
If this is indeed the bug then may be the fix is not easy because requires a full reimplementation of the plane drawing in a completely different (even if easier) way.
Related
Remove lines between quad glyphs in Bokeh
I am trying to create a 2D histogram with square bins (instead of hex elements) and I'm using quad to do so. First, I create a 2D histogram using numpy (not shown for brevity). Then I plot it using the quad glyph: def plotHist2D(self,name): """ Creates a 2D histogram figure in Bokeh. Parameters ---------- name : string The name of one of the column data sources in self.files. Returns ------- A bokeh plot object. """ cds = ColumnDataSource(self.makeHist2D(name)) plot_cmap = linear_cmap('tops','Turbo256',0,1,) bar_cmap = LinearColorMapper(palette='Turbo256',low=0,high=1) p = figure(plot_height=350,plot_width=350, title='kW/mm^2', x_axis_label='x (m)', y_axis_label='y (m)', x_range=(-0.05,0.05), y_range=(-0.05,0.05), output_backend="webgl", toolbar_location="above") p.quad(top='yrights',bottom='ylefts',left='xlefts',right='xrights', fill_color=plot_cmap,line_color=plot_cmap, line_width=0.1,line_alpha=1.0, alpha=1.0,source=cds) color_bar = ColorBar(color_mapper=bar_cmap,width=8, border_line_color=None,location=(0,0)) p.add_layout(color_bar,'right') return p This functions, in that it creates a figure, but the figure has unpleasant lines at the border between the quad elements. I cannot find any combination of settings for the parameters line_width, line_color, and line_alpha that will get rid of these lines. Is there any combination of settings that will eliminate those lines entirely? If not, how should I be creating this 2D histogram with square bins?
The trick to this is to use line_color with the same color mapper and not set line_width too small. The following call to quad works. p.quad(top='yrights',bottom='ylefts',left='xlefts',right='xrights', fill_color=plot_cmap, line_color=plot_cmap, line_width=1.0,line_alpha=1.0, alpha=1.0,source=cds) The desired plot: I thought I had tried every permutation before asking this question. I'm not sure why a smaller line_width makes the lines more visible. As #EugenePakhomov suggests you can also just leave out the line_width entirely. Presumably this is because the default setting is large enough not to cause the "meshing."
Set Nan Colour In Mayavi
I have lots of data, interpolated with separate functions, which contains gaps (NaN) values. I would like to plot these planes of data as images in MayaVi using imshow(), which I have done with some success. To avoid the complexity of the data, consider displaying the array: import numpy as np from mayavi.mlab import * grid_z0 = np.array([[1,2,3],[4,np.nan,6],[7,8,9]]) imshow(grid_z0, interpolate = False) Snapshot without transparency: In reality, I would like the gaps in my data to be transparent. (i.e. the dark red 128,0,0 square in the middle would be see through). I'm aware that editing colormaps isn't really a thing in MayaVi (as it is in matplotlib), but I can see 'NaN color' options in the MayaVi pipeline, and documentation like this show that editing the color options is a possibility. However, I'm stuck to see why the NaN values come out as (128,0,0) RGB, and what I can do to make them transparent.
More thinking, reading and fiddling: img = imshow(grid_z0) img.module_manager.scalar_lut_manager.lut.nan_color = 0, 0, 0, 0 img.update_pipeline()
How to smooth or overlap bins in pyplot.hist2d?
I am plotting a 2D histogram to show, for example, the concentration of lightnings (given by their position registered in longitude and latitude). The number of data points is not too large (53) and the result is too coarse. Here is a picture of the result: For this reason, I am trying to find a way to weight in data from surrounding bins. For example, there is a bin at longitude = 130 and latitude = 34.395 with 0 lightning registered, but with several around it. I would want this bin to reflect somehow the concentration around it. In other words, I want to smooth the data by having overlapping bins (so that a data point can be counted more than once, by different contiguous bins). I understand that hist2d has the input option for "weights", but this would only work to make a data point more "important" within its bin. The simplified code is below and I can clarify anything needed. import numpy as np import matplotlib.pyplot as plt # Here are the data, to experiment if needed longitude = np.array([119.165, 115.828, 110.354, 117.124, 119.16 , 107.068, 108.628, 126.914, 125.685, 116.608, 122.455, 116.278, 123.43, 128.84, 128.603, 130.192, 124.508, 121.916, 133.245, 125.088, 126.641, 127.224, 113.686, 129.376, 127.312, 121.353, 117.834, 125.219, 138.077, 153.299, 135.66 , 128.391, 118.011, 117.313, 119.986, 118.619, 119.178, 120.295, 121.991, 123.519, 135.948, 132.224, 129.317, 135.334, 132.923, 129.828, 139.006, 140.813, 116.207, 139.254, 120.922, 112.171, 143.508]) latitude = np.array([34.381, 34.351, 34.359, 34.357, 34.364, 34.339, 34.351, 34.38, 34.381, 34.366, 34.373, 34.366, 34.369, 34.387, 34.39 , 34.39 , 34.386, 34.371, 34.394, 34.386, 34.384, 34.387, 34.369, 34.4 , 34.396, 34.37 , 34.374, 34.383, 34.403, 34.429, 34.405, 34.385, 34.367, 34.36 , 34.367, 34.364, 34.363, 34.367, 34.367, 34.369, 34.399, 34.396, 34.382, 34.401, 34.396, 34.392, 34.401, 34.401, 34.362, 34.404, 34.382, 34.346, 34.406]) # Number of bins Nbins = 15 # Plot histogram of the positions plt.hist2d(longitude,latitude, bins=Nbins) plt.plot(longitude,latitude,'o',markersize = 8, color = 'k') plt.plot(longitude,latitude,'o',markersize = 6, color = 'w') plt.colorbar() plt.show()
Perhaps you're getting confused with the concept of 2D-histogram, or histogram. Besides the fact a histogram is a bar plot groupping data into plot, it is also a dicretized estimation of a probability funtion. In your case, the presence probability. For this reason, I would not try to overlap histograms. Moreover, because the histogram is 'discrete', it will be necessarily coarse. Actually, the resolution of a histogram is an important parameter regarding the desired visualization. Going back to your question, if you want to disminish the coarse effect, you may to simply want to play on Nbins. Perhaps, other graph type would suit better your usage: see this gallery and the 2D-density plot with shading.
pyplot plot shows a window with no graph
I have several arrays for which I calculate the Frobenius norm. Then I simply draw a graph of these calculated norms vs the index of their corresponding arrays. The problem is that when the plot window pops out, there is no graph on it. But, when I add a styling for my plot, it shows the graph. I also tried to use save figure, but the saved figure just shows a window without any graph on it. The last thing that I tried was to print out the array of the calculated norms, defining it as a numpy array and draw it vs the array of the corresponding indices and it shows me the graph! So, my question is why I cannot draw the graph with pylot plot function. This is what I get when I print out the array of calculated norms: FrobNorm=[[ -3.27415727e-01 2.83421670e+00 -2.59669415e+00 -3.83713705e+00 -1.11064367e+00 -9.83842479e+00 9.64202990e+00 -3.66747069e+00 9.49022713e+00 -3.58659316e+00 4.28355911e+00 -4.58104577e+00 -4.26765959e+00 -6.54306600e-01 4.31816208e+00 1.08043604e+01 3.36647201e+01 -9.47369163e+00 1.41183067e+01 1.75464238e+00 6.84732164e+00 -1.13034176e+01 -1.83641151e+01 -6.07528575e+01 -2.11765783e+01 -3.46253416e+01 -3.50911001e+01 -1.78855570e+01 2.00630855e+01 1.90068192e+01 3.33858144e-01 -1.75526132e+01 -1.34355117e+01 -8.39318642e+00 -1.96338714e+01 -5.80396650e+01 -1.52712614e+01 -7.95109842e+00 -1.14383666e+01 -4.29497153e+00 -1.97874688e+01 -1.32635215e+01 3.10595354e+00 3.30488466e-01 1.24957569e+00 2.32608957e+01 -5.12962561e-01 3.23879652e+00 1.80536181e+01 1.64091731e+01 2.46815567e+01 2.01190758e+01 2.25210602e+01 1.92789009e+01 4.32809711e+01 1.24060317e+02 5.11700004e+00 2.56249967e+00 3.27317719e+01 3.01294858e+01 2.96865339e+01 2.01666494e+01 -1.75473758e+00 -9.73091969e+00 -1.51961382e+01 8.11369952e+00 -1.74469244e+01 5.94097932e+00 -5.43142631e+00 -4.40072150e+00 -1.51168549e+01 -5.58957352e+00 -2.34872324e+04 9.19836593e+02 6.76833045e+03 7.59304882e+03 1.77573454e+03 9.71109062e+02 1.63742243e+03 3.70221807e+02 1.01405251e+03 4.06811235e+02 1.45049823e+02 1.43212472e+02 8.88928849e+01 3.10859242e+02 4.79435420e+01 6.86347162e+01 2.14372829e+01 5.43555421e+01 1.39810283e+01 9.51714116e+00 4.98563968e+01 4.02058896e+01 1.61359027e+02 7.91939932e+00 1.73949723e+01 5.19412047e+01 1.89645369e+01 2.25526021e+01 1.36734416e+01 3.13646035e+01 2.02633125e+01 5.16259077e+01 7.34024536e+01 2.01376746e+01 8.50796026e+00 1.76689397e+01 5.32159344e+01 1.75182361e+01 2.38797434e+01 2.21623152e+01 2.15496171e+01 1.56287225e+01 7.12160153e+01 1.20319418e+01 -2.14376043e-01 -2.16844613e+00 7.31383577e+00 9.60358643e+00 1.53346738e+01 -1.75376507e+01 -4.23607412e+01 -1.34004685e+01 -5.74096286e+01 -1.88056408e+01 1.24411854e+00 -2.20228598e+00 -1.44691587e+01 -4.02906454e+00 -7.06859151e+00 -9.28329296e-01 3.97785623e+00 -1.17290825e+01 5.30538782e+00 -1.30573008e+00 2.57332085e-01 -5.03652416e+00 -8.01889243e+00 -4.21210481e+00 7.97575488e+00 1.33063141e+01 1.94559898e+01 1.30643051e+01 1.39963350e+00 1.31746057e+01 4.87291463e-01 7.62221548e+00 1.90832548e+00 -9.17783469e+00 -6.74190235e+00 -5.18322407e+00 2.08694160e+00 -8.32251763e+00 -3.41052019e+01 -4.07077413e+00 -5.35572194e+00 -1.00300755e+01 -1.85180723e+00 -2.85137343e+00 -2.92087149e+00 5.82955457e+00 4.00575111e+00 1.17418771e+01 2.13152055e+01 6.74130687e+00 2.89890044e+00 9.56403257e+00 9.49920338e+00 -4.90698086e+00 -4.31125932e-01 7.43422603e+00 -1.36522668e+00 6.71239870e+00 2.97819245e+01 2.70232682e+00 1.43525496e+01 7.69774164e-01 6.11231825e+00 1.48208154e+00 -2.23136432e+00 4.61075719e+00 -3.59137897e+01 -1.62455157e+01 -6.07367620e+01 -2.62556836e+00 -1.64717047e-01 -1.33588774e+01 -8.23873116e+00 -4.69412397e+00 -8.64679071e+00 -7.05601974e+00 9.42962930e+00 -1.08717341e+01 -5.27810809e+01 -8.69225245e+00 -4.99076301e+00]] When I plot the graph vs its indices array, I only get the window with no graph: plt.plot(numVec,FrobNorm) plt.show() But, when I use a styling for the plot it shows the graph (something like scatter plot, which I am not interested in): plt.plot(numVec,FrobNorm,'ro') plt.show() Now, I print the array of calculated norms. comma separate it, and define a numpy array with its elements and simply draw the graph of this numpy array and the corresponding array of indices and I get: I want to get the same thing in the first place. My question is why I cannot get any graph when I plot the calculated norms. As, I said I am not looking for the scatter graph, like in the second figure, which surprisingly is something that I can get only by changing the styling of the figure.
I think I got it. I used squeeze and it works. So, the plot line should be changed like this: plt.plot(np.squeeze(NumVec),np.squeeze(FrobNorm)) I still don't understand why, but this is what I guess; I think somehow the format of the numpy arrays that were produced, was in the way that plot function could only see the range of the values without having access to every single element of the arrays. When I didn't use the squeeze function, I got the window without the plot, but the range of the x and y axis were the same as when I could draw the plot in the second and third figures. This is only a guess, I hope someone could help me with the real reason. Thank you for all the feedback!
Empty figures with basemap
I am trying to use model output on flows in a tidal basin. The model uses a curvilinear grid. My first task is to just plot one component of the velocity of the highest water layer. I wrote a little bit of code based on the question under the name: Matplotlib Streamplot for Unevenly (curvilinear) Grid. Now as far as I can see, I didn't change anything essential except for the numbers in comparison to the earlier metioned question, but the figures remain empty. I put the code and some numbers below. import numpy as np from mpl_toolkits.basemap import Basemap import matplotlib.pyplot as plt Lat = np.array([[ 30.40098833, 30.40103752, 30.40108727, 30.40113704], [ 30.40140046, 30.40145021, 30.40149997, 30.40154973], [ 30.40186559, 30.40191478, 30.40196453, 30.4020143 ], [ 30.40239781, 30.402447, 30.40249676, 30.40254652]]) Lon = np.array([[-86.51729818, -86.51794126, -86.5185871, -86.51923603], [-86.51725858, -86.51790149, -86.51854717, -86.51919595], [-86.51721383, -86.51785659, -86.51850228, -86.51915089], [-86.51716242, -86.51780518, -86.51845087, -86.51909948]]) Xvel = np.array([[ 0.0325774, -0.02811189, -0.04972513, -0.07736091], [ 0.00592685, -0.00043959, -0.00735147, -0.05015078], [-0.03365543, -0.03183309, -0.03701356, -0.07232581], [-0.09578606, -0.10139448, -0.11220678, -0.13221299]]) plt.ion() fig,(ax1) = plt.subplots(1,1) m = Basemap(llcrnrlon=Lon.min(),llcrnrlat=Lat.min(), urcrnrlon=Lon.max(), urcrnrlat=Lat.max(), projection='merc',resolution='i',ax=ax1) m.contourf(Lat,Lon,Xvel,latlon=True) m.drawcoastlines() m.drawrivers() m.plot(Lat,Lon,'-k',alpha=0.3,latlon=True) m.plot(Lat.T,Lon.T,'-k',alpha=0.3,latlon=True) Could someone tell me what it is that causes the plots to remain empty? I have another question regarding the use of Basemap: My datasheet also contains a lot of NaN's (gridpoints with no information). I was wondering how I can let Basemap know that I just don't have any information on these positions and that I don't want any plotting there. In the current code it causes an 'Points of LinearRing do not form a closed linestring' error.
Regarding the second part of your question (since Ajean appears to have solved the first half), the standard way to tell Matplotlib (and hence Basemap) to not plot data is to create a masked array. Lets say your Xvel contained NaNs, then to plot it you would do import numpy.ma as ma m.contourf(Lon, Lat, ma.masked_invalid(Xvel), latlon=True) the function ma.masked_invalid, as its name implies, masks all invalid (i.e., NaN) values, so that they're not plotted.