Plotting 3D vector field with quiver3d() in Mayavi
I'm having a hard time to figure out why the following code isn't working inside the Jupyter Notebook! Any help is really appreciated
%gui qt
import matplotlib.pyplot as plt
import numpy as np
from sympy import symbols
from mayavi.mlab import quiver3d
x,y,z = symbols('x y z')
def gradient(f):
return (f.diff(x), f.diff(y),f.diff(z))
f = x*y**2+z**2
g = gradient(f)
xrange = np.linspace(3,3,15)
yrange = np.linspace(3,3,15)
zrange = np.linspace(3,3,15)
X,Y,Z = np.meshgrid(xrange, yrange, zrange)
U = np.zeros((15,15,15))
V = np.zeros((15,15,15))
W = np.zeros((15,15,15))
for i in range(len(xrange)):
for j in range(len(yrange)):
for k in range(len(zrange)):
x1 = X[i,j,k]
y1 = Y[i,j,k]
z1 = Z[i,j,k]
U[i,j,k] = g[0].subs({x:x1, y:y1, z:z1})
V[i,j,k] = g[1].subs({x:x1, y:y1, z:z1})
W[i,j,k] = g[2].subs({x:x1, y:y1, z:z1})
quiver3d(X,Y,Z,U,V,W)
In fact, I don't receive any error message just a black window of Mayavi. I have waited more than 10min but the windows permanents black. I'm running this code on a machine with Ubuntu 19.10. As a matter of completeness, I would like to mention that the below code works normally.
%gui qt
from mayavi import mlab
import numpy as np
def V(x, y, z):
""" A 3D sinusoidal lattice with a parabolic confinement. """
return np.cos(10*x) + np.cos(10*y) + np.cos(10*z) + 2*(x**2 + y**2 + z**2)
X, Y, Z = np.mgrid[2:2:100j, 2:2:100j, 2:2:100j]
mlab.contour3d(X, Y, Z, V)
See also questions close to this topic

Unable to go from on page to another using scrapy
I am working on a project for which I have to scrape a website "http://app.bmiet.net/student/login" after logging into it. I have successfully loged into the website however, after logging in I reach the dashboard i.e. http://app.bmiet.net/student/dashboard. I have to scrape data from http://app.bmiet.net/student/attendance. I am not sure how to go from the dashboard to the attendance link and thats what I need help for. I am still learning to use scrapy and so I am not sure if this can be done or not. Please Help me with my code and do tell me what my mistake was. The code is given below.
import scrapy from scrapy.http import FormRequest from scrapy.utils.response import open_in_browser class spidey(scrapy.Spider): name = 'spidyy' start_urls = [ 'http://app.bmiet.net/student/login' ] def parse(self, response): token = response.css('form input::attr(value)').extract_first() return FormRequest.from_response(response, formdata={ 'csrf_token' : token, 'username' : '//Username//', 'password' : '//password//' }, callback = self.start_scrapping) def start_scrapping(self, response): open_in_browser(response) all = response.css('.tablehover td') for x in all: att = x.css('td:nthchild(2)::text').extract() sub = x.css('td~ td+ td::text').extract() yield { 'Subject': sub, 'Status': att }
NOTE: Removed Username and Password for obvious reasons
This is what I get when I crawl my spider.
20200321 19:38:50 [scrapy.core.engine] INFO: Spider opened 20200321 19:38:50 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min) 20200321 19:38:50 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023 20200321 19:38:52 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://app.bmiet.net/robots.txt> (referer: None) 20200321 19:38:52 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://app.bmiet.net/student/login> (referer: None) 20200321 19:38:53 [scrapy.downloadermiddlewares.redirect] DEBUG: Redirecting (302) to <GET http://app.bmiet.net/student/dashboard> from <POST http://app.bmiet.net/student/stud entlogin> 20200321 19:38:54 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://app.bmiet.net/student/dashboard> (referer: http://app.bmiet.net/student/login) 20200321 19:38:55 [scrapy.core.engine] INFO: Closing spider (finished)

Python: How to create concatenated string from dictionary
I want to pass from a dictionary, for example:
defaultdict(int, {'A': 5, 'B': 4, 'C': 4})
to a list like this:
'A5B4C4'
Is there any short and clever way?

Pandas couple of questions
I have a task to do where I need to process 6.4k rows x 75 cols Excel sheet. I have to arrange the columns according to the recommendations, for eg.: Columns DE from input file will be merged into Column A in output file/spreadsheet. There will be 83 operations on whole columns performed. Some of the tasks to be performed:
 merging two columns into single one,
 replacing values in a column based on data in input file comparing them for eg.: Input File: Baltimore, Output file Games Shop BC
 Some computing, for eg.: Creating CustomAccountNumbers based on ClientID + some numbers
Hence my questions:
 Will it be better to do it step by step for eg.: >Load Columns DE into dataframe from input file merge them into into single column and save as Column A in output file/spreadsheet< OR >Load whole excel file into dataframe1, create empy dataframe2, perform operations and step by step filling dataframe2 and then save dataframe2 as new file/spreadsheet<
I find fast solution for merging columns but it's not working in current pandas version. Could someone write some explanation for it and help me updating it for current pandas verion please? It's not working because of df.ix() has been deprecated. I tried replacing it with df.iloc() but I couldnt make it work. Or maybe there's something better for merging task?
df[0].str.cat(df.ix[:, 1:].T.values, sep=' ')
For now I've created interface to read specific columns from Excel file based on user input and save them in correct order (I've chosen the first method (dealing with file step by step) but I don't know if it's the right way to do it) and I have stopped at the first task where I started to wonder if am I doing it correctly and I decided to ask for Your help. I know that I've made very long text but I don't have anyone to ask these questions. I will be grateful for everyone who replies. Alse if someone could point me at some tools for task 2 and 3 it would be great.
Best regards, Ed.

How to plot plotly graph inside kivy application?
I have this script in python where i plot several sensors' values and i want to display those plots inside a kivy application. How can this be done? Plotly opens a new browser tab and i want its graph to open inside this window:
The code for plotly graphs is below:
from plotly.subplots import make_subplots import plotly.graph_objects as go import pandas as pd from plotly.offline import plot csv_name = 'https://raw.githubusercontent.com/iulianastroia/csv_data/master/march_data.csv' data = pd.read_csv(csv_name) fig = make_subplots( rows=4, cols=2 ) for i in range(4, 11): fig.add_trace(go.Scatter( x=data[data.columns[12]], # day y=data[data.columns[i]], #sensor value name=data.columns[i]), #sensor name row=i // 2  1, col=i % 2 + 1 ) fig.update_layout(title="URADMONITOR data from March 2020", height=1800, xaxis=dict( tickmode='linear', tick0=0, # starting position dtick=1000 # tick step ) ) fig.show()

Is there an 'inverse' to the tilde function for plotting in R
If the starting data is:
[![df = data.frame(AAA=rnorm(1000,1,1), BBB=sample(LETTERS,1000,replace=TRUE)) df = droplevels(df\[df$BBB %in% unique(df$BBB)\[1:5\] == TRUE,\])][1]][1] # Not sure how to do this less dumb... # Just random number of values to 5 random letters
and in this case, I'm using package vioplot to plot:
vioplot(AAA~BBB, data = df, side = "right")
I would like to then plot on "side = left" all of the data not selected for each xaxis entry So if it was plotting on the right the values for df$BBB == "A", I would want it to plot on the left, the !(df$BBB == "A"). And so on for each letter.
Also just as a note, the number of xaxis levels is undefined. Ideally, I'd like to not install any packages.
My goal was to highlight, if present, the difference between sampled population vs remaining population using a split violin plot.
Thanks for any help
My workaround at the moment is (I'm still really clumsy with R):
Plotting_Vios_T < function(input_Data) { library(vioplot) vioplot(AAA~BBB, data = input_Data, side = "right") x = list() y = list() for(idx_class in 1:length(unique(input_Data$BBB))) { y_t < input_Data[!(input_Data$BBB == unique(input_Data$BBB)[idx_class]),"AAA"] x_t < rep(unique(input_Data$BBB)[idx_class], length(y_t)) x < c(x,x_t) y < c(y,y_t) } df < data.frame(x = as.character(x), y = as.numeric(y)) vioplot(y~x, data = df, side = "left", add = TRUE) }

fill space between graphs in Matplotlib
I need to fill the area between y, y1 and y2 but I am filling up the extra space under the y graph.How do I fix this?
import numpy as np import matplotlib.pyplot as plt y = lambda z: (2 * z  z ** 2) ** (1 / 2) y1 = lambda x: (6 * x  x ** 2) ** (1 / 2) y2 = lambda c: c x = np.linspace(0, 12, 500) z = np.linspace(0, 12, 500) c = np.linspace(0, 12, 500) plt.ylim(0, 4) plt.xlim(0, 4) plt.plot(z, y(z), color='blue', label="$y=\\sqrt{2xx^2}$") plt.plot(c, y2(c), color='black', label='$y=x$') plt.plot(x, y1(x), color='red', label='$y=\\sqrt{6xx^2}$') plt.plot([0, 4], [0, 0], color='yellow', label='y=0') plt.grid(True, zorder=5) plt.fill_between(x, np.minimum(y2(c), y1(x)), y1(x), alpha=0.5) plt.legend() plt.show()

how to use sympy solver to solve equation generated by corresponding elements from two numpy.array?
As I know, if the parameters in the equation are scalars, solving the equation above is implemented as:
from sympy.solvers import solve from sympy import Symbol from math import sin, cos, sqrt # ep, t, a are in short for epsilon, theta and alpha, respectively ep = Symbol('ep') a = 0.4798212489019141 t = 39.603733 solve(abs(a)abs((ep1)(sin(t)**2  ep*(1+sin(t)**2)/(ep*cos(t)+sqrt(epsin(t)**2))**2)), ep)
The question is, if these parameters are numpy.ndarray in 3 dimensions, how to implement this calculation? In my case, the shape of theta and alpha is (5,277,232). So the output, ep, should also have the same shape.

Sympy solve linear system of equations
I've create a linear system of equations in a for loop using sympy symbols and stored all equations in a numpy array. In the present moment I'm using sympy.linear_eq_to_matrix to "transform" the equations in to a matrix and solve then. But, this process using sympy.linear_eq_to_matrix is taking a long time when I've a lot of variables. So, to avoid sympy.linear_eq_to_matrix, what is the best alternative ? Can I solve the system without pass through sympy.linear_eq_to_matrix ? I'm just writing a simple script.
## I've imported all libs. comp = 5 h = 0.1 nodes = int(((comp/h)+3)) a = np.array(sp.symbols("w0:"+str(nodes**2))) w = np.reshape(a, (nodes,nodes)) equations = np.array([]) ## In this interval I've some for loop to apply boundary conditions. for i in range(2, (nodes2)): for j in range(2, (nodes2)): eq = Eq(5*(w[i,j]) + w[i,j2]), 0.1) ## This equation is more complex, I reduced. equations = np.append(equations, eq) hg = np.array([]) ## hg = just some variables to solve. for i in range(2, (nodes2)): for j in range(2, (nodes2)): hg = np.append(hg, w[i,j]) system, equality =sp.linear_eq_to_matrix(equations, hg) ## THE PROBLEMATIC PART system_array = np.array(system).astype(np.float64) system_equality = np.array(equality).astype(np.float64) ans = np.linalg.solve(system_array, system_equality) print("\nAnswer") print(ans)

sympy: rootfinding of parameterized function in range
I'm having trouble to find the root of a parameterized quintic polynome. Background is: I want to find the parameter
s_f
such that for any given parameterd_f
, the curvature of the polynomial is smaller than a threshold (yeah .. sounds complex, but the math is rather straight);# define quintic polynomial (jerkminimized trajectory) # see http://courses.shadmehrlab.org/Shortcourse/minimumjerk.pdf s = symbols('s', real=True, positive=True) s_f = symbols('s_f', real=True, positive=True, nonzero=True) d_0 = 0 d_f = symbols('d_f', real=True, positive=True, nonzero=True) d_of_s = d_0 + (d_f  d_0) * ( 10*(s/s_f)**3  15*(s/s_f)**4 + 6*(s/s_f)**5 ) display(d_of_s) # define curvature of d_of_s # see https://en.wikipedia.org/wiki/Curvature#In_terms_of_a_general_parametrization y = d_of_s dy = diff(d_of_s, s) ddy = diff(dy, s) x = s dx = diff(x, s) # evaluates to 1 ddx = diff(dx, s) # evaluates to 0 k = (dx*ddy  dy*ddx) / ((dx*dx + dy*dy)**Rational(3,2)) # the goal is to find s_f for any given d_f, such that k(s) < some_threshold # strategy: find the roots of the derivative of k in the range of s∈[0, s_f] dk = diff(k, s) dk = simplify(dk) display(dk) # now solve res = solveset(dk, s, Interval(0, s_f).intersection(S.Reals)) display(res)
The function
dk(s, d_f, s_f)
has two root in the interval s∈[0, s_f], howeversolveset
returns this:ConditionSet(s, Eq(5400*d_f**2*s**4*(s**2 + 2*s*s_f  s_f**2)*(2*s**2  3*s*s_f + s_f**2)**2 + (900*d_f**2*s**4*(s**2  2*s*s_f + s_f**2)**2 + s_f**10)*(6*s**2  6*s*s_f + s_f**2), 0), Interval(0, s_f))
.. wich is afaik equivalent to: I can't solve this, we got infinite number of results. Well, this is true for the function in general.
limit(dk, s, oo)
andlimit(dk, s, +oo)
is zero. But since I stated the domain interval, why am I not getting the two roots I'm expecting? I'd also expect to get a more granular result:  set containing the roots for s < 0;  set containting the roots for s > s_f  the two roots when s∈[0, s_f]I started with solve() and a lot of different assumptions on my symbols. I get different results for different assumptions, but no combination seems to yield what I need. When I state no assumptions, I get back a set with a huge condition and 8 roots, that don't seem real or correct. In general, the constraints are:
 all symbols are real  s_f > 0  d_f > 0  s ∈ [0, s_f] (domain range .. the polynomial is only evaluated in this interval)
I guess the problem is that I'm not setting up my solveset correctly:
 how to specify that s_f and d_f are real? afaik the symbol assumptions are ignored when using solveset?
 how to specify intervals and assumptions on other multivariate functions, i.e. other symbols than the domain one?
This is what d_of_s look like for s_f = 1, d_f = 1
And this is what dk(s) looks like (I plotted outside the domain range to visualize the problem).
 How to plot pseudo3D images like that with Python

mayavi wx embedding error with latest libraries
With the latest version of traitsui, the standard mayavi wx embedding example does not function correctly anymore: http://docs.enthought.com/mayavi/mayavi/auto/example_wx_embedding.html
The error is:
NotImplementedError: the null tvtk.pyface.ui.null backend doesn't implement scene_editor:SceneEditor
The library versions on my system (Ubuntu 18.04) are:
 traits: 6.0.0
 traitsui: 6.1.3
 mayavi: 4.7.1
 envisage: 4.9.2
 vtk: 8.1.2
 wx: 4.0.1
Any idea how this can be solved?

Drawing spheres in Mayavi giving empty plot
I'm trying to visualize 10000 spheres, each with center position and radius data.
I did come across this helpful link and followed it throught: draw many spheres efficiently
import pandas as pd from numpy import cos,sin,cross,pi,dot,sqrt,power import numpy as np from mayavi import mlab phi, theta = np.mgrid[0:pi:12j, 0:2 * pi:12j] surface_x=sin(phi)*cos(theta) surface_y=sin(phi)*sin(theta) surface_z=cos(phi) def sphere(x,y,z,r): return mlab.mesh(r*surface_x+x,r*surface_y+y,r*surface_z+z,color=(0.5,0.5,0.5)) def read_file(file): f=pd.read_csv(file,sep='\s+') return f file=read_file('Input file') mlab.figure(1, bgcolor=(1, 1, 1), fgcolor=(0, 0, 0), size=(400, 300)) for i in range(6): sphere(file.x[i],file.y[i],file.z[i],file.r[i]) mlab.show()
However, I only ended up with empty figures, so is there something I'm doing wrong?