I want to draw a 2d histogram with python. I found following example on the matplotlib wiki
from pylab import *
x = randn(1000)
y = randn(1000)+5
#normal distribution center at x=0 and y=5
hist2d(x,y,bins=40)
show()
But i get the error
> Traceback (most recent call last): File "./hist2d_demo.py", line 9,
> in <module>
> hist2d(x,y,bins=40) NameError: name 'hist2d' is not defined
Anyone has an idea how i can fix that?
I just copypasted your code sample in my python 2.7.6 and it works perfectly.
I also have numpy 1.8.0, scipy 0.9.0 and matplotlib 1.3.0 installed.
Your version of matplotlib may be too old, check it this way :
import matplotlib
print matplotlib.__version__
Related
I'm learning how to use qiskit and I'm using the jupyter notebook, but everytime I try to visualize the circuit with the attribute draw I get this error:
import qiskit
from qiskit import *
from qiskit import IBMQ
qr = QuantumRegister(2)
cr = ClassicalRegister(2)
circuit = QuantumCircuit(qr, cr)
%matplotlib inline
circuit.draw(output='mpl')
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-8-bd220039ee1c> in <module>
----> 1 circuit.draw(output='mpl')
AttributeError: module 'qiskit.circuit' has no attribute 'draw'
I also try applying a Hadamard gate and I get:
circuit.h(qr(0))
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-59-c8b4318b743b> in <module>
----> 1 circuit.h(qr(0))
AttributeError: module 'qiskit.circuit' has no attribute 'h'
It seems that there is a name conflict. It is taking the circuit in from qiskit import circuit instead of circuit = ....
You just probably need to restat your notebook kernel.
Try another name for your circuit variable, right now python thinks you want the qiskit.circuit module to draw something. QuantumCircuit objects are the ones that have a draw method. You can see these two objects here if you call both, note I put one qubit and classical bit in the QuantumCircuit just per example as well you do not need the dots here it is just to make it more clear, just running circuit and QuantumCircuit(1,1) respectively would yield the same result.
You would get desired results if you tried a different variable name:
When I try using the variable name circuit it works for me, but try to use descriptive variable names that also could never be confused with modules or classes from the packages you import.
Also all your import statements can be combined into 1:
from qiskit import *
The star lets you import everything from qiskit including IBMQ. It can help you save a line or two.
I am trying to use open3d to create an "alphahull" around a set of 3d points using TriangleMesh. However I get a TypeError.
import open3d as o3d
import numpy as np
xx =np.asarray([[10,21,18], [31,20,25], [36,20,24], [33,19,24], [22,25,13], [25,19,24], [22,26,10],[29,19,24]])
cloud = o3d.geometry.PointCloud()
cloud.points = o3d.utility.Vector3dVector(xx)
mesh = o3d.geometry.TriangleMesh.create_from_point_cloud_alpha_shape(pcd=cloud, alpha=10.0)
output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: create_from_point_cloud_alpha_shape(): incompatible function arguments. The following argument types are supported:
1. (pcd: open3d.open3d.geometry.PointCloud, alpha: float, tetra_mesh: open3d::geometry::TetraMesh, pt_map: List[int]) -> open3d.open3d.geometry.TriangleMesh
The error says the object I am passing the function is the wrong type. But when I check the type I get this:
>>print(type(cloud))
<class 'open3d.open3d.geometry.PointCloud'>
Please can someone help me with this error?
Note: A comment on this post Python open3D no attribute 'create_coordinate_frame' suggested it might be a problem with the installation and that a solution was to compile the library from source. So I compiled the library from source. After this ran
make install-pip-package. Though I am not sure it completed correctly because I couldn't import open3d in python yet; see output of installation: https://pastebin.com/sS2TZfTL
(I wasn't sure if that command was supposed to complete the installation, or if you were still required to run pip? After I ran python3 -m pip install Open3d I could import the library in python.)
Bug in bindings of current release (v0.9.0):
https://github.com/intel-isl/Open3D/issues/1621
Fix for master:
https://github.com/intel-isl/Open3D/pull/1624
Workaround:
tetra_mesh, pt_map = o3d.geometry.TetraMesh.create_from_point_cloud(pcd)
mesh = o3d.geometry.TriangleMesh.create_from_point_cloud_alpha_shape(pcd, alpha, tetra_mesh, pt_map)
I am working with networkx library for graph optimization problems. However, when I try running the example on their documentation it says in my PyCharm IDE after executing the example:
Traceback (most recent call last):
File "/home/PycharmProjects/testing_things.py", line 1, in <module>
import community
ImportError: No module named community
Does anyone knows how to get rid of this error? I am using Python 2.7
It would seem that your python installation doesn't have community installed.
You can install it by running:
pip install python-louvain
Cheers!
you can use :
conda install python-louvain
Use pip to install Python-Louvain:
pip install python_louvain
then in your script import the module directly using:
from community import community_louvain
In your code, use the function in the following way:
partition = community_louvain.best_partition(G)
Here's the sample community detection on the famous karate club graph based on Louvain Community Detection Algorithm:
# Replace this with your networkx graph loading depending on your format!
r = nx.karate_club_graph()
#first compute the best partition
partition = community.best_partition(r)
#drawing
size = float(len(set(partition.values())))
pos = nx.spring_layout(r)
count = 0
for com in set(partition.values()) :
count = count + 1.
list_nodes = [nodes for nodes in partition.keys()
if partition[nodes] == com]
nx.draw_networkx_nodes(r, pos, list_nodes, node_size = 20,
node_color = str(count / size))
nx.draw_networkx_edges(r, pos, alpha=0.5)
plt.show()
I am plotting with matplotlib and trying to get a nice layout like plotly. Especially I would like to have a bar chart/box plot with filling and contour in the same colour. Not as default black/grey contour and coloured contour but like plotly. Any suggestions without stylesheet?
The following is solved:
Because by searching for the solution I found style sheets, but I get the following error message trying to load specific stylesheets in python matplotlib following the official manual:
>>> import matplotlib.pyplot as plt
>>> plt.style.use('ggplot')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'style'
same problem with:
>>> print plt.style.available
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'style'
Information about the system:
>>> matplotlib.__version__
'1.3.1'
So it seems that something is wrong with my installation, but what can I do? Any suggestions?
As shown in the release notes, the style package was added in matplotlib 1.4. You will need to upgrade if you want to use that.
I'm using spyder and have written the following class:
class Ray:
def __init__(self, r, p, k):
if r.shape == (3,):
self.r = r
if p.shape == (3,):
self.p = p
if k.shape == (3,):
self.k = k
r = array(range(3))
p = array(range(3))
k = array(range(3))
It is stored in /home/user/workspace/spyder/project and the console working directory is that one. In the console I can run an array(range(3)) and it returns an array with values 0,1,2. However when doing
import ray
I get the following error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "ray.py", line 8, in <module>
class Ray:
File "ray.py", line 20, in ray
r = array(range(3));
NameError: name 'array' is not defined
EDIT:
by default spyder has the following behaviour, don't really understand why array() works by default I thought it was only part of numpy.
import numpy as np # NumPy (multidimensional arrays, linear algebra, ...)
import scipy as sp # SciPy (signal and image processing library)
import matplotlib as mpl # Matplotlib (2D/3D plotting library)
import matplotlib.pyplot as plt # Matplotlib's pyplot: MATLAB-like syntax
from mayavi import mlab # 3D plotting functions
from pylab import * # Matplotlib's pylab interface
ion() # Turned on Matplotlib's interactive mode
Within Spyder, this intepreter also provides:
* special commands (e.g. %ls, %pwd, %clear)
* system commands, i.e. all commands starting with '!' are subprocessed
(e.g. !dir on Windows or !ls on Linux, and so on)
You need from numpy import array.
This is done for you by the Spyder console. But in a program, you must do the necessary imports; the advantage is that your program can be run by people who do not have Spyder, for instance.
I am not sure of what Spyder imports for you by default. array might be imported through from pylab import * or equivalently through from numpy import *. If you want to directly copy code from the Spyder console to a program, you might need from numpy import * or even from pylab import *. It is officially not recommended to do this in a program, though, as this pollutes the program's namespace; doing import numpy as np and then np.array(…) is customary.