Cannot get matplotlib graphics to show up inline - python

I would like to load automatically the command lines with Jupyter in Ubuntu 16.10 :
%matplotlib inline
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
With Ubuntu, we could normally use configuration files as .vimrc, .bashrc and so on to automate some command lines. I know we could create the config file jupyter_notebook_config.py with jupyter notebook --generate-config. How could we implement that in python in that .py file?
Thanks!

You can't have graphics inline in a regular terminal because regular terminals don't have graphics capabilities. However, if you would be willing to use an alternative console, Jupyter Qtconsole, may be suitable for you.
EDIT: okay, I've had a look and you need to look at this, specifically the parts about running files/lines of code at startup. (like c.InteractiveShellApp.exec_lines or c.InteractiveShellApp.exec_files

Related

matplotlib doesn't plot in terminal

I am trying to plot some scientific data by using Matplotlib in terminal.
here is my simple code :
import matplotlib.pyplot as plt
plt.plot([1,2,3],[3,2,5])
plt.show(block=True)
But I can't see any graph on my linux.
I am using wsl on windows for linux.
debian is my linux.
The link below doesn't help my problem:
Matplotlib plots aren't shown when running file from bash terminal
as you see I set block to True and nothing! for now I am using plt.savefig() function to see my result in windows.
may be I need to install some software in my Linux for image viewer , no?
thank you

Modularize Jupyter notebook preamble

I have a set of Jupyter notebooks, all of which need the same lines of code to initialize. The initialization contains:
IPython magic commands (such as %matplotlib inline and %load_ext autoreload)
importing modules
configuring some settings, such as plot style
I am looking for a good way to put this code into a module that can be imported and ideally called with something like:
import preamble
preamble.run()
For the initialization code it is easy to do, but what about the module imports and magic commands?
You may instead use a jupyter notebook for that.
Simply put all your code into preamble.ipynb and run it in your first cell with:
%run ./preamble.ipynb

Jupyter reload custom mplstyles

I want to develop custom styles for plots in matplotlib. For this reason I have a custom stylesheet (my_style.mpstyle) located in the default directory (~/.config/matplotlib/styles). I can do this with Python scripts without a problem but recently I find myself using Jupyter more. Although the stylesheets generally work, I have the problem, that changes in the stylesheet are not considered unless I restart the Kernel - is there a way of reloading the stylesheet when I execute the cell such that I can develop the style with Jupyter?
Thanks.
I got around this problem by defining a PYTHONSTARTUP file. This is a file that contains a set of python commands (usually import statements) that are automatically executed every time you launch a python interpreter, even when launching a jupyter application or restarting a kernel within one.
Here's a portion of my startup file that imports pyplot and sets a style sheet.
try:
import matplotlib.pyplot as plt
print('import matplotlib.pyplot as plt')
except:
print('Could not import matplotlib.pyplot')
try:
plt.style.use('mystylesheet')
except:
print('No mpl style sheet set; could not find "mystylesheet"')
To make python execute the startup file, you need to define the environmental variable PYTHONSTARTUP=full/path/to/startupfile. In Linux, it's quite easy; just modify your .bashrc to include
export PYTHONSTARTUP="full/path/to/startupfile"
For windows it is a little more involved, and of course the process varies from one version to another. Here is one website that appears to summarize the process for several versions.

Can %matplotlib notebook only be bused in Jupyter?

I have this and it works in jupyter in browser:
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import scipy as sp
However, after I copied it to a python file in the PyDev IDE editor, it always complains the first line:
%matplotlib notebook
Error message:
Encountered "%" at line 1, column 1. Was expecting one of: <EOF> <NEWLINE> ... "(" ... "{" ...
Does this mean the magic method can only be used in jupyter notebook?
Yes, It's a jupyter notebook command.
Well, you can't use it in a file (as that's not valid Python syntax), but you can use it in the PyDev interactive console: http://www.pydev.org/manual_adv_interactive_console.html if you have IPython properly installed.
The command in question, %matplotlib notebook, can be used to specify how to display graphs in a Jupyter notebook. If you use %matplotlib inline, the graphs will be displayed as a part of the Jupyter notebook. Using the form of the command that you asked about will display the graph in an interactive form within the Jupyter notebook. Or using %matplotlib gtk will open another program to display each individual graph, which is how Python would display the graphs in any other situation. So the command does not have any use or meaning outside of a Jupyter notebook.

Plots won't show up in qtconsole

I have read tons of questions on this, but I am still baffled. I'm running Anaconda in Scientific Linux. I launche a console a type ipython qtconsole.
My script.py is something like
import matplotlib.pyplot as plt
plt.plot([1,2,3,4,5,6,7,8,9,10])
plt.show()
I type run script.py from the qtconsole, and the program just sits and does nothing. What am I doing wrong? I have been using the qtconsole for running my scripts, maybe it's better for really being interactive with and I should be running my scripts some other way?
Any general advice on workflow would be very helpful here. When should I use python script.py, when should I use ipython script.py, and when should I use the qtconsole, etc...?
This seems like a Magic Function problem
More specifically %matplotlib. %matplotlib inline will show it within the browser, or you can replace inline with whatever you feel better for your use.

Categories

Resources