Plot-style of matplotlib - python

I'd like to know why matplotlib outputs plots in different styles depending on which system I run the file. For example, I let the same code run twice, once on a Windows machine and once on Ubuntu system, and got the following two plots
The information in the plots is obviously the same, but is there a special command to let matplotlib know that I'd, for example, would like all my plots to be produced in the "Ubuntu style" (lower of the two) version?

The upper plot is produced with a version of matplotlib >= 2.0. The lower plot is produced with a version < 2.0. The style changes are documented in the Changes to the default style.
It's hardly possible to make a matplotlib version < 2.0 produce the exact output you'd get with matplotlib >= 2.0, although you might copy the style sheet from a newer version to the old one and set the parameters accordingly.
The inverse is however possible. To get the old (classic) style in a new version of matplotlib should be possible via
import matplotlib.pyplot as plt
plt.style.use("classic")

Related

Matplotlib ugly graph on xubuntu

i have a strange problem.
I have a python script in which i plot some data with plt.errorbar(...) and plt.plot(...).
The strange fact is, depending on the OS i am using, the graph looks different.
With elementary OS everything is ok, but with xubuntu it looks like this! (picture allegated).
The script is EXACTLY the same.
How can i solve to have it like elementary os one? (It's the one with tiny bars and without "flat" theme like).
(the picture are different only in titles, but every other thing is exactly the same).
The first obvious reason the two plots look different, is that you use different versions of matplotlib on the two systems.
Once you use the same version, you should make sure that the matplotlib rcParams are the same.
The backend in use should only have a minor influence on the appearance (e.g. line thickness might be slightly different in saved figures or so).

matplotlib changed rendering style unexpectedly in jupyter-notebook

My matplotlib plots have changed styles unexpectedly. I am trying to pin down what I did to change them. My best guess is that I changed matplotlib versions, or I'm possibly am using a different backend.
A histogram plot currently looks like this:
They used to look like this (nice defined border lines between the bars):
I have made no changes to the code that generates the plots, but I have mucked with import statements, and re-installed various components of anaconda, including matplotlib for unrelated reasons.
Earlier this year, matplotlib changed their defaults. You want the edgecolor param.
plt.hist(np.random.norma(0,1,100), edgecolor='k')
You can use a classic style by passing plt.style.use('classic').
Starting from matplotlib version 2.0, patches do not have edges anymore. See the Changes to the default style.
Options to set edges back on:
Use the edgecolor argument of the artist. E.g.
plt.bar(...., edgecolor="k")
Use the rcParams to globally set edges,
plt.rcParams['patch.force_edgecolor'] = True
or edit you matplotlibrc file accordingly.
Turn the old style on again, using
plt.style.use('classic')

Matplotlib example output significantly differs from website

When I run the Matplotlib api example code: radar_chart.py on my computer the output differs from the result on the Matplotlib website at a crucial point. The zero values, of which there are plenty of them, do not hit the origin of the chart on the Matplotlib website, see the chart at the link. When I run the exact same code on my own computer the zero values do hit the origin. See picture below. This results in a less smooth and readable chart compared to the one on the Matplotlib website, however this is not what one would expect. Could anyone please tell me why this difference exists?
The reason for this difference is that the linked example is produced using matplotlib 2.0, while on your computer you run <= 1.5.
It can be observed when looking at the old example on the matplotlib page.
This difference is due to the axes margins being set to 0 in matplotlib 1.5 and to 0.05 in matplotlib 2.0.
There are several ways to set the margins, one being plt.margins(x=0.05, y=0.05).
Since here you want to have the same margins for all axes, one easy method is to use rc params. Adding
plt.rcParams['axes.xmargin'] = 0.05
plt.rcParams['axes.ymargin'] = 0.05
at the top of the script, will set the margins to the values used by default in matplotlib 2.0. Of course you can play around with them and see which values best fit your needs.

Update figure without saving

Using the PGF-backend for matplotlib, I am trying to modify the ticklabels of my logarithmic y-axis. I do not however know beforehand what my ticklabels will be, so I'd like to get the ticklabels with ax.get_yticklabels() and then modify the text of the labels (by removing a substring). The problem is, that the list returned by get_yticklabels() is only filled when issuing the savefig('plot.pdf') command.
Is there a function that updates the whole figure without actually saving it, so I can get to the labels? The fig.canvas.draw() function does not work as I would have expected. I am using matplotlib version 1.2.0 with Python 3.3.0 on Linux.
PS: The underlying problem is, that I can't get a sans-serif font for the labels on logarithmic plots with the PGF-backend, which I am pretty sure is a bug in the backend. Any solution to that would of course also be welcome and make my initial question obsolete.

Barchart (o plot) 3D in Python

I need to plot some data in various forms. Currently I'm using Matplotlib and I'm fairly happy with the plots I'm able to produce.
This question is on how to plot the last one. The data is similar to the "distance table", like this (just bigger, my table is 128x128 and still have 3 or more number per element).
Now, my data is much better "structured" than a distance table (my data doesn't varies "randomly" like in a alphabetically sorted distance table), thus a 3D barchart, or maybe 3 of them, would be perfect. My understanding is that such a chart is missing in Matplotlib.
I could use a (colored) Countor3d like these or something in 2D like imshow, but it isn't really well representative of what the data is (the data has meaning just in my 128 points, there isn't anything between two points). And the height of bars is more readable than color, IMO.
Thus the questions:
is it possible to create 3D barchart in Matplotlib? It should be clear that I mean with a 2D domain, not just a 2D barchart with a "fake" 3D rendering for aesthetics purposes
if the answer to the previous question is no, then is there some other library able to do that? I strongly prefer something Python-based, but I'm OK with other Linux-friendly possibilities
if the answer to the previous question is no, then do you have any suggestions on how to show that data? E.g. create a table with the values, superimposed to the imshow or other colored way?
For some time now, matplotlib had no 3D support, but it has been added back recently. You will need to use the svn version, since no release has been made since, and the documentation is a little sparse (see examples/mplot3d/demo.py). I don't know if mplot3d supports real 3D bar charts, but one of the demos looks a little like it could be extended to something like that.
Edit: The source code for the demo is in the examples but for some reason the result is not. I mean the test_polys function, and here's how it looks like:
example figure http://www.iki.fi/jks/tmp/poly3d.png
The test_bar2D function would be even better, but it's commented out in the demo as it causes an error with the current svn version. Might be some trivial problem, or something that's harder to fix.
MyavaVi2 can make 3D barcharts (scroll down a bit). Once you have MayaVi/VTK/ETS/etc. installed it all works beautifully, but it can be some work getting it all installed. Ubuntu has all of it packaged, but they're the only Linux distribution I know that does.
One more possibility is Gnuplot, which can draw some kind of pseudo 3D bar charts, and gnuplot.py allows interfacing to Gnuplot from Python. I have not tried it myself, though.
This is my code for a simple Bar-3d using matplotlib.
import mpl_toolkits
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
%matplotlib inline
## The value you want to plot
zval=[0.020752244,0.078514652,0.170302899,0.29543857,0.45358061,0.021255922,0.079022499,\
0.171294169,0.29749654,0.457114286,0.020009631,0.073154019,0.158043498,0.273889264,0.419618287]
fig = plt.figure(figsize=(12,9))
ax = fig.add_subplot(111,projection='3d')
col=["#ccebc5","#b3cde3","#fbb4ae"]*5
xpos=[1,2,3]*5
ypos=range(1,6,1)*5
zpos=[0]*15
dx=[0.4]*15
dy=[0.5]*15
dz=zval
for i in range(0,15,1):
ax.bar3d(ypos[i], xpos[i], zpos[i], dx[i], dy[i], dz[i],
color=col[i],alpha=0.75)
ax.view_init(azim=120)
plt.show()
http://i8.tietuku.com/ea79b55837914ab2.png
You might check out Chart Director:
http://www.advsofteng.com
It has a pretty wide variety of charts and graphs and has a nice Python (and several other languages) API.
There are two editions: The free version puts a blurb on the generated image, and the
pay version is pretty reasonably priced.
Here's one of the more interesting looking 3d stacked bar charts:
(source: advsofteng.com)

Categories

Resources