I am trying to show displacement on a 3D truss example however I am running into an error.I have simplified my code below.I am able to show displacement on a 2D problem however I am unable on a 3D problem.I am also trying to show the node numbers at each node.I managed to put the nodes(green color) however the numbers are not showing even after i used the "plt.annotate" command.Can someone help me get the displacement and node numbers to show?Thank you in advance.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import sys
np.set_printoptions(threshold=sys.maxsize)
def plot_truss(nodes, elements, areas,forces):
# plot nodes in 3d
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = [i[0] for i in nodes.values()]
y = [i[1] for i in nodes.values()]
z = [i[2] for i in nodes.values()]
# size = 400
# ax.scatter(x, y, z, c='r', marker='o', s=size, zorder=5)
size = 400
offset = size / 4000
ax.scatter(x, y, z, c='y', s=size, zorder=5)
for i, location in enumerate(zip(x, y, z)):
plt.annotate(i + 1, (location[0] - offset, location[1] - offset), zorder=10)
# plot elements in 3d
for element in elements:
fromPoint = np.array(nodes[elements[element][0]])
toPoint = np.array(nodes[elements[element][1]])
x1 = fromPoint[0]
y1 = fromPoint[1]
z1 = fromPoint[2]
x2 = toPoint[0]
y2 = toPoint[1]
z2 = toPoint[2]
ax.plot([x1, x2], [y1, y2], zs=[z1, z2], c='b', linestyle='-', linewidth=5*areas[element], zorder=1)
nodes = {1: [0, 10, 0], 2: [0, 0, 0], 3: [10, 5, 0], 4: [0, 10, 10]}
areas = {1: 1.0, 2: 2.0, 3: 2.0}
elements = {1: [1, 3], 2: [2, 3], 3: [4, 3]}
forces = {1: [0, 0, 0], 2: [0, 0, 0], 3: [0, -200, 0], 4: [0, 0, 0]}
disps = {1: [0, 0, 0], 2: [0, 0, 0], 3: [ 3, -2, 4], 4: [0, 0, 0]}
def plt_displacement(nodes,elements,disps color="red"):
nodes_disp = np.copy(nodes)
nodes_disp[:, 0] += disp[::2, 0]
nodes_disp[:, 1] += disp[1::2, 0]
plt.scatter(nodes_disp[:, 0], nodes_disp[:, 1], color=color)
for e in elements:
x_tmp = [nodes_disp[e[0], 0], nodes_disp[e[1], 0]]
y_tmp = [nodes_disp[e[0], 1], nodes_disp[e[1], 1]]
plt.plot(x_tmp, y_tmp, color=color)
plt_displacement(nodes,elements,disps)
plot_truss(nodes, elements, areas, forces)
plt.show()
when i run the code I am getting the error below;
<ipython-input-47-758895b259be> in plt_displacement(elements, nodes, disp, color)
31 def plt_displacement(elements, nodes, disp, color="red"):
32 nodes_disp = np.copy(nodes)
---> 33 nodes_disp[:, 0] += disp[::2, 0]
34 nodes_disp[:, 1] += disp[1::2, 0]
35 plt.scatter(nodes_disp[:, 0], nodes_disp[:, 1], color=color)
IndexError: too many indices for array
It looks like you may have switched “nodes” and “elements” in your call to plt_displacement() (3rd and 12th to last lines) vs your definition.
plt_displacement(nodes,elements,disps)
def plt_displacement(elements, nodes, disp, color="red"):
I’m not sure exactly what plt_displacement is supposed to do. But looking at nodes_disp it is an array of no shape, so slicing won’t work.
>>> nodes_disp = np.copy(nodes)
>>> nodes_disp
array({1: [0, 10, 0], 2: [0, 0, 0], 3: [10, 5, 0], 4: [0, 10, 10]}, dtype=object)
>>> nodes_disp.shape
()
You can change the values to an array and slice it like this:
>>> npdisp = np.copy(list(disps.values()))
>>> nodes_disp
array([[ 0, 10, 0],
[ 0, 0, 0],
[10, 5, 0],
[ 0, 10, 10]])
But I’m not sure if that’s your intent.
Like wise you’d have to change the type of disp to an array in order to slice it, as it is a dictionary
Related
I'm having hard time to draw this... Can someone help me please
Make linspaces of grid_resolution points in xlim and grid_resolution points in ylim. e.g. For xlim=(-1, 1), ylim=(0, 2) and grid_resolution=3, make the linspace (-1, 0, 1) of x coordinates and the linspace (0, 1, 2) of y coordinates.
Use np.tile() to repeat the x grid points grid_resolution times (e.g. (-1, 0, 1, -1, 0, 1, -1, 0, 1)) and np.repeat() to repeat each of the y grid points grid_resolution times (e.g. (0, 0, 0, 1, 1, 1, 2, 2, 2)).
Use np.stack() to combine the x grid points and y grid points into a 2D array of size grid_resolution2 x 2. (e.g. [[-1, 0], [0, 0], [1, 0], [-1, 1], [0, 1], [1, 1], [-1, 2], [0, 2], [1, 2]] )
Make a dictionary keyed by -1 and 1 with values 'pink' and 'lightskyblue'.
Use clf.predict() on the 2D array of points to get predicted y values.
6.For each y in {-1, 1}, use plt.plot() to plot those points in your 2D array with that predicted y value in the color specified by your dictionary.
above is the requirements
def plot_decision_boundary(clf, xlim, ylim, grid_resolution):
"""Display how clf classifies each point in the space specified by xlim and ylim.
- clf is a classifier (already fit to data).
- xlim and ylim are each 2-tuples of the form (low, high).
- grid_resolution specifies the number of points into which the xlim is divided
and the number into which the ylim interval is divided. The function plots
grid_resolution * grid_resolution points."""
below are the test code
data_string = """
x0, x1, y
0, 0, -1
-1, 1, -1
1, -1, -1
0, 1, 1
1, 1, 1
1, 0, 1
"""
df = pd.read_csv(StringIO(data_string), sep='\s*,\s+', engine='python')
clf = svm.SVC(kernel="linear", C=1000)
clf.fit(df[['x0', 'x1']], df['y'])
# Call student's function.
plot_decision_boundary(clf=clf, xlim=(-4, 4), ylim=(-4, 4), grid_resolution=100)
# Add training examples to plot.
colors = {-1:'red', 1:'blue'}
for y in (-1, 1):
plt.plot(df.x0[df.y == y], df.x1[df.y == y], '.', color=colors[y])
I'm plotting 100K+ cylinders which are added to a GLViewWidget like this:
openGlWidget = GLViewWidget()
points = np.array([
[0, 1, 2],
[0, 1, 5],
[4, 1, 6],
[4, 1, 10]
])
cyl = MeshData.cylinder(6, 6, radius=[.1, .1])
for i in range(0, len(points), 2):
mesh = GLMeshItem(meshdata=cyl)
p1, p2 = points[i], points[i+1]
v = p2 - p1
theta = np.arctan2(v[1], v[0])
phi = np.arctan2(np.linalg.norm(v[:2]), v[2])
tr = Transform3D()
tr.translate(*p1)
tr.rotate(theta * 180 / np.pi, 0, 0, 1)
tr.rotate(phi * 180 / np.pi, 0, 1, 0)
tr.translate(0, 0, 1)
tr.scale(1, 1, np.linalg.norm(v1))
mesh.setTransform(tr)
self.openGlWidget.addItem(mesh)
This of course gives low fps when rotating/paning and I assume a good part of it has to do with adding that many singular items. So, I started thinking about the possibility of combining the cylinders into one item, is this possible? And/or should I be using something else than the GLViewWidget?
I have many 2d sequences with variable length, i.e. lists of list where each sublist is a sequence. I want to project these sequences/lines/sublists in a 3d visualisation adding time-step as another dimension. So far I am failing to plot all the 3d lines using plotly.express.
import plotly.express as px
t = [[ii+1 for ii in range(len(features[i]))] for i in range(len(labels))]
x0 = [[x[0] for x in features[i]] for i in range(len(labels))]
x1 = [[x[1] for x in features[i]] for i in range(len(labels))]
df = pd.DataFrame(dict(
X=[tii for ti in t for tii in ti],
Y=[xii for xi in x0 for xii in xi],
Z=[xii for xi in x1 for xii in xi],
color=[aa for a in labels for aa in a]
))
fig = px.line_3d(df, x="X", y="Y", z="Z", color="color")
fig.show
This is what I get, which is not really what I want. It is treating all the cases/sublists with common label as one single sequence, thus we see at the end of each line it goes back the where it starts. I have looked up on how to iteratively plotting this in a for-loop (just like matplotlib) (basically creating a new pandas dataframe at each iteration and plot it), however with no success. Does anyone have any experience on this please? Much appreciated!
A mcve is as below:
import plotly.express as px
import numpy as np
import pandas as pd
features = [np.random.rand(4,2).tolist(),
np.random.rand(5,2).tolist(),
np.random.rand(6,2).tolist(),
np.random.rand(5,2).tolist(),
np.random.rand(9,2).tolist()]
labels = [[1, 1, 1, 1], [1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2],
[2, 2, 2, 2, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0]]
t = [[ii+1 for ii in range(len(features[i]))] for i in range(len(labels))]
x0 = [[x[0] for x in features[i]] for i in range(len(labels))]
x1 = [[x[1] for x in features[i]] for i in range(len(labels))]
df2 = pd.DataFrame(dict(
X=[tii for ti in t for tii in ti],
Y=[xii for xi in x0 for xii in xi],
Z=[xii for xi in x1 for xii in xi],
color=[aa for a in labels for aa in a]
))
fig1 = px.line_3d(df2, x="X", y="Y", z="Z", color="color")
fig1.show()
You see basically 3 lines instead of 5.
Your problems is that you are using the same label for different traces. Here a workaround with a loop
import numpy as np
import plotly.graph_objs as go
features = [np.random.rand(4,2).tolist(),
np.random.rand(5,2).tolist(),
np.random.rand(6,2).tolist(),
np.random.rand(5,2).tolist(),
np.random.rand(9,2).tolist()]
labels = [[1, 1, 1, 1],
[1, 1, 1, 1, 1],
[2, 2, 2, 2, 2, 2],
[2, 2, 2, 2, 2],
[0, 0, 0, 0, 0, 0, 0, 0, 0]]
fig = go.Figure()
for i, feat in enumerate(features):
feat = np.array(feat)
fig.add_trace(
go.Scatter3d(
x=np.arange(len(feat)),
y=feat[:,0],
z=feat[:,1],
mode='lines',
hovertext=labels[i]
)
)
fig.show()
You might need to play with trace names.
Update
Hoefully it's not too overcomplicated but it is meant to be as generic as possible
import numpy as np
import plotly.graph_objs as go
from itertools import cycle
def plotly_color_map(names):
# From https://stackoverflow.com/a/44727682
plotly_colors = cycle(['#1f77b4', # muted blue
'#ff7f0e', # safety orange
'#2ca02c', # cooked asparagus green
'#d62728', # brick red
'#9467bd', # muted purple
'#8c564b', # chestnut brown
'#e377c2', # raspberry yogurt pink
'#7f7f7f', # middle gray
'#bcbd22', # curry yellow-green
'#17becf' # blue-teal
])
return dict(zip(names, plotly_colors))
features = [np.random.rand(4,2).tolist(),
np.random.rand(5,2).tolist(),
np.random.rand(6,2).tolist(),
np.random.rand(5,2).tolist(),
np.random.rand(9,2).tolist()]
labels = [[1, 1, 1, 1],
[1, 1, 1, 1, 1],
[2, 2, 2, 2, 2, 2],
[2, 2, 2, 2, 2],
[0, 0, 0, 0, 0, 0, 0, 0, 0]]
legend_groups = [l[0] for l in labels]
traces = [False if (len(legend_groups[:i])>0 and l in legend_groups[:i])
else True for i, l in enumerate(legend_groups)]
cm = plotly_color_map(set(legend_groups))
fig = go.Figure()
for i, feat in enumerate(features):
feat = np.array(feat)
fig.add_trace(
go.Scatter3d(
x=np.arange(len(feat)),
y=feat[:,0],
z=feat[:,1],
mode='lines',
line={"color":cm[legend_groups[i]]},
legendgroup=legend_groups[i],
hovertext=labels[i],
showlegend=traces[i],
name="label_{}".format(legend_groups[i])
)
)
fig.show()
I'm trying to draw a contour plot using two 2D arrays (both has the shape (6,1) as x and y and my third data (z) is the calculated RMS between the x vector (x list has just 6 items) and y vector (y list has 600 items). RMS value is a scalar for each point. I mean I have one RMS value for each point. when I try to draw a contour plot using x, y, z the following error arises:
TypeError: Input z must be a 2D array
How to solve this problem?
Thanks in advance
import numpy
import matplotlib.pyplot as plt
from math import sqrt
def Cumulative_Sensitivity (depth, coil_spacing, coil_position):
global z
global s
z=numpy.array(depth)
s=numpy.array(coil_spacing)
if coil_position == "hcp":
cs=(4*(z/s)**2+1)**(-0.5)
cs=numpy.array(cs)
elif coil_position == "vcp":
cs=(4*(z/s)**2+1) **(0.5)-2*(z/s)
cs=numpy.array(cs)
return cs
def forward_model (sigma, depth, coil_spacing, coil_position):
global cs
global cond_true
global cond_apps
cond_true=numpy.array(sigma)
cond_apps=numpy.zeros(len(coil_spacing))
for i in range (0, len(coil_spacing)):
cs= Cumulative_Sensitivity (depth, coil_spacing[i], coil_position)
cond_app = sum(cond_true[:-1]*(cs[:-1]-cs[1:]))
cond_app = cond_app + cond_true[-1]*(cs[-1])
cond_apps[i] = cond_app
return cond_apps
# s1=30mS/m and s2=50mS/m, z=1m
sa_1=forward_model ([30, 50], [0, 1], [0.32], "hcp")
sa_2=forward_model ([30, 50], [0, 1], [0.71], "hcp")
sa_3=forward_model ([30, 50], [0, 1], [1.18], "hcp")
sa_4=forward_model ([30, 50], [0, 1], [0.32], "vcp")
sa_5=forward_model ([30, 50], [0, 1], [0.71], "vcp")
sa_6=forward_model ([30, 50], [0, 1], [1.18], "vcp")
data=numpy.array([sa_1, sa_2, sa_3, sa_4, sa_5, sa_6])
#
for i in range (10,110, 10):
for j in range (10,110, 10):
cond_HC1=forward_model ([j, i], [0, 1], [0.32], "hcp")
cond_HC2=forward_model ([j, i], [0, 1], [0.71], "hcp")
cond_HC3=forward_model ([j, i], [0, 1], [1.18], "hcp")
cond_VC1=forward_model ([j, i], [0, 1], [0.32], "vcp")
cond_VC2=forward_model ([j, i], [0, 1], [0.71], "vcp")
cond_VC3=forward_model ([j, i], [0, 1], [1.18], "vcp")
predicted=numpy.array([cond_HC1, cond_HC2, cond_HC3, cond_VC1, cond_VC2, cond_VC3])
rms=numpy.array(sqrt(sum((predicted - data)**2).mean()))
x = numpy.linspace(10,100,10)
y = numpy.linspace(10,100,10)
X,Y=numpy.meshgrid(x,y)
Z=rms
contour = plt.contour(X, Y, Z)
This is an example dataframe:
import pandas as pd
import numpy as np
values = np.array([
[0, 1, 2, 0, 0, 4],
[1, 0, 0, 1, 1, 0 ],
[0, 4, 0, 0, 2, 1],
[2, 0, 2, 0, 4, 0],
])
indexes= 'a','b','c','d'
columns='ab','bc','cd','de','ef','fg'
df = pd.DataFrame(index=indexes,columns=columns, data=values)
print(df)
from this dataframe I need to create a series of pie charts, one for every column, shown on the same figure, where the slices dimension is fixed (equal to 100/len(indexes)) and the color of the slices depends on the value of the index, in particular: white if 0, green if 1, yellow if 2, red if 4.
What suggestions can you give me?
I found that:
df.plot(kind='pie', subplots=True, figsize=(len(columns)*2, 2))
it creates a series, but I can't control the input values...
I've created a pie for a column, but then I wasn't able to link the color to the value of index:
labels = indexes
sizes = np.linspace(100/len(labels),100/len(labels), num=len(labels))
fig1, ax1 = plt.subplots()
ax1.pie(sizes, labels=labels)
ax1.axis('equal')
plt.show()
ImportanceOfBeingErnest answer has helped me giving to the piechart the wanted look:
fig1, ax1 = plt.subplots()
labels = indexes
sizes = np.linspace(100/len(labels),100/len(labels), num=len(labels))
coldic = {0 : "w", 1 : "g", 2 : "y", 4 : "r" }
colors = [coldic[v] for v in values[:,0]]
ax1.pie(sizes, labels=labels, colors=colors,counterclock=False, startangle=90)
ax1.axis('equal')
plt.show()
Now the colors a linked to the values, and the dimensions of the slices are fixed. I just need to have the same pie chart for all the columns and in the same image.
The importance of these charts is given by the colors, not the dimensions of the slices, which I want to be always equal.
Thanks for your time!
Not relying on pandas internal plotting functions (which are of course limited) one can use matplotlib' pie function to plot the diagrams.
The colors can be set as a list, which is generated from the values according to some mapping dictionary.
import numpy as np
import matplotlib.pyplot as plt
coldic = {0 : "w", 1 : "g", 2 : "y", 4 : "r" }
values = np.array([
[0, 1, 2, 0, 0, 4],
[1, 0, 0, 1, 1, 0 ],
[0, 4, 0, 0, 2, 1],
[2, 0, 2, 0, 4, 0],
])
labels= ['a','b','c','d']
fig1, axes = plt.subplots(ncols=values.shape[1], )
for i in range(values.shape[1]):
colors = [coldic[v] for v in values[:,i]]
labs = [l if values[j,i] > 0 else "" for j, l in enumerate(labels)]
axes[i].pie(values[:,i], labels=labs, colors=colors)
axes[i].set_aspect("equal")
plt.show()
For fixed wedge sizes you just use a fixed array to supply to pie.
import numpy as np
import matplotlib.pyplot as plt
coldic = {0 : "w", 1 : "g", 2 : "y", 4 : "r" }
values = np.array([
[0, 1, 2, 0, 0, 4],
[1, 0, 0, 1, 1, 0 ],
[0, 4, 0, 0, 2, 1],
[2, 0, 2, 0, 4, 0],
])
labels= ['a','b','c','d']
fig1, axes = plt.subplots(ncols=values.shape[1], )
for i in range(values.shape[1]):
colors = [coldic[v] for v in values[:,i]]
axes[i].pie(np.ones(values.shape[0]), labels=labels, colors=colors,
wedgeprops=dict(linewidth=1, edgecolor="k"))
axes[i].set_aspect("equal")
axes[i].set_title("".join(list(map(str,values[:,i]))))
plt.show()