Line partially not plotted - matplotlib

Problem:
Plotting a closed 2D polygon works fine.
When I try plotting it 1D (by replacing the second dimension with a constant), it fails in some special cases: Then the polygon (which becomes a line) is not drawn completely.
What I tried:
Different plotting-styles like . , o -- of which only the latter one reproduces the problem while non-line-like-styles work.
Shifted/rolled the polygon-array which did not work when shifting by 1 or 2, but for 3 and 4 that solved the problem.
Asked a friend who is clueless ^^
Helped myself so far by directly plotting a line between x.min() and x.max().
For simplicity I removed the z-Data. When plotting x in combination with z, it also works - as long as z is far away from being constant. But then I get a wobbly graph instead of a straight line.
Still How can it be, that whether the data is fully plotted or not depends on its order? What did I do wrong?
I tried reducing the data for one of the special cases, but did not get very far.
Here's the working mini-example to produce the image, I'm sorry for the long dataset but could not figure out which values to remove while keeping the problem reproducable.
import numpy as np
from matplotlib import pyplot as plt
s = np.array([
[-0.08527125, 0.08810856],
[-0.08967261, -0.06748633],
[-0.08772675, -0.08396971],
[-0.08766724, -0.08440267],
[-0.08748191, -0.08521958],
[-0.08438602, -0.09529874],
[-0.08385044, -0.09684308],
[-0.08202714, -0.10180355],
[-0.07874478, -0.1066276 ],
[-0.07857811, -0.10686354],
[-0.07789635, -0.10778213],
[-0.07781094, -0.10789337],
[-0.07710836, -0.10880646],
[-0.07007289, -0.11655674],
[-0.06962708, -0.11703841],
[-0.06727917, -0.11933749],
[-0.06584873, -0.12070607],
[-0.06552574, -0.12100522],
[-0.06527846, -0.12121293],
[-0.06401669, -0.12214381],
[-0.06349801, -0.12245992],
[-0.06328962, -0.12258028],
[-0.0627093 , -0.12290901],
[-0.06225359, -0.12314782],
[-0.06116991, -0.12368258],
[-0.06041895, -0.12403051],
[-0.06017477, -0.12413955],
[-0.05992829, -0.12424804],
[-0.04659197, -0.13006904],
[-0.04634663, -0.13017319],
[-0.04628731, -0.1301966 ],
[-0.04567821, -0.13041967],
[-0.04552972, -0.13047215],
[-0.04521702, -0.13058213],
[-0.00325617, -0.14513102],
[ 0.00180445, -0.14683444],
[ 0.00950078, -0.14923653],
[ 0.01392647, -0.15030046],
[ 0.01518804, -0.15045976],
[ 0.02734024, -0.15177574],
[ 0.02813995, -0.15177997],
[ 0.02882764, -0.15176844],
[ 0.02947446, -0.15171012],
[ 0.03001744, -0.15165147],
[ 0.0309922 , -0.15149313],
[ 0.03121784, -0.15145567],
[ 0.031327 , -0.15143527],
[ 0.03142507, -0.15141573],
[ 0.03150791, -0.15139799],
[ 0.03222764, -0.15124224],
[ 0.03798838, -0.14988557],
[ 0.038991 , -0.1496481 ],
[ 0.0391266 , -0.14961472],
[ 0.03920601, -0.14959498],
[ 0.03938681, -0.14954736],
[ 0.03991586, -0.14940614],
[ 0.05465674, -0.14538513],
[ 0.05500815, -0.14528457],
[ 0.05512499, -0.14524203],
[ 0.05522656, -0.14520209],
[ 0.05558739, -0.14504381],
[ 0.05580602, -0.1449474 ],
[ 0.05609256, -0.14478439],
[ 0.05713959, -0.14412759],
[ 0.05743526, -0.14392656],
[ 0.05889567, -0.14277855],
[ 0.06059875, -0.14143159],
[ 0.06530732, -0.13766523],
[ 0.06933674, -0.13424303],
[ 0.0702827 , -0.13335664],
[ 0.07036637, -0.13327235],
[ 0.07405533, -0.12951652],
[ 0.07810863, -0.125367 ],
[ 0.08087981, -0.12226141],
[ 0.08176499, -0.12123622],
[ 0.08381519, -0.1150717 ],
[ 0.08734418, -0.0927558 ],
[ 0.08230393, 0.09131774],
[ 0.08183173, 0.097691 ],
[ 0.08169106, 0.09931977],
[ 0.08130204, 0.10177584],
[ 0.07914312, 0.11230088],
[ 0.07895476, 0.11316267],
[ 0.07514703, 0.12076318],
[ 0.07464758, 0.12166996],
[ 0.0733333 , 0.12382546],
[ 0.07297538, 0.12425848],
[ 0.06620658, 0.13199037],
[ 0.05294461, 0.14660768],
[ 0.05262742, 0.14694802],
[ 0.05097036, 0.14849098],
[ 0.04972845, 0.14937964],
[ 0.04815287, 0.15002114],
[ 0.04783434, 0.15013311],
[ 0.04757239, 0.15022502],
[ 0.02928349, 0.15634586],
[ 0.02842644, 0.15662035],
[ 0.02776939, 0.15677726],
[ 0.0270936 , 0.15690821],
[ 0.02666639, 0.15698384],
[ 0.02610376, 0.15707915],
[ 0.02601474, 0.15709266],
[ 0.025126 , 0.15722434],
[ 0.02474755, 0.15727606],
[ 0.02296123, 0.15742128],
[ 0.02202472, 0.15744494],
[ 0.02086636, 0.15744469],
[ 0.01967163, 0.15742308],
[ 0.01872141, 0.15737815],
[ 0.01568162, 0.15718458],
[-0.00722516, 0.15361993],
[-0.00785781, 0.15350418],
[-0.02865655, 0.14966376],
[-0.02928172, 0.1495445 ],
[-0.02970399, 0.14942282],
[-0.03124785, 0.14896281],
[-0.03160079, 0.14884731],
[-0.03311488, 0.14832349],
[-0.04643741, 0.14369722],
[-0.04831254, 0.14301643],
[-0.04846789, 0.14294296],
[-0.04902343, 0.14264339],
[-0.04980039, 0.14218488],
[-0.05127431, 0.14122099],
[-0.05343824, 0.13978482],
[-0.06641953, 0.1306598 ],
[-0.06719992, 0.13009146],
[-0.06732267, 0.12997761],
[-0.06842394, 0.12872382],
[-0.06895024, 0.12808815],
[-0.07854825, 0.11574519],
[-0.08365013, 0.10504 ],
[-0.0837306 , 0.10486896],
[-0.08384365, 0.10417084],
[-0.08444646, 0.09768441]])
# make first and last point the same, to close the circle
s = np.concatenate([s,s[:1]])
y = s[:,0]
x = s[:,1]
# plot the polygon xy
plt.plot(x, y, 'b--', label='Polygon with x/y')
# plot only x values of the polygon (does not work)
plt.plot(x, np.zeros(len(x)), 'g-', linewidth=10, label='does not stretch over red dots')
# do the same, but with dots to show x values (does work)
plt.plot(x, np.zeros(len(x)), 'r.')
# do the same, a little bit lower, but with shifted/rolled x values.
# rolled by 1 or 2 does not help, but by 3 or 4 does.
plt.plot(np.roll(x,3), np.ones(len(x))-1.02, '-', color="lightgreen", linewidth=10, label="stretches over red dots")
# do the same again with dots to show x values
plt.plot(np.roll(x,3), np.ones(len(x))-1.02, 'r.')
plt.legend(loc='lower center').get_frame().set_alpha(1)
plt.show()
Python 3.5 x64 via Anaconda in Windows10. Matplotlib is v2.0.0

How long is a swimming pool that is 0 mm wide? In theory, it has a length of course, but in practice you couldn't measure it. The same happens here, a polygon without extention in one dimension could potentially be rendered to any length. And you would see the same effect happening to both polygons depending on the figure size, zoom level, axis range.
I think you already found a solution, by plotting a line instead of a polygon. And that would also be my recommendation.

Related

Get column-wise maximums from a NumPy array

I have a 2D array, say
x = np.random.rand(10, 3)
array([[ 0.51158246, 0.51214272, 0.1107923 ],
[ 0.5210391 , 0.85308284, 0.63227215],
[ 0.57239625, 0.06276943, 0.1069803 ],
[ 0.71627613, 0.66454443, 0.56771438],
[ 0.24595493, 0.01007568, 0.84959605],
[ 0.99158904, 0.25034553, 0.00144037],
[ 0.43292656, 0.9247424 , 0.5123086 ],
[ 0.07224077, 0.57230282, 0.88522979],
[ 0.55665913, 0.20119776, 0.58865823],
[ 0.55129624, 0.26226446, 0.63070611]])
Then I find the indexes of maximum elements along the columns:
indexes = np.argmax(x, axis=0)
array([5, 6, 7])
So far so good.
But how do I actually get those elements? That is, how do I get ?some_operation?(x, indexes) == [0.99158904, 0.9247424, 0.88522979]?
Note that I need both the indexes and the associated values.
The best I could come up with was x[indexes, range(x.shape[1])], but it looks kinda complicated and inefficient. Is there a more idiomatic way?
You can use np.amax to find max value along an axis.
Using your example (x is the original array in your post):
In[1]: np.argmax(x, axis=0)
Out[1]:
array([5, 6, 7], dtype=int64)
In[2]: np.amax(x, axis=0)
Out[2]:
array([ 0.99158904, 0.9247424 , 0.88522979])
Documentation link

matplotlib pcolormesh returns Flat contour

Below is my input for the contour plot. Even if the values are different in z, it returns a plot with flat contours. I am using python3.
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import matplotlib.cm as cmx
import matplotlib as mpl
x = [8,10,12,14,16,18,20]
y = [ 0.4,0.45,0.5, 0.55, 0.6,0.65,0.7, 0.75,0.8,0.85,0.9,0.95,1.,1.05,1.1 ,1.15, 1.2 ]
z= [[ 1.78963157e-05, 1.61644497e-05, 1.82095363e-05, 1.68437063e-05, 1.72978900e-05, 1.32008829e-05, 1.34279038e-05],
[ -6.88334562e-07, -1.36603740e-06, -4.87142628e-05, -3.72777265e-04, -1.13885082e-03, -2.21202558e-03, -3.04815629e-03],
[ -2.75334480e-06, -1.69193652e-04, -3.32760366e-03, -1.41131639e-02, -3.22716385e-02, -5.08755872e-02, -6.43446161e-02] ,
[ -1.99621453e-05, -6.62870148e-03, -5.51200368e-02, -1.46310900e-01, -2.50479130e-01, -3.44686045e-01, -4.14010369e-01],
[ -8.05638699e-04, -6.86469566e-02, -3.09807186e-01, -5.95417818e-01, -8.70429863e-01, -1.10896694e+00, -1.28863817e+00],
[ -1.09028691e-02, -2.86631096e-01, -8.72346699e-01, -1.38425321e+00, -1.83269423e+00, -2.21171217e+00, -2.49614674e+00],
[ -5.44918164e-02, -7.04700599e-01, -1.61944488e+00, -2.23166225e+00, -2.70610186e+00, -3.05736645e+00, -3.31027451e+00],
[ -1.46581196e-01, -1.27933729e+00, -2.34440457e+00, -2.90545526e+00, -3.30654522e+00, -3.61596075e+00, -3.81523854e+00],
[ -2.83847825e-01, -1.94187773e+00, -2.97264260e+00, -3.53393575e+00, -3.91886829e+00, -4.39978913e+00, -4.40556012e+00],
[ -4.52972035e-01, -2.60277547e+00, -3.69173458e+00, -6.28054855e+00, -6.28057405e+00, -6.28058156e+00, -6.28059977e+00],
[ -6.52260330e-01, -3.24424411e+00, -6.28054559e+00, -4.23917726e+00, -3.53008773e+00, -3.57473901e+00, -3.51794282e+00],
[ -8.83887486e-01, -4.43537561e+00, -3.81221898e+00, -2.95280246e+00, -2.74353298e+00, -2.75130073e+00, -2.76171281e+00],
[ -1.14292285e+00, -6.28045385e+00, -2.91582775e+00, -2.43497586e+00, -2.34172630e+00, -2.35585837e+00, -2.33020785e+00],
[ -1.48005357e+00, -3.32913613e+00, -2.51281694e+00, -2.22591860e+00, -2.20713439e+00, -2.18431855e+00, -2.15543300e+00],
[ -1.95083193e+00, -2.84478852e+00, -2.30980115e+00, -2.17543461e+00, -2.18077741e+00, -2.15239738e+00, -2.12150557e+00],
[ -2.67504634e+00, -2.47989403e+00, -2.17826984e+00, -2.13298637e+00, -2.14346365e+00, -2.13308098e+00, -2.11940317e+00],
[ -6.27708545e+00, -2.24516401e+00, -2.07349772e+00, -2.04499101e+00, -2.07452620e+00, -2.07811441e+00, -2.08486007e+00]]
fig,ax = plt.subplots()
cmap=plt.cm.bwr
ax.pcolormesh(x, y, z, cmap=cmap, rasterized=True, norm=mpl.colors.Normalize(vmin=-6,vmax=0))
This is what I got:
enter image description here
Found it! I was creating the x list and it wasn't sorted. 8 was at the end of the list. So I was getting a flat plot. Now I sort x and y parameters and it's working. I hope this answer help other save some time:)

ValueError: Floating point image RGB values must be in the 0..1 range. while using matplotlib

I want to visualize weights of the layer of a neural network. I'm using pytorch.
import torch
import torchvision.models as models
from matplotlib import pyplot as plt
def plot_kernels(tensor, num_cols=6):
if not tensor.ndim==4:
raise Exception("assumes a 4D tensor")
if not tensor.shape[-1]==3:
raise Exception("last dim needs to be 3 to plot")
num_kernels = tensor.shape[0]
num_rows = 1+ num_kernels // num_cols
fig = plt.figure(figsize=(num_cols,num_rows))
for i in range(tensor.shape[0]):
ax1 = fig.add_subplot(num_rows,num_cols,i+1)
ax1.imshow(tensor[i])
ax1.axis('off')
ax1.set_xticklabels([])
ax1.set_yticklabels([])
plt.subplots_adjust(wspace=0.1, hspace=0.1)
plt.show()
vgg = models.vgg16(pretrained=True)
mm = vgg.double()
filters = mm.modules
body_model = [i for i in mm.children()][0]
layer1 = body_model[0]
tensor = layer1.weight.data.numpy()
plot_kernels(tensor)
The above gives this error ValueError: Floating point image RGB values must be in the 0..1 range.
My question is should I normalize and take absolute value of the weights to overcome this error or is there anyother way ?
If I normalize and use absolute value I think the meaning of the graphs change.
[[[[ 0.02240197 -1.22057354 -0.55051649]
[-0.50310904 0.00891289 0.15427093]
[ 0.42360783 -0.23392732 -0.56789106]]
[[ 1.12248898 0.99013627 1.6526649 ]
[ 1.09936976 2.39608836 1.83921957]
[ 1.64557672 1.4093554 0.76332706]]
[[ 0.26969245 -1.2997849 -0.64577204]
[-1.88377869 -2.0100112 -1.43068039]
[-0.44531786 -1.67845118 -1.33723605]]]
[[[ 0.71286005 1.45265901 0.64986968]
[ 0.75984162 1.8061738 1.06934202]
[-0.08650422 0.83452386 -0.04468433]]
[[-1.36591709 -2.01630116 -1.54488969]
[-1.46221244 -2.5365622 -1.91758668]
[-0.88827479 -1.59151018 -1.47308767]]
[[ 0.93600738 0.98174071 1.12213969]
[ 1.03908169 0.83749604 1.09565806]
[ 0.71188802 0.85773659 0.86840987]]]
[[[-0.48592842 0.2971966 1.3365227 ]
[ 0.47920835 -0.18186836 0.59673625]
[-0.81358945 1.23862112 0.13635623]]
[[-0.75361633 -1.074965 0.70477796]
[ 1.24439156 -1.53563368 -1.03012812]
[ 0.97597247 0.83084011 -1.81764793]]
[[-0.80762428 -0.62829626 1.37428832]
[ 1.01448071 -0.81775147 -0.41943246]
[ 1.02848887 1.39178836 -1.36779451]]]
...,
[[[ 1.28134537 -0.00482408 0.71610934]
[ 0.95264435 -0.09291686 -0.28001019]
[ 1.34494913 0.64477581 0.96984017]]
[[-0.34442815 -1.40002513 1.66856039]
[-2.21281362 -3.24513769 -1.17751861]
[-0.93520379 -1.99811196 0.72937071]]
[[ 0.63388056 -0.17022935 2.06905985]
[-0.7285465 -1.24722099 0.30488953]
[ 0.24900314 -0.19559766 1.45432627]]]
[[[-0.80684513 2.1764245 -0.73765725]
[-1.35886598 1.71875226 -1.73327696]
[-0.75233924 2.14700699 -0.71064663]]
[[-0.79627383 2.21598244 -0.57396138]
[-1.81044972 1.88310981 -1.63758397]
[-0.6589964 2.013237 -0.48532376]]
[[-0.3710472 1.4949851 -0.30245575]
[-1.25448656 1.20453358 -1.29454732]
[-0.56755757 1.30994892 -0.39370224]]]
[[[-0.67361742 -3.69201088 -1.23768616]
[ 3.12674141 1.70414758 -1.76272404]
[-0.22565465 1.66484773 1.38172317]]
[[ 0.28095332 -2.03035069 0.69989491]
[ 1.97936332 1.76992691 -1.09842575]
[-2.22433758 0.52577412 0.18292744]]
[[ 0.48471382 -1.1984663 1.57565165]
[ 1.09911084 1.31910467 -0.51982772]
[-2.76202297 -0.47073677 0.03936549]]]]
It sounds as if you already know your values are not in that range. Yes, you must re-scale them to the range 0.0 - 1.0. I suggest that you want to retain visibility of negative vs positive, but that you let 0.5 be your new "neutral" point. Scale such that current 0.0 values map to 0.5, and your most extreme value (largest magnitude) scale to 0.0 (if negative) or 1.0 (if positive).
Thanks for the vectors. It looks like your values are in the range -2.25 to +2.0. I suggest a rescaling new = (1/(2*2.25)) * old + 0.5

How do I create a surface plot with matplotlib of a closed loop revolve about an axis given coordinate data of the 2D profile?

I have the closed loop stored as a two column by N row numpy array.
The last row of the array is the same as the first row, implying that it is, indeed, a closed loop.
The number of angular divisions in the rotation (as in, "slices of pie" so to speak) ought be set by a variable called 'angsteps'
The profile in question is plotted in the x-y coordinate plane, and is rotated about the 'x-axis'.
You can find the profile in question plotted here. https://i.imgur.com/yJoKIEp.png
I apologize for the lack of code, but the profile data has so many interdependencies that I can't post the code that generates it without basically taking a shortcut to plugging the github page for it.
a downsampled version of the curve data looks like this.
bulkmat = [[ 5.2 0. ]
[ 0.381 0. ]
[ 0.381 3.164 ]
[ 2. 3.164 ]
[ 2. 4.1 ]
[ 3.78 4.1 ]
[ 3.78 6.477 ]
[ 1.898 6.477 ]
[ 1.898 7. ]
[ 3.18 7. ]
[ 3.18 9.6 ]
[ 1.898 9.6 ]
[ 1.898 9.6 ]
[ 2.31987929 12.42620027]
[ 3.4801454 15.24663923]
[ 5.22074074 17.97407407]
[ 7.38360768 20.521262 ]
[ 9.81068861 22.80096022]
[ 12.34392593 24.72592593]
[ 14.825262 26.20891632]
[ 17.09663923 27.16268861]
[ 19. 27.5 ]
[ 19. 27.5 ]
[ 19.62962963 27.44718793]
[ 20.18518519 27.29972565]
[ 20.66666667 27.07407407]
[ 21.07407407 26.7866941 ]
[ 21.40740741 26.45404664]
[ 21.66666667 26.09259259]
[ 21.85185185 25.71879287]
[ 21.96296296 25.34910837]
[ 22. 25. ]
[ 22. 25. ]
[ 21.12125862 24.17043472]
[ 18.91060645 23.59946824]
[ 15.97201646 22.9218107 ]
[ 12.84280513 21.85346069]
[ 9.96762011 20.14089993]
[ 7.67242798 17.51028807]
[ 6.13850192 13.61665735]
[ 5.37640942 7.99310742]
[ 5.2 0. ]]
The following would be an example of a solid of revolution plotted around the z axis. As input we take some points and then create the necessary 2D arrays from them.
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as axes3d
# input xy coordinates
xy = np.array([[1,0],[2,1],[2,2],[1,1.5],[1,0]])
# radial component is x values of input
r = xy[:,0]
# angular component is one revolution of 60 steps
phi = np.linspace(0, 2*np.pi, 60)
# create grid
R,Phi = np.meshgrid(r,phi)
# transform to cartesian coordinates
X = R*np.cos(Phi)
Y = R*np.sin(Phi)
# Z values are y values, repeated 60 times
Z = np.tile(xy[:,1],len(Y)).reshape(Y.shape)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')
ax2 = fig.add_axes([0.05,0.7,0.15,.2])
ax2.plot(xy[:,0],xy[:,1], color="k")
ax.plot_surface(X, Y, Z, alpha=0.5, color='gold', rstride=1, cstride=1)
plt.show()

transposing data in array using numpy

I have list as following and need to be tranposed to a numpy array
samplelist= [ [ ['Name-1','Name-2','Name-3'] , ['Age-1','Age-2','Age-3'] ],
[ ['new_Name_1','new_Name_2','new_Name_3'], ['new_Age_1','new_Age_2','new_Age_3'] ]
]
Expected Result:
samplearray = [ [ ['Name-1','Age-1'], ['Name-2','Age-2'], ['Name-3','Age-3'] ],
[ ['new_Name_1','new_Age_1], ['new_Name_2','new_Age_2'], ['new_Name_3','new_Age_3'] ]
]
np.transpose results:
np.transpose(a)
array([[['Name-1', 'new_Name_1'],
['Age-1', 'new_Age_1']],
[['Name-2', 'new_Name_2'],
['Age-2', 'new_Age_2']],
[['Name-3', 'new_Name_3'],
['Age-3', 'new_Age_3']]],
dtype='|S10')
samplelist is a 3-D array.
In [58]: samplelist.shape
Out[58]: (2, 2, 3)
Using transpose swaps the first and last axes (0 and 2):
In [55]: samplelist.T
Out[55]:
array([[['Name-1', 'new_Name_1'],
['Age-1', 'new_Age_1']],
[['Name-2', 'new_Name_2'],
['Age-2', 'new_Age_2']],
[['Name-3', 'new_Name_3'],
['Age-3', 'new_Age_3']]],
dtype='|S10')
In [57]: samplelist.swapaxes(0,2)
Out[57]:
array([[['Name-1', 'new_Name_1'],
['Age-1', 'new_Age_1']],
[['Name-2', 'new_Name_2'],
['Age-2', 'new_Age_2']],
[['Name-3', 'new_Name_3'],
['Age-3', 'new_Age_3']]],
dtype='|S10')
To get the desired array, swap axes 1 and 2:
import numpy as np
samplelist = np.array([
[ ['Name-1','Name-2','Name-3'] , ['Age-1','Age-2','Age-3'] ],
[ ['new_Name_1','new_Name_2','new_Name_3'], ['new_Age_1','new_Age_2','new_Age_3'] ]
])
print(samplelist.swapaxes(1,2))
# [[['Name-1' 'Age-1']
# ['Name-2' 'Age-2']
# ['Name-3' 'Age-3']]
# [['new_Name_1' 'new_Age_1']
# ['new_Name_2' 'new_Age_2']
# ['new_Name_3' 'new_Age_3']]]