Matplotlib rendering bar graph incorrectly - python

OK, I've got some data that I'm plotting with matplotlib in python 3.x (it's basically just a csv file with x and y data)
The graph plots fine, except that it ends up with weird bands in it where the spacing between the bars seems to have broken down:
I know it's not a problem with the data because I ran a test in excel, and got interesting patterns there as well (though in different places)
Please can anybody tell me whether 1) this is something that is already known about, and 2) there is a way of correcting it?

This is a problem of stamping in raster images like bmp or png. The structure you want to show (in this case vertical lines) shows features the size of which are on the same order of magnitude than the raster grid (the pixels of the image). This will inevitably lead to distotions.
The option you have is to increase dpi, such that the lines have more pixels to be placed on.

Related

How to make a plot that connect its points to its closest neighbors?

I am working on a code to project a given object onto a plane.
The code works fine (at least it seems like it) in achieving that purpose, the only issue I'm having is in plotting my results.
In the image below, for instance, I'm plotting the projection of a parallelepiped (its edges, to be more precise) in a plane of my choice.
I would like to make a plot where each point is connected to its closest neighbor. I'm not very confident that this approach would get the job done, but I think it would be worth the shot.
Different ideas to get there are also welcome!
Any thoughts?
Thanks in advance.
Note: I also tried using a solid line style when plotting as opposed to the pixel marker style, but the result I got was not quite what I expected to say the least:
When telling matplotlib to plot a sequence of points and join them with a line, it creates a straight line between two adjacent points in your input data. To create several lines, it's often easier to split your plot command into several ones. An alternative is to arrange your points such that they form the edges you want, but that would be much more complicated in your case.
As discussed in the comments, separating each edge into its own separate plot command worked for your case.

Bokeh line graph looping

I’ve been working on bokeh plots and I’m trying to plot a line graph taking values from a database. But the plot kind of traces back to the initial point and I don’t want that. I want a plot which starts at one point and stops at a certain point (and circle back). I’ve tried plotting it on other tools like SQLite browser and Excel and the plot seems ok which means I must be doing something wrong with the bokeh stuff and that the data points itself are not in error.
I’ve attached the images for reference and the line of code doing the line plot. Is there something I’ve missed?
>>> image = fig.line(“x”, “y”, color=color, source=something)
(Assume x and y are integer values and I’ve specified x and y ranges as DataRange1d(bounds=(0,None)))
Bokeh does not "auto-close" lines. You can see this is the case by looking at any number of examples in the docs and repository, but here is one in particular:
http://docs.bokeh.org/en/latest/docs/gallery/stocks.html
Bokeh's .line method will only "close up" if that is what is in the data (i.e., if the last point in the data is a repeat of the first point). I suggest you actually inspect the data values in source.data and I believe you will find this to be the case. Then the question is why is that the case and how to prevent it from doing that, but that is not really a Bokeh question.

matplotlib shows different figure than saves from the show() window

I plot rather complex data with matplotlib's imshow(), so I prefer to first visually inspect if it is all right, before saving. So I usually call plt.show(), see if it is fine, and then manually save it with a GUI dialog in the show() window. And everything was always fine, but recently I started getting a weird thing. When I save the figure I get a very wrong picture, though it looks perfectly fine in the matplotlib's interactive window.
If I zoom to a specific location and then save what I see, I get a fine figure.
So, this is the correct one (a small area of the picture, saved with zooming first):
And this one is a zoom into approximately the same area of the figure, after I saved it all:
For some reason pixels in the second one are much bigger! That is vary bad for me - as you can see, it looses a lot of details in there.
Unfortunately, my code is quite complicated and I wasn't able to reproduce it with some randomly generated data. This problem appeared after I started to plot two triangles of the picture separately: I read my two huge data files with np.loadtxt(), get np.triu(data1) and np.tril(data2), mask zeroes, NAs, -inf and +inf and then plot them on the same axes with plt.imshow(data, interpolation='none', origin='lower', extent=extent). I do lot's of other different things to make it nicer, but I guess it doesn't matter, because it all worked like a charm before.
Please, let me know, if you need to know anything else specific from my code, that could be relevant to this problem.
When you save a figure in png/jpg you are forced to rasterize it, convert it to a finite number of pixels. If you want to keep the full resolution, you have a few options:
Use a very high dpi parameter, like 900. Saving the plot will be slow, and many image viewers will take some time to open it, but the information is there and you can always crop it.
Save the image data, the exact numbers you used to make the plot. Whenever you need to inspect it, load it in Matplotlib in interactive mode, navigate to your desired corner, and save it.
Use SVG: it is a vector graphics format, so you are not limited to pixels.
Here is how to use SVG:
import matplotlib
matplotlib.use('SVG')
import matplotlib.pyplot as plt
# Generate the image
plt.imshow(image, interpolation='none')
plt.savefig('output_image')
Edit:
To save a true SVG you need to use the SVG backend from the beginning, which is unfortunately, incompatible with interactive mode. Some backends, like GTKCairo seem to allow both, but the result is still rasterized, not a true SVG.
This may be a bug in matplotlib, at least, to the best of my knowledge, it is not documented.

transforming coordinates in matplotlib

I'm trying to plot a series of rectangles and lines based on a tab delimited text file in matplotlib. The coordinates are quite large in the data and shown be drawn to scale -- except scaled down by some factor X -- in matplotlib.
What's the easiest way to do this in matplotlib? I know that there are transformations, but I am not sure how to define my own transformation (i.e. where the origin is and what the scale factor is) in matplotlib and have it easily convert between "data space" and "plot space". Can someone please show a quick example or point me to the right place?
If you simply use matplotlib's plot function, the plot will fit into one online window, so you don't really need to 'rescale' explicitly. Linearly rescaling is pretty easy, if you include some code sample to show your formatting of the data, somebody can help you in translating the origin and scaling the coordinates.

Python plotting: How can I make matplotlib.pyplot stop forcing the style of my markers?

I am trying to plot a bunch of data points (many thousands) in Python using matplotlib so I need each marker to be very small and precise. How do I get the smallest most simple marker possible? I use this command to plot my data:
matplotlib.pyplot( x , y ,'.',markersize=0.1,linewidth=None,markerfacecolor='black')
Then I can look at it either with pl.show() and then save it. Or directly use plt.savefig('filename.ps') in the code to save it. The problem is this: when I use pl.show() to view the file in the GUI it looks great with small tiny black marks, however when I save from the show() GUI to a file or use directly savefig and then view the ps I created it looks different! Each marker has gained a little blue halo around it (as if it started at each point to connect them with the default blue lines, but did not) and the style is all wrong. Why does it change the style when saved? How do I stop python from forcing the style of the markers? And yes I have looked at some alternative packages like CairoPlot, but I want to keep using matplotlib for now.
Update: It turns out that the save to PNG first makes the colors turn out okay, but it forces a conversion of the image when I want to save it again as a .ps later (for inclusion in a PDF) and then I lose quality. How do I preserve the vector nature of the file and get the right formatting?
For nice-looking vectorized output, don't use the '.' marker style. Use e.g. 'o' (circle) or 's' (square) (see help(plot) for the options) and set the markersize keyword argument to something suitably small, e.g.:
plot(x, y, 'ko', markersize=2)
savefig('foo.ps')
That '.' (point) produces less nice results could be construed as a bug in matplotlib, but then, what should "point" mean in a vector graphic format?
Have you tried the ',' point shape? It creates "pixels" (small dots, instead of shapes).
You can play with the markersize option as well, with this shape?
If you haven't, you should try saving in a rasterizing engine -- save it to a PNG file and see if that fixes it. If you need a vector plot, try saving to PDF and converting with an external utility. I've also had problems before with the PS engine that were resolved by saving with the Agg or PDF engines and converting externally.

Categories