I got an error message from Python:
Traceback (most recent call last):
File "stock_script.py", line 9, in <module>
from matplotlib.finance import candlestick
ImportError: cannot import name candlestick
Why?
Matplotlib code has been changed...
Go into the script and replace "candlestick" by "candlestick_ohlc"
So it should read:
from matplotlib.finance import candlestick_ohlc
candlestick_ohlc(ax1, ...
Not very nice changing the name and breaking existing code...
I have fixed this by changing the import like this.
from matplotlib.finance import candlestick_ohlc as candlestick
No further changes are then needed in the code.
You need to add an underscore to it to use this same library:
from matplotlib.finance import _candlestick
Related
I am just learning to code using the wordcloud and stylecloud libraries in python 3.9.0. I wrote the basic code for the stylecloud library but it gives an error that says:
ImportError: cannot import name 'makeMappingArray' from 'matplotlib.colors'
This is the code:
import wordcloud
import matplotlib
def cloud_design():
stylecloud.gen_stylecloud(file_path=r"C:\Users\Edwin Anil\OneDrive - AL RAYAN HOLDING CO\ALL PYTHON CODES\word cloud text file.txt",icon_name="<i class='fas fa-guitar'></i>")
cloud_design()
The version of wordcloud that is being used is wordcloud-1.8.1.
The version of matplotlib that is being used is matplotlib-3.4.1.
this is the entire error message:
File "C:\Users\Edwin Anil\OneDrive - AL RAYAN HOLDING CO\ALL PYTHON CODES\word cloud.py", line 1, in <module>
import stylecloud
File "C:\Users\Edwin Anil\AppData\Local\Programs\Python\Python39\lib\site-packages\stylecloud\__init__.py", line 1, in <module>
from .stylecloud import gen_stylecloud
File "C:\Users\Edwin Anil\AppData\Local\Programs\Python\Python39\lib\site-packages\stylecloud\stylecloud.py", line 6, in <module>
from matplotlib.colors import makeMappingArray, to_rgb
ImportError: cannot import name 'makeMappingArray' from 'matplotlib.colors' (C:\Users\Edwin Anil\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\colors.py)```
I did try various other syntax forms for import and tried uninstalling and reinstalling matplotlib too but I don't seem to figure it out. Some help is appreciated.
when you copy the html,you just need to put the class name. example "fas fa-guitar":
import wordcloud
import matplotlib
import stylecloud
def cloud_design():
stylecloud.gen_stylecloud(file_path="my_random.txt",icon_name="fas fa-guitar")
cloud_design()
output:
guitar
Alright so according to #simpleApp's solution. I tried downgrading to an older version of matplotlib and it worked.
The cmd command was pip install matplotlib==3.2.2
Here is the result:
just new to python, and I believe this is not a big deal but because I am a freshman.
Basically, this is a simple program plotting a FM signal in time domain. I write a module by myself.
def FreqMod (fc,fm,t_domain)
pi=py.pi
if fc>fm:
delta=fc-fm
else:
delta=fm-fc
return py.cos(2*pi*fc*t_domain+ (delta/fm)*py.sin(2*pi*fm*t_domain))
def AmpMod(fc,fm,t_domain):
pi=py.pi
return py.cos(2*pi*fc*t_domain)*py.cos(2*pi*fm*t_domain)
And import it in another program
import numpy as py
import mylib
import matplotlib.pyplot as plt
pi=py.pi
y=mylib.FreqMod(5,1000,t=py.arange(0,2*pi,pi/4000))
plt.plot(y)
The lib file is located as the same directory as the program. But I Got this later:
Traceback (most recent call last):
File "...(The directory)...", line 14, in <module>
y=mylib.FreqMod(5,1000,t=py.arange(0,2*pi,pi/4000))
AttributeError: module 'mylib' has no attribute 'FreqMod'
It seems like I didn't import the module successfully. I have compared it with examples in how to write and import a module, but yet can't figure out why. This really confuse me as a beginner in python.
I am using Zeppelin and matplotlib to visualize some data. I try them but fail with the error below. Could you give me some guidance how to fix it?
%pyspark
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()
And here is the error I've got
Traceback (most recent call last):
File "/tmp/zeppelin_pyspark-3580576524078731606.py", line 235, in <module>
eval(compiledCode)
File "<string>", line 1, in <module>
File "/usr/lib64/python2.6/site-packages/matplotlib/pyplot.py", line 78, in <module>
new_figure_manager, draw_if_interactive, show = pylab_setup()
File "/usr/lib64/python2.6/site-packages/matplotlib/backends/__init__.py", line 25, in pylab_setup
globals(),locals(),[backend_name])
File "/usr/lib64/python2.6/site-packages/matplotlib/backends/backend_gtkagg.py", line 10, in <module>
from matplotlib.backends.backend_gtk import gtk, FigureManagerGTK, FigureCanvasGTK,\
File "/usr/lib64/python2.6/site-packages/matplotlib/backends/backend_gtk.py", line 8, in <module>
import gtk; gdk = gtk.gdk
File "/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py", line 64, in <module>
_init()
File "/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py", line 52, in _init
_gtk.init_check()
RuntimeError: could not open display
I also try to add these lines, but still cannot work
import matplotlib
matplotlib.use('Agg')
The following works for me with Spark & Python 3:
%pyspark
import matplotlib
import io
# If you use the use() function, this must be done before importing matplotlib.pyplot. Calling use() after pyplot has been imported will have no effect.
# see: http://matplotlib.org/faq/usage_faq.html#what-is-a-backend
matplotlib.use('Agg')
import matplotlib.pyplot as plt
def show(p):
img = io.StringIO()
p.savefig(img, format='svg')
img.seek(0)
print("%html <div style='width:600px'>" + img.getvalue() + "</div>")
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
show(plt)
The Zeppelin documentation suggests that the following should work:
%python
import matplotlib.pyplot as plt
plt.figure()
(.. ..)
z.show(plt)
plt.close()
This doesn't work for me with Python 3, but looks to be addressed with the soon-to-be-merged PR #1213.
Note that as of Zeppelin 0.7.3, matplotlib integration is much more seamless, so the methods described here are no longer necessary. https://zeppelin.apache.org/docs/latest/interpreter/python.html#matplotlib-integration
As per #eddies suggestion, I tried and this is what worked for me on Zeppelin 0.6.1 python 2.7
%python
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.figure()
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
z.show(plt, width='500px')
plt.close()
Change this:
import matplotlib
matplotlib.use('Agg')
with
import matplotlib.pyplot as plt; plt.rcdefaults()
plt.switch_backend('agg')
Complete code example Spark 2.2.0 + python3(anaconda3.5):
%spark.pyspark
import matplotlib.pyplot as plt; plt.rcdefaults()
plt.switch_backend('agg')
import numpy as np
import io
def show(p):
img = io.StringIO()
p.savefig(img, format='svg')
img.seek(0)
print ("%html <div style='width:600px'>" + img.getvalue() + "</div>")
# Example data
people=('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos=np.arange(len(people))
performance=3 + 10 * np.random.rand(len(people))
error=np.random.rand(len(people))
plt.barh(y_pos, performance, xerr=error, align='center', alpha=0.4)
plt.yticks(y_pos, people)
plt.xlabel('Performance')
plt.title('How fast do you want to go today?')
show(plt)
I would suggest you to use IPython/IPySpark interpreter in zeppelin 0.8.0 which will be released soon. The matplotlib integration in ipython is almost the same as jupyter. There's one tutorial https://www.zepl.com/viewer/notebooks/bm90ZTovL3pqZmZkdS9lN2Q3ODNiODRkNjA0ZjVjODM1OWZlMWExZjM4OTk3Zi9ub3RlLmpzb24
Trying Bokeh for the first time. Using the following example code from http://docs.bokeh.org:
from collections import OrderedDict
from bokeh.charts import Scatter
from bokeh.sampledata.iris import flowers
output_notebook()
Inside iPython (Anaconda, py 3.4, Win 7), get the following error:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-2-c57c6fa8f51a> in <module>()
----> 1 output_notebook()
NameError: name 'output_notebook' is not defined
Why? This is straight from the examples.
It looks as if you forgot to import output_notebook.
Typical notebooks have following imports:
from bokeh.io import output_notebook, show
from bokeh.plotting import figure
output_notebook()
I wrote a module (processing_0) in which I import all packages and modules required for my project.
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
import collections
import la
import csv
import fishery
import re
from collections import OrderedDict
import processing_1
import processing_2
import processing_3
from processing_1 import readingraph, readinpathgraph, preparefisher, inEEG
from processing_2 import pathwayprofile
from processing_3 import checkkin
from fishery import fisher
The modules that I wrote (processing_1/2/3) all require access to networkx (nx).
As part of the master module, I have a the startup function:
def startup():
EEG = readingraph("/.../file1")
EET = readingraph("/.../file2")
EEL = readingraph("/.../file3")
return EEG, EET, EEL
However, after importing processing_0 and trying to run startup() that uses readingraph from processing_1, I keep getting the error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "processing_0.py", line 31, in startup
EEG = readingraph("/.../file1")
File "processing_1.py", line 4, in process
graph = nx.read_adjlist(filename)
NameError: global name 'nx' is not defined
Is there any way to globally import networkx as nx and make it accessible to all imported modules?
if you are using linux ubuntu, do these followings in order.
sudo apt-get update
sudo apt-get install python-networkx
go to pycharm env and > file>setting> interpreter and structure to
configure your python env and add packages, there is all packages
that are in env, then click on + to add newpackage
type networkx in search text box, and then select it from package
list and click on install
after finish it , click ok and close windows
In every file that you use networkx you need to import it. So just repeat the line
import networkx as nx
inside the file processing_1.py