How to use ggplot style of matplotlib with agg backend - python

My problem is as follows:
I have a script running remotely via a Jenkins job.
This script is making plots and saving them with plt.savefig()
Without the following line on the top of my imports, I can not run my scripts from jenkins:
matplotlib.use('Agg')
The moment I added this, I could no longer have the ggplot style on my plots:
This line:
matplotlib.style.use('ggplot')
is giving the following error:
Traceback (most recent call last):
File "plot.py", line 5, in <module>
matplotlib.style.use('ggplot')
AttributeError: 'module' object has no attribute 'style'
NOTE that:
this style was working locally when I was executing the script without the line matplotlib.use('Agg')
And the script runs normally on jenkins without the ggplot style.
How can I have both on a remote server?
(python 2.7.6, matplotlib 1.4.3)

style is a package that can be imported from matplotlib.pyplot, not directly from matplotlib.
So, try:
import matplotlib.pyplot
matplotlib.pyplot.style.use('ggplot')

Related

Matplotlib is not working as it used to on pycharm. How can I solve it?

Note: I am just a beginner and doing this by myself, so if you can reply with basic descriptions, that could help more.
This is the code I wrote
from matplotlib import pyplot as plt
plt.plot([2, 1], [8, 2], 'ro')
However, I get this error. I had a code using same way to import and it used to work just fine. Now that code is not working either.
C:\Users\Tuna\PycharmProjects\start\venv\Scripts\python.exe C:/Users/Tuna/PycharmProjects/start/matplotlib.py
Traceback (most recent call last):
File "C:\Users\Tuna\PycharmProjects\start\matplotlib.py", line 1, in <module>
from matplotlib import pyplot as plt
File "C:\Users\Tuna\PycharmProjects\start\matplotlib.py", line 1, in <module>
from matplotlib import pyplot as plt
ImportError: cannot import name 'pyplot' from partially initialized module 'matplotlib' (most likely due to a circular import) (C:\Users\Tuna\PycharmProjects\start\matplotlib.py)
Process finished with exit code 1
To fix, you just need to rename your file to something that isn't matplotlib.py. Python thinks you are trying to import your own file matplotlib.py to use rather than the actual module and hence why you are getting a circular reference.
This should work fine if you have matplotlib installed, if not pycharm should prompt you to install it which should be the easiest way for you to use it in your project.

Python plotly is working in command line but not in python file - import error

plolty 4.5.4 has been installed via conda install -c plotly plotly
python version: 3.7.6
in anaconda CLI, the following lines from plotly quickstart guide display a beautiful plotly graph:
However the same content in a .py file:
import plotly.graph_objects as go
fig = go.Figure(data=go.Bar(y=[2, 3, 1]))
fig.write_html('first_figure.html', auto_open=True)
And then a launch of this file:
python plotly.py
results in an import error:
Traceback (most recent call last):
File "plotly.py", line 1, in module
from plotly import graph_objs as go
File "plotly.py", line 1, in
from plotly import graph_objs as go
ImportError: cannot import name 'graph_objs' from 'plotly'
Can anyone helps ?
This is the well known name shadowing trap.
Just name the file differently from any standard library, third party package, or module that the application relies on.

Importing matplotlib.pyplot in atom editor

I am using pyzo to run my python scripts. However I felt the need to switch over to the Atom code editor. I can run my python scripts without any problem.
At one point I need to use the library matplotlib. In pyzo I would do:
import matplotlib.pyplot as plt
But it doesn't work in Atom
Error message:
Traceback (most recent call last):
File "C:\Users\ivanl\Desktop\python trade\matplotlib.py", line 1, in
import matplotlib.pyplot as plt
File "C:\Users\ivanl\Desktop\python trade\matplotlib.py", line 1, in
import matplotlib.pyplot as plt
ImportError: No module named 'matplotlib.pyplot'; 'matplotlib' is not a package
Where should I look for installing matplotlib? Why is it working on pyzo and not on atom?
From The Module Search Path.
When a module named spam is imported, the interpreter first searches
for a built-in module with that name. If not found, it then searches
for a file named spam.py in a list of directories given by the
variable sys.path. sys.path is initialized from these locations:
the directory containing the input script (or the current directory).
PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
the installation-dependent default.
Which means you should avoid naming your modules with the same name as standard-library or built-in module names.
So you should rename your script file instead of matplotlib.py.
I faced a error like below while trying to import Matplotlib from atom:
Traceback (most recent call last):
File "lanes.py", line 3, in
import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib'.
All I did is go to my C:\Users*user_name*.atom directory in command prompt and type pip3 install matplotlib. Now it works fine.
So in command prompt you need to give something like below:
C:\Users\*user_name*\.atom>pip3 install matplotlib.
Hope it works.

Hanging during matplotlib import

Here is an issue some of you might have already encountered : I just installed matplotlib using :
pip install matplotlib
on my OS X El Capitan Version 10.11.1. It seems to me that the installation went well, and the packages are properly located in /usr/local/lib/python2.7/site-packages
I get no problem when I try to import matplotlib using :
import matplotlib
Now, when I try to import matplotlib.pyplot in a python script, using :
import matplotlib.pyplot as plt
the program starts hanging, and I have to interrupt it using Keyboard Ctrl + C.
Besides, when I only import matplotlib and try to run this piece of code :
import matplotlib
matplotlib.pyplot
I get this message in the terminal Session :
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'pyplot'
Does anyone have a solution to fix this problem ?

Can't load matplotlib.pyplot from script

I'm trying to run the contourf example from the matplotlib documentation.
When I run it from a python shell, everything works well, but when I run it as a script (python contourf_demo.py), the following error rises:
Traceback (most recent call last):
File "matplotlib.py", line 3, in
import matplotlib.pyplot as plt
File "/home/user/dir/contourf_demo.py", line 3, in
import matplotlib.pyplot as plt
ImportError: No module named pyplot
I'm using Ubuntu 12.04, and the problem comes when importing:
import matplotlib.pyplot as plt
I have tried using different ways to call the import (such as from matplotlib import pyplot), but it doesn't work neither.
You can't import a module named XYZ from a file called XYZ.py, because Python would interpret that you are trying to import the file itself.
Change your file from matplotlib.py to anything else.

Categories