Overlapping axis label with length distribution - numpy
I'm a newbie in python plot, I want to plot the lists with this code:
import numpy as np
import matplotlib.pyplot as plt
alphab = [172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,280,281,282,283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299,300,301,302,303,304,305,306,307,308,309,310,311,312,313,314,315,316,317,318,319,320,321,322,323,324,325,326,327,328,329,330,331,332,333,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,358,359,360,361,362,363,364,365,366,367,368,369,370,371,372,373,374,375,376,377,378,379,380,381,382,383,384,385,386,387,388,389,390,391,392,393,394,395,396,397,398,399,400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,419,420,421,422,423,424,425,426,427,428,429,430,431,432,433,434,435,436,437,438,439,440,441,442,443,444,445,446,447,448,449,450,451,452,453,454,455,456,457,458,459,460,461,462,463,464,465]
frequencies = [24,17,21,27,10,21,26,41,23,27,25,22,21,24,31,24,19,18,27,15,29,28,22,35,35,28,30,20,29,42,39,35,30,29,38,32,35,47,30,44,55,34,41,41,46,56,39,39,57,39,58,44,51,52,51,44,57,48,50,59,54,46,64,63,56,60,74,72,75,72,60,75,74,55,75,69,70,69,73,69,63,80,70,74,62,77,69,78,70,68,68,80,71,77,79,64,83,76,64,92,77,93,86,65,88,86,79,91,79,97,87,67,83,96,94,79,102,114,89,92,90,112,100,107,98,95,99,95,96,91,103,111,85,105,113,103,105,95,110,103,111,102,102,117,127,128,110,100,122,99,126,99,113,114,133,129,118,120,105,121,112,115,118,127,109,116,96,101,98,98,94,114,94,87,83,117,87,105,120,116,96,112,92,106,115,107,98,107,87,86,111,108,113,106,109,102,89,81,102,87,124,127,116,106,98,106,117,95,113,107,121,92,102,97,94,94,122,110,101,118,112,106,95,112,115,102,136,114,125,136,126,120,116,119,140,114,125,148,126,137,140,129,134,124,141,126,127,124,162,124,137,136,137,142,156,131,153,150,139,131,143,119,145,142,135,151,117,143,151,146,149,125,109,124,135,144,125,127,161,120,158,112,129,125,134,131,130,122,118,145,132,123,131,129]
pos = np.arange(len(alphab))
plt.bar(pos, frequencies)
plt.xticks(pos, alphab, rotation=90)
plt.show()
but I get the following:
how could I get this?
The lists, are length distribution, e.g, 172 appears 24 times,..., 465 appear 129 times.
Thanks for your help.
option 1
Let plt figure it out
plt.bar(pos, frequencies)
# plt.xticks(pos, alphab, rotation=90)
plt.show()
option 2
mess with the steps
plt.bar(pos, frequencies)
plt.xticks(pos[::50], alphab, rotation=90)
plt.show()
Related
Scale Y axis of matplotlib plot in jupyter notebook
I want to scale Y axis so that I can see values, as code below plots cant see anything other than a thin black line. Changing plot height doesn't expand the plot. import numpy as np import matplotlib.pyplot as plt data=np.random.random((4,10000)) plt.rcParams["figure.figsize"] = (20,100) #or swap line above with one below, still no change in plot height #fig=plt.figure(figsize=(20, 100)) plt.matshow(data) plt.show() One way to do this is just repeat the values then plot result, but I would have thought it possible to just scale the height of the plot? data_repeated = np.repeat(data, repeats=1000, axis=0)
You can do it like this: import numpy as np import matplotlib.pyplot as plt data=np.random.random((4, 10000)) plt.figure(figsize=(40, 10)) plt.matshow(data, fignum=1, aspect='auto') plt.show() Output:
Is it possible to do draw plot in matplotlib where all points are linked to the x axis?
I am trying to find a way in matplotlib to draw a lineplot, except that I don't want to draw a line between points. Instead I want to draw a perpendicular line between each of my points and the x axis. When I do a standard plot, I obtain the following : import numpy as np import matplotlib.pyplot as plt data = np.array([0,1,3,2,3,1,4]) plt.plot(data) plt.xlim([-0.2,6.2]) plt.ylim([-0.2,5]) Instead I want to obtain the following : Any ideas how to do this ? Thanks
There are two other options apart from stem and bar chart is the following using vlines() and LineCollection() Option 1 -- Using vlines() for x, y in enumerate(data): plt.vlines(x=x, ymin=0, ymax=y, color='r') Or in a single line without using loops plt.vlines(x=range(data.size), ymin=0, ymax=data, color='r') Option 2 -- Using LineCollection() from matplotlib.collections import LineCollection lines = [[(x, 0), (x, y)] for x, y in enumerate(data)] linesCol = LineCollection(lines, linewidths=3, color='r') fig, ax = plt.subplots() ax.add_collection(linesCol) plt.scatter(range(len(data)), data, s=0)
changing the size of subplots with matplotlib
I am trying to plot multiple rgb images with matplotlib the code I am using is: import numpy as np import matplotlib.pyplot as plt for i in range(0, images): test = np.random.rand(1080, 720,3) plt.subplot(images,2,i+1) plt.imshow(test, interpolation='none') the subplots appear tiny though as thumbnails How can I make them bigger? I have seen solutions using fig, ax = plt.subplots() syntax before but not with plt.subplot ?
plt.subplots initiates a subplot grid, while plt.subplot adds a subplot. So the difference is whether you want to initiate you plot right away or fill it over time. Since it seems, that you know how many images to plot beforehand, I would also recommend going with subplots. Also notice, that the way you use plt.subplot you generate empy subplots in between the ones you are actually using, which is another reason they are so small. import numpy as np import matplotlib.pyplot as plt images = 4 fig, axes = plt.subplots(images, 1, # Puts subplots in the axes variable figsize=(4, 10), # Use figsize to set the size of the whole plot dpi=200, # Further refine size with dpi setting tight_layout=True) # Makes enough room between plots for labels for i, ax in enumerate(axes): y = np.random.randn(512, 512) ax.imshow(y) ax.set_title(str(i), fontweight='bold')
plot shuffled array numpy
I am writting a very simple script, one that plot a sin using jupyter notebook (python 3). when I put: import numpy import matplotlib.pyplot as plt x=np.arange(0.0,5*np.pi,0.001) y = np.sin(x) plt.plot(x,y) The plot is fine. However if : import numpy import matplotlib.pyplot as plt x=np.arange(0.0,5*np.pi,0.001) np.random.shuffle(x) y = np.sin(x) plt.plot(x,y) the image is I don't understand why shuffling the x BEFORE I ran sin does it. thank you
Let's first simplify things a bit. We plot 4 points and annote them with the order in which they are plotted. import numpy as np; np.random.seed(42) import matplotlib.pyplot as plt x=np.arange(4) y = np.sin(x) plt.plot(x,y, marker="o") for i, (xi,yi) in enumerate(zip(x,y)): plt.annotate(str(i), xy=(xi,yi), xytext=(0,4), textcoords="offset points", ha="center") plt.show() No if we shuffle x and plot the same graph, x=np.arange(4) np.random.shuffle(x) y = np.sin(x) we see that positions of the points are still are the same, but while e.g. previously the first point was the one at (0,0), it's now the third one appearing there. Due to this randomized order, the connecting lines go zickzack. Now if you use enough points, all those lines will add up to look like a complete surface, which is what you get in your image.
Tick labels displaying outside axis limits
Is there a way to automatically not display tick mark labels if they would protrude past the axis itself? For example, consider the following code #!/usr/bin/python import pylab as P, numpy as N, math as M xvals=N.arange(-10,10,0.1) yvals=[ M.sin(x) for x in xvals ] P.plot( xvals, yvals ) P.show() See how the -10 and 10 labels on the x-axis poke out to the left and right of the plot? And similar for the -1.0 and 1.0 labels on the y-axis. Can I automatically suppress plotting these but retain the ones that do not go outside the plot limits?
I think you could just format the axis ticks yourself and then prune the ones that are hanging over. The recommended way to deal with setting up the axis is to use the ticker API. So for example from matplotlib.ticker import MaxNLocator import matplotlib.pyplot as plt import numpy as np fig = plt.figure() ax = fig.add_subplot(111) xvals=np.arange(-10,10,0.1) yvals=[ np.sin(x) for x in xvals ] ax.plot( xvals, yvals ) ax.xaxis.set_major_locator(MaxNLocator(prune='both')) plt.show() Here we are creating a figure and axes, plotting the data, and then setting the xaxis major ticks. The formatter MaxNLocator is given the argument prune='both' which is described in the docs here. This is not exactly what you were asking for, but maybe it will solve your problem.