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.
Related
As I am aiming to plot the data into the plot, I encounter 2 problems.
(1) For the Bokeh function, while I am plotting 4 pictures in a row and adding the save as the SVG. Then the browser is shown as figure1. Moreover, as I comment out the code below, it work definitely right as I thought. It's weird.
p1.output_backend = "svg"
export_svgs(p1, filename="./number_like_comment/fig/{}.svg".format(action_str))
(2) For the second question, after I save those files, I realized that the "Label" I put in the plot also disappear. (as figure3).
Thanks so much for reading through. This question bother me quite a long time.
I guess that the data (on the x-axis) starts at 0, and on a logaxis for some reason the svg renderer doesn't like that and refuses to draw the entire line. The canvas renderer (used for the interactive plots) only refuses to draw the line-segment going from or to 0.
If you want to keep the logarithmic axis, you can either remove the 0 values or replace them by something which can be displayed in a logarithmic scale (e.g. 0.1) and then fixate the x-axis.
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")
I have a number of patches with associated z values, and I'd like them to display so that if two patches overlap the one with the higher z value is shown. It seems that the way to do this is by ordering the renderers according to this github conversation: https://github.com/bokeh/bokeh/issues/696
I have done this successfully by applying a series of glyphs using patch in order of their z value. Unfortunately, using patch for all 460 glyphs takes 25 seconds for a response so it isn't a viable solution.
How do I change the render order for patches? I tried ordering the input data, but this didn't seem to have an impact.
edit:
It seems there might not be a way to accomplish what I want. https://github.com/bokeh/bokeh/issues/3601
As of Bokeh 0.12.10 patch draw order is always stable (see #7049 for details). Therefore the answer is: upgrade to Bokeh 0.12.10 or newer and put the data in the orer you would like it to be drawn.
I'm preparing some plots for a scientific paper, which need to be wide and short in order to fit into the page limit. However, when I save them as pdf, the x axis labels are missing, because (I think) they're outside the bounding box.
Putting the following into an iPython notebook reproduces the problem.
%pylab inline
pylab.rcParams['figure.figsize'] = (8.0, 2.0)
plot([1,5,2,4,6,2,1])
xlabel("$x$")
ylabel("$y$")
savefig("test.pdf")
The resulting pdf file looks like this:
How can I change the bounding box of the pdf file? Ideally I'd like a solution that "does it properly", i.e. automatically adjusts the size so that everything fits neatly, including getting rid of that unnecessary space to the left and right - but I'm in a hurry, so I'll settle for any way to change the bounding box, and I'll guess numbers until it looks right if I have to.
After a spot of Googling, I found an answer: you can give bbox_inches='tight' to the savefig command and it will automatically adjust the bounding box to the size of the contents:
%pylab inline
pylab.rcParams['figure.figsize'] = (8.0, 2.0)
plot([1,5,2,4,6,2,1])
xlabel("$x$")
ylabel("$y$")
savefig("test.pdf",bbox_inches='tight')
Those are some tight inches, I guess.
Note that this is slightly different from Ffisegydd's answer, since it adjusts the bounding box to the plot, rather than changing the plot to fit the bounding box. (But both are fine for my purposes.)
You can use plt.tight_layout() to have matplotlib adjust the layout of your plot. tight_layout() will automatically adjust the dimensions, and can also be used when you have (for example) overlapping labels/ticks/etc.
%pylab inline
pylab.rcParams['figure.figsize'] = (8.0, 2.0)
plot([1,5,2,4,6,2,1])
xlabel("$x$")
ylabel("$y$")
tight_layout()
savefig("test.pdf")
Here is a .png of the output (can't upload pdfs to SO but I've checked it and it works the same way for a pdf).
If you are preparing the plot for a scientific paper, I suggest to do the 'clipping' by yourself,
using
plt.subplots_adjust(left,right,bottom,top,..)
after the creation of the figure and before saving it. If you are running from an ipython console you can also call subplots_adjust after the generation of the figure, and tune the margins by trial and error. Some backends (I think at least the Qt backend) also expose a GUI for this feature.
Doing this by hand takes time, but most times provides a more precise result, especially with Latex rendering in my experience.
This is the only option whenever you have to stack vertically or horizontally two figures (with a package like subfigure for example), as tight_layout will not guarantee the same margins in the two figures, and the axis will result misaligned in the paper.
This is a nice link on using matplotlib for publications, covering for example how to set the figure width to match the journal column width.
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)