How can I get old matplotlib version 1.5.1 interface? - python

I recently updated from matplotlib 1.5.1 to 2.2.3 with
$ pip install -U matplotlib
When showing plotted data with
>>> plt.show()
I could see plots displayed as shown in the attached picture "Output before update" before running the update from 1.5.1 to 2.2.3.
Because I didn't like the new interface of version 2.2.3 when displaying plots I switched back to the old version with
$ pip install matplotlib==1.5.1
Yet I'm still seeing a different interface when I show plots using the old matplotlib version (as you can see in the image "Output after update and return to the old version"), even though I returned to the old version of matplotlib I had used to generate the "Output before update".
You can check what I mean by "interface" when you type in your command line:
$ python
>>> import matplotlib.pyplot as plt
>>> plt.plot()
>>> plt.show()
You'll see the "interface" (in my case IPython) and an empty plot.
My questions are:
How do I display plots with the old output window again ("Output before update", see below)?
And why didn't the return to version 1.5.1 bring the old output window back as well?
Attached you find a comparison between the old output interface that I'd like to get back, and the new output using the same matplotlib version (slightly different data shown, but both plotted in default style, only with colors and markers manually set). The new output is blurry and thick, and the data points are very large as opposed to the old version, which I find much less aesthetic.
Output after update and return to the old version
Output before update
Picture of the "blurry" output window

Related

Matplotlib notebook magic in Jupyter notebook partial plot

The first command in my Jupyter notebook is the %matplotlib notebook. This has been working fine but curiously the figures being drawn today are only partial plots. (The inline option without interactivity display complete plots).
When I pan, I can see more sections of the image.
I would like to see the entire plot and I am not sure what has caused this change in display
When image is first displayed it appears like this:
Image created upon running the cell
After panning it appears like this:
New section of image displayed upon panning
NOTE: This started to occur after updating to Matplotlib version 3.3.0.
For the current time I have reverted to Matplotlib version 3.1.0. But if the latest has some dependencies that I am missing which is causing the issue, I would like to learn how to figure those out.
I do not receive any errors or warnings upon import matplotlib or pyplot when working with 3.3.0

Basic code from plotly's own website doesn't work. How do I handle a Layout property ceasing to exist?

I'm getting an error trying to set the legend title, which should be very basic. Eventually I tried the code on plotly's website, and even that didn't work.
from plotly.graph_objs import Figure, Scatter
fig = Figure()
fig.add_trace(Scatter(x=[1,2,3,4,5], y=[1,2,3,4,5], name='Increasing'))
fig.add_trace(Scatter(x=[1,2,3,4,5], y=[5,4,3,2,1], name='Decreasing'))
fig.update_layout(legend_title='<b> Trend </b>')
fig.show()
The error I'm getting is:
`Invalid property specified for object of type plotly.graph_objs.Layout: 'legend_title'
The computer I'm trying it on (in Jupyter Notebook) is running Python 3.6.5 and plotly 4.4.1. The code does work on a different computer running Python 3.7.1 and plotly 4.6.0. However, all of those versions seem relatively new. For reasons, I can't upgrade the Python or plotly versions of the computer that I can't get it to work on (which is the computer that I need to get it to work on). Is my plotly corrupted? I tried '!pip install plotly' but that didn't do anything. I then tried "!pip uninstall plotly' but that just locked up the cell at [*]. Is there another way to reinstall plotly? Or is there something else that I'm missing?
legend_title was introduced in Plotly v4.5 so yes, you will need to upgrade: https://community.plotly.com/t/announcing-plotly-py-4-5/34045

Incorrect matplotlib plot using Python 2.7.15 but not when using 2.7.10

Notice how the plot is shifting to the left in the bottom plot. Both plots were made with the exact same input data and same Python script, but plotted on different computers with different versions of Python. Can anyone suggest where I might find the culprit? The top plot is correct, bottom is not.
From the matplotlib documentation of bar
Version 1.3.1
align : [‘edge’ | ‘center’], optional, default: ‘edge’
Version 2.2.3
align : {'center', 'edge'}, optional, default: 'center'
Use align="edge" to obtain the first image in both versions. Use align="center" to obtain the second image in both versions.
As #DanielLabbe has suggested this is most likely an issue with matplotlib versions. You can check your version of matplotlib:
import matplotlib as mpl
mpl.__version__
If you have sudo / administrative rights, you'll want to update matplotlib on one of your machines so they both run the same version. If you can't do this, explicitly specifying the align argument in plt.hist might help, eg.
plt.hist(my_data, align='right')

Using matplotlib *without* TCL

Exactly what the title says. Is there a way to use the matplotlib library without installing TCL? Please don't tell me to bite the bullet and install TCL - I know how to do it but for my own (ok maybe silly) reasons I don't want to.
I don't care about displaying the plots, I only want to be able to output them in a png. I tried various things (using different backends etc) but matplotlib always wanted to find tcl to work :( Why is TCL so essential for matplotlib?
Also, please notice that I am using windows -- I have installed everything that could be required (numpy, pandas, matplotlib) using pip.
#gerrit's solution is the correct one (I was trying to change the backends but I was doing it after loading pyplot -- the important thing seems to be that you need to change the backend immediately after imporing matplotlib). Here's a small example using it:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
fig, ax = plt.subplots( nrows=1, ncols=1 )
ax.plot([0,1,2], [10,20,3])
fig.savefig('foo.png')
plt.close(fig)
This will output a file named 'foo.png' without using TCL \o/
Matplotlib 3.0 and newer
(Added to answer in October 2018)
Starting with Matplotlib 3, released on 19 September 2018, the problem described in the question should not occur. From the what's new part of the documentation:
The default backend no longer must be set as part of the build process. Instead, at run time, the builtin backends are tried in sequence until one of them imports.
Headless linux servers (identified by the DISPLAY env not being defined) will not select a GUI backend.
So, as long as you make sure DISPLAY is not defined, you should not run into any problems with the backend when running in a script on a headless Linux server.
Matplotlib 2.2 and older
(Original answer May 2016)
Immediately after loading matplotlib, enter
matplotlib.use('Agg')
Do this before loading pyplot, if at all.
By default, Matplotlib uses the TkAgg backend, which requires Tcl. If you don't want to display the plots, Agg is fine. Other alternatives include WX and QTAgg, but both require the installation of additional libraries.
Alternately, you can set this directive in your matplotlibrc file:
backend : Agg
For details, see the Matplotlib Usage FAQ on What is a backend?.

Matplotlib stopped working after upgrade to 1.1.0

I'm trying to run matplotlib 1.1.0 under Ubuntu 11.10. I previously had matplotlib 1.0.1 installed and working, but I needed some features from version 1.1.0 (and was becoming quite annoyed that all the documentation was for another version than mine) so I decided to upgrade.
Now, I can't show any plots.
I have uninstalled everything I had from the package repository, and installed version 1.1.0 from source. I've read through the installation FAQ but the hints there did not help. I get the following output from their suggested troubleshooting procedure, but no plot:
$HOME=/home/tomas
CONFIGDIR=/home/tomas/.matplotlib
matplotlib data path /usr/local/lib/python2.7/dist-packages/matplotlib/mpl-data
loaded rc file /usr/local/lib/python2.7/dist-packages/matplotlib/mpl-data/matplotlibrc
matplotlib version 1.1.0
verbose.level helpful
interactive is False
platform is linux2
Using fontManager instance from /home/tomas/.matplotlib/fontList.cache
backend agg version v2.2
What do I need to do to get this working?
Update:
After following up on some troubleshooting tips in comments, I can report that it's the actual showing of plots that doesn't work.
Running the following script outputs a png with the expected plot, but does not show any plotting window.
from matplotlib import pyplot as plt
plt.plot([1, 2, 3])
plt.savefig('testfig.png')
plt.show()
When you import pyplot, it has to set a backend, and it may be setting it to a non-interactive one, which would explain the behavior you are seeing. See what backend is being used by running plt.get_backend()
To set the backend, you have to run these commands before importing pyplot:
import matplotlib
matplotlib.use(your_backend)
where
your_backend in set(["FLTKAgg", "GTK", "GTKAgg", "GTKCairo", "macosx",
"QTAgg", "QT4Agg", "TkAgg", "WX", "WXAgg"])
To make this a permanent setting, put the backend name (without quotes) in your ~/.matplotlib/matplotlibrc file, like this example:
backend : WXAgg

Categories