My current code produces overlapping figures for each element in the list test. How can I produce two separate figures using list comprehension? (Preferably using just one line of code).
test=[[1, 2, 3, 4],[5, 6, 7, 8]]
[plt.plot(test[k]) for k in range(0,2)]
Current output looks like this:
As explained in the Matplotlib Usage Guide, plt.plot works the same way as the ax.plot graphing method:
for each Axes graphing method, there is a corresponding function in the matplotlib.pyplot module that performs that plot on the "current" axes, creating that axes (and its parent figure) if they don't exist yet.
So when you run the list comprehension you have given as an example, the first plt.plot method call in the for loop creates a new figure object containing a child Axes object. Then, as noted in the Matplotlib Pyplot tutorial section on working with multiple figures and axes:
MATLAB, and pyplot, have the concept of the current figure and the current axes. All plotting functions apply to the current axes.
This is why all the subsequent plt.plot calls in the loop are being plotted in the same figure. To draw the lines in separate figures, a new figure must be created for each plt.plot call, like so:
test = [[1, 2, 3, 4],[5, 6, 7, 8]]
for k in range(len(test)): plt.figure(), plt.plot(test[k])
Edit: based on the comment by JohanC (on the question), replaced list comprehension with a single-line compound statement. Even though this goes against PEP 8 recommendations, it does indeed make the code more readable while meeting the one-line requirement.
Related
Could someone please guide me on how should I make sure that all ticks (or maybe a better way to specify will be to say all elements in the list passed to plot function) are displayed on the x axis when using matplotlib to plot graphs?
plt.plot(xValues, meanWeekdayArrivalCounts, 'k-')
I want all the values in the list xValues to show up on the graph. By default, only, 0, 10, 20, 30, 40, 50 show up.
Simply add plt.xticks(xValues) to your code. Given the number of points in your graph, the labels might clutter.
You could display them as minor ticks if you set them on the axes object with ax.set_xticks(xValues, minor=True).
use this.
fig = plt.figure(figsize=(10,8))
plt.xticks(np.arange(min(x), max(x)+1, 1.0))
I wanted to show all the years in my graph so I did
plt.xticks(list(table_05.anio.unique()))
link to code and files
1.I =imread('one.jpg');
2.I = inresize(I,[20,20]);
3.I=im2double(I);
4.I=mean(I,3);
#This next line
5.a = reshape(I,[],400);
I read an image and resizzed it to 20*20 and then converted it to matrix and then find the grayscale .All this I can do in Python too....but I can't do the 5 th line of code...if I tried ,
reshape (I,1,400)...the image appears rotated...I don't know how to write the 5 the line as above in python
The problem
in the link along with the code theres is a displayData function.I saved the matrix i got using python as mat and loaded it on octave when i called displayData() on the matrix i got a rotated image.thats inclued in the link.And theres no such problem in octave.Thank you for looking into this.
For reshaping an array you can use numpy, and, following your code, you can use reshape. In your case, you are changing the size of I, from (20,20) to (1,400).
A complete example which saves the resulting reshaped array to a mat file, using OpenCV APIs for dealing with images, is:
import numpy as np
import cv2
import scipy.io
I = cv2.imread('one.jpg')
I = cv2.resize(I,(20,20))
I = cv2.normalize(I.astype('float'), None, 0.0, 1.0, cv2.NORM_MINMAX)
I = np.mean(I, axis=2)
a = np.reshape(I, (1,400), order='F')
scipy.io.savemat('a.mat', mdict={'a': a})
Note the second parameter of reshape, which is a tuple containing the new size of the array. Also, notice the third parameter order that allows to rearrange elements in column-major style (Fortran) which is the convention used by octave (see reshape in octave http://www.gnu.org/software/octave/doc/v4.0.1/Rearranging-Matrices.html#XREFreshape). This results in a correct image, non rotated, compared to the one got from octave.
However, given the fact that you want to get from a 2d array a 1d array, you can use, from numpy, ravel if you want to get a view of I (when possible), namely a modification of a changes also I; or flatten, which returns a copy of I, thus modifying a does not change I. However, note that both ravel and flatten returns a 1d array resulting in a size of (400,). The same order parameter should be used.
For some reason, I just can't get this to work. What I end up getting is two grids with axes plotted on top of each other and then the second plot plotted under this. My code works fine for a seaborn distplot:
plt.subplot(121)
h = sns.distplot(dfmain["Runs"])
plt.subplot(122)
j = sns.distplot(dfHighScores["Runs"])
But if I try and plot two factor plots side by side (they are independent so I don't want the axes to be shared), then this just doesn't work as described above:
plt.subplot(121)
h = sns.factorplot("Runs",data=dfmain)
plt.subplot(122)
j = sns.factorplot("Runs",data=dfHighScores)
I get this behavior whether I use an iPython notebook or spyder as my IDE. Any help would be greatly appreciated.
According to the documentation of factorplot it should be able do draw two plots side by side by itself. If you combine both your data frames into one (and add a column to tell between them) then something like this should work:
sns.factorplot("Runs", col="mainOrHighScores", data=dfCombined)
Otherwise I think factorplot doesn't operate on axes (unlike distplot).
In any case only functions that have an "ax" parameter for axes can draw on axes.
I'm confused by the relationship among matplotlib figures, axes, and subplots.
Usually, I figure out such things by looking at and experimenting with code, which typically embodies the structural relationship among entities in a object model that can be inferred from examples of what works. But in matplotlib I often find a bewildering array of ways to accomplish the same thing, which obscures the underling structure.
For example, if I want to make a simple (no subfigures) log-log figure, any of the following seem to have exactly the same effect.
import matplotlib.pyplot as plt
# All of the following seem to have the same effect:
plt.axes().loglog()
plt.gca().loglog()
plt.loglog()
plt.gcf().gca().loglog()
# These don't work though:
# plt.gcf().axes().loglog()
# plt.gcf().loglog()
I've tried the documentation and the tutorials, but I'm no wiser having done so.
What does each of the working examples above do? How to they differ? Why do the non-working examples fail? If I'm writing code that I expect others (or me) to be able to read, is one of these idioms preferred over another?
Note that my interest here is in programmatically creating images for publication or export rather than in the interactive creation of figures or in mimicking MATLABs functionality. I gather that some of the "shortcuts" above have to do with making this latter scenario work.
My standard is to get fig, ax from plt.subplots like this:
fig, ax = plt.subplots(1)
ax.loglog(a, b)
I do it this way because then you can also get multiple ax objects as a list, e.g.:
# Make a column of three figures
fig, axes = plt.subplots(3)
for ax, a, b in zip(axes, as, bs):
ax.loglog(a, b)
Or if you do a 2 by 5 grid, you get a list of lists of ax objects, so I usually unlist the list using axes.flat:
# Make a 2x5 grid of figures
nrows = 2
ncols = 5
height = nrows * 4
width = ncols * 4
# Don't ask me why figsize is (width, height) instead of (height, width)....
fig, axes = plt.subplots(nrows=2, ncols=5, figsize=(width, height))
for ax, a, b in zip(axes.flat, as, bs):
ax.loglog(a, b)
I do it this way because then I have the ax object to tweak with the appearance with afterwards. I generally don't use plt.gca() except for internal plotting functions.
plt.gcf() is getting the current figure and when you add gca() or axes() or loglog() to it, I believe they create the underlying axes. I'm not sure why the gcf()-first stuff didn't work with axes() and loglog(). So my advice is to stick to ax objects.
EDIT: removed itertools.chain stuff, swapped to axes.flat
A figure is basically a window or a file. If you make several separate figures, the idea is usually to pop up several widows or save several files.
An axis and a subplot are in some sense the same thing. For example, the figure method subplot returns an axis object. Each axis object represents a specific set of axes that you want to plot something on. Each axis can have several individual data sets plotted on it, but they will all use the same x and y axes.
Making a plot a loglog plot is determined by the function that you use to actually plot the data. For example, if you have two arrays a and b that I want to loglog plot against each other, I would use:
fig=plt.figure() #Make a figure
loglog_ax=fig.subplot(111) # Make a single axis, which is the *only* subplot
loglog_ax.loglog(a,b) # Plot the data on a log-log plot
Current situation: I'm trying to extract segments from an image. Thanks to openCV's findContours() method, I now have a list of 8-connected point for every contours. However, these lists are not directly usable, because they contain a lot of duplicates.
The problem: Given a list of 8-connected points, which can contain duplicates, extract segments from it.
Possible solutions:
At first, I used openCV's approxPolyDP() method. However, the results are pretty bad... Here is the zoomed contours:
Here is the result of approxPolyDP(): (9 segments! Some overlap)
but what I want is more like:
It's bad because approxPolyDP() can convert something that "looks like several segments" in "several segments". However, what I have is a list of points that tend to iterate several times over themselves.
For example, if my points are:
0 1 2 3 4 5 6 7 8
9
Then, the list of point will be 0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 9... And if the number of points become large (>100) then the segments extracted by approxPolyDP() are unfortunately not duplicates (i.e : they overlap each other, but are not strictly equal, so I can't just say "remove duplicates", as opposed to pixels for example)
Perhaps, I've got a solution, but it's pretty long (though interesting). First of all, for all 8-connected list, I create a sparse matrix (for efficiency) and set the matrix values to 1 if the pixel belongs to the list. Then, I create a graph, with nodes corresponding to pixels, and edges between neighbouring pixels. This also means that I add all the missing edges between pixels (complexity small, possible because of the sparse matrix). Then I remove all possible "squares" (4 neighbouring nodes), and this is possible because I am already working on pretty thin contours. Then I can launch a minimal spanning tree algorithm. And finally, I can approximate every branch of the tree with openCV's approxPolyDP()
To sum up: I've got a tedious method, that I've not yet implemented as it seems error-prone. However, I ask you, people at Stack Overflow: are there other existing methods, possibly with good implementations?
Edit: To clarify, once I have a tree, I can extract "branches" (branches start at leaves or nodes linked to 3 or more other nodes) Then, the algorithm in openCV's approxPolyDP() is the Ramer–Douglas–Peucker algorithm, and here is the Wikipedia picture of what it does:
With this picture, it is easy to understand why it fails when points may be duplicates of each other
Another edit: In my method, there is something that may be interesting to note. When you consider points located in a grid (like pixels), then generally, the minimal spanning tree algorithm is not useful because there are many possible minimal trees
X-X-X-X
|
X-X-X-X
is fundamentally very different from
X-X-X-X
| | | |
X X X X
but both are minimal spanning trees
However, in my case, my nodes rarely form clusters because they are supposed to be contours, and there is already a thinning algorithm that runs beforehand in the findContours().
Answer to Tomalak's comment:
If DP algorithm returns 4 segments (the segment from the point 2 to the center being there twice) I would be happy! Of course, with good parameters, I can get to a state where "by chance" I have identical segments, and I can remove duplicates. However, clearly, the algorithm is not designed for it.
Here is a real example with far too many segments:
Using Mathematica 8, I created a morphological graph from the list of white pixels in the image. It is working fine on your first image:
Create the morphological graph:
graph = MorphologicalGraph[binaryimage];
Then you can query the graph properties that are of interest to you.
This gives the names of the vertex in the graph:
vertex = VertexList[graph]
The list of the edges:
EdgeList[graph]
And that gives the positions of the vertex:
pos = PropertyValue[{graph, #}, VertexCoordinates] & /# vertex
This is what the results look like for the first image:
In[21]:= vertex = VertexList[graph]
Out[21]= {1, 3, 2, 4, 5, 6, 7, 9, 8, 10}
In[22]:= EdgeList[graph]
Out[22]= {1 \[UndirectedEdge] 3, 2 \[UndirectedEdge] 4, 3 \[UndirectedEdge] 4,
3 \[UndirectedEdge] 5, 4 \[UndirectedEdge] 6, 6 \[UndirectedEdge] 7,
6 \[UndirectedEdge] 9, 8 \[UndirectedEdge] 9, 9 \[UndirectedEdge] 10}
In[26]:= pos = PropertyValue[{graph, #}, VertexCoordinates] & /# vertex
Out[26]= {{54.5, 191.5}, {98.5, 149.5}, {42.5, 185.5},
{91.5, 138.5}, {132.5, 119.5}, {157.5, 72.5},
{168.5, 65.5}, {125.5, 52.5}, {114.5, 53.5},
{120.5, 29.5}}
Given the documentation, http://reference.wolfram.com/mathematica/ref/MorphologicalGraph.html, the command MorphologicalGraph first computes the skeleton by morphological thinning:
skeleton = Thinning[binaryimage, Method -> "Morphological"]
Then the vertex are detected; they are the branch points and the end points:
verteximage = ImageAdd[
MorphologicalTransform[skeleton, "SkeletonEndPoints"],
MorphologicalTransform[skeleton, "SkeletonBranchPoints"]]
And then the vertex are linked after analysis of their connectivity.
For example, one could start by breaking the structure around the vertex and then look for the connected components, revealing the edges of the graph:
comp = MorphologicalComponents[
ImageSubtract[
skeleton,
Dilation[vertices, CrossMatrix[1]]]];
Colorize[comp]
The devil is in the details, but that sounds like a solid starting point if you wish to develop your own implementation.
Try math morphology. First you need to dilate or close your image to fill holes.
cvDilate(pimg, pimg, NULL, 3);
cvErode(pimg, pimg, NULL);
I got this image
The next step should be applying thinning algorithm. Unfortunately it's not implemented in OpenCV (MATLAB has bwmorph with thin argument). For example with MATLAB I refined the image to this one:
However OpenCV has all needed basic morphological operations to implement thinning (cvMorphologyEx, cvCreateStructuringElementEx, etc).
Another idea.
They say that distance transform seems to be very useful in such tasks. May be so.
Consider cvDistTransform function. It creates to an image like that:
Then using something like cvAdaptiveThreshold:
That's skeleton. I guess you can iterate over all connected white pixels, find curves and filter out small segments.
I've implemented a similar algorithm before, and I did it in a sort of incremental least-squares fashion. It worked fairly well. The pseudocode is somewhat like:
L = empty set of line segments
for each white pixel p
line = new line containing only p
C = empty set of points
P = set of all neighboring pixels of p
while P is not empty
n = first point in P
add n to C
remove n from P
line' = line with n added to it
perform a least squares fit of line'
if MSE(line) < max_mse and d(line, n) < max_distance
line = line'
add all neighbors of n that are not in C to P
if size(line) > min_num_points
add line to L
where MSE(line) is the mean-square-error of the line (sum over all points in the line of the squared distance to the best fitting line) and d(line,n) is the distance from point n to the line. Good values for max_distance seem to be a pixel or so and max_mse seems to be much less, and will depend on the average size of the line segments in your image. 0.1 or 0.2 pixels have worked in fairly large images for me.
I had been using this on actual images pre-processed with the Canny operator, so the only results I have are of that. Here's the result of the above algorithm on an image:
It's possible to make the algorithm fast, too. The C++ implementation I have (closed source enforced by my job, sorry, else I would give it to you) processed the above image in about 20 milliseconds. That includes application of the Canny operator for edge detection, so it should be even faster in your case.
You can start by extraction straight lines from your contours image using HoughLinesP which is provided with openCV:
HoughLinesP(InputArray image, OutputArray lines, double rho, double theta, int threshold, double minLineLength = 0, double maxLineGap = 0)
If you choose threshold = 1 and minLineLenght small, you can even obtain all single elements. Be careful though, since it yields many results in case you have many edge pixels.