Is there a maximum amount of ticklabels in a matplotlib axes? - python-3.x

So I have two lists one containing a bunch of years and the other one containing some integers, each list has 17 values.
when I make a simple bar chart
plt.bar(keys,values)
plt.show()
in the X axis of the graph it only contains some of the years in the keys list eg: the graph only has 2000,2002,2005,2007,2010,2012,2015. It has missed 2001,2003,2006,2008,2009 etc.
Is it because there is a maximum amount of keys allowed in the bar chart so it randomly took some keys?
If not how do i fix this?

There is a maximum amount of ticklabels on a matplotlib axes. This limit however lies well above 1000 and you would first run into severe lags when creating the figure.
The usual automatic ticking by matplotlib is that the axes are equipped with just as many labels as needed. I.e. if you plot 50 points on a plot, you would not want to have 50 labels as well. Further if you plot a point at 0.853164 you would not want to have such odd number being displayed as ticklabel on the axes.
Now, I cannot think of any reason matplotlib would produce the labels you report about, 2000,2002,2005,2007,2010,2012,2015, because the automatic locator for the ticks chooses equidistant points on the axes. For any help with this specific problem, a MCVE would be needed.
But in general there are two concepts from which you may choose.
Numerical axes
When plotting numbers, matplotlib will by default choose a linear axes and tick it automatically as described above.
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(2000, 2017)
y = np.random.randint(5,21, size=len(x))
plt.bar(x,y)
plt.show()
In this case an equidistant ticking of 2.5 units is chosen to have 7 nicely spaced ticks on the axes. If instead you would want to have every bar ticked, you could use a custom ticker.
E.g. a MultipleLocator with the interval set to 1,
import matplotlib.ticker as mticker
plt.gca().xaxis.set_major_locator(mticker.MultipleLocator(1))
plt.gca().tick_params(axis="x", rotation=90)
Or, a FixedLocator with the locations set to the x values of the bars,
import matplotlib.ticker as mticker
plt.gca().xaxis.set_major_locator(mticker.FixedLocator(x))
plt.gca().tick_params(axis="x", rotation=90)
Categorical axes
You may also decide that your xaxis shall be categorical. This means that every unique value gets its own tick and those ticks are equally spaced, independent of their value. This is easiest accomplished by converting the numbers to strings.
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(2000, 2017)
y = np.random.randint(5,21, size=len(x))
cats = list(map(str, x))
plt.bar(cats,y)
plt.gca().tick_params(axis="x", rotation=90)
plt.show()
The result is visually the same as above, but this time, the number 2000 is not at location 2000, but at its index 0, 2001 is at 1 and so on.

You can show all the ticks in this way:
plt.xticks(np.arange(min(keys), max(keys)+1, 1.0), rotation=45)
Example:
keys = [2000, 2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016]
values = range(1,18)
import matplotlib.pyplot as plt
plt.bar(keys,values)
plt.xticks(np.arange(min(keys), max(keys)+1, 1.0), rotation=45)
plt.show()

Related

Is there a way to plot Matplotlib's Imshow against changing x-axis limits and y-axis limits?

I'm trying to visualize a numpy array using imshow() since its similar to imagesc() in MATLAB.
import numpy as np
import matplotlib.pyplot as plt
plt.imshow(np.random.rand(8, 90), aspect='auto')
The resulting figure is as below with automatic selection of x-axis limits & y-axis limits. How could I set the parameters to change the x-axis & y-axis, which are of 2 different sizes of array of elements?
For Eg: x-axis = [100,], y-axis = [15,]
I tried using extent, however I need to provide [x-axis_min, x-axis_max, y-axis_min, y-axis_max]. But the problem is, I am having an array of elements as x-axis limits and an array of elements as y-axis limits. I don't know how to set these arrays as limits?
Thanks
If I understand your question, then this should do the trick?
img = np.random.rand(8, 90)
x = np.linspace(2.2e10, 2.475e10, 100)
y = np.linspace(-0.007, 0.007, 15)
plt.figure()
plt.imshow(img, aspect='auto', extent=[np.min(x), np.max(x), np.max(y), np.min(y)])

Equivalent of pcolormesh for irregular points

I am using pcolormesh on a 2-D NumPY array of points M, so
pcolormesh(X,Y,M)
plots a grid of colors where the X-axis range labels correspond to X[i], Y-axis range labels correspond to Y[j], and the color plotted at point (i,j) corresponds to the level of M[i,j].
I would also like to plot the same thing but where I have a 1-D array M[i], and the color plotted at point (X[i], Y[i]) corresponds to the level of M[i].
I don't see any out of the box solution for this in matplotlib. Is there one? This is the closest I could come up with, taking a cue from an answer to this question:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
def intensityplot(x,y,z):
z=z/z.max()
colors = cm.rainbow(z)
for X,Y,Z in zip(x,y,colors):
plt.scatter([X],[Y], color=Z)

Cannot format the ticklabel in the twin x axis in matplotlib.pyplot

I would like to plot a figure with double x axis, and format the ticklabel in the upper axis to scientific notations.
import numpy as np
import matplotlib.pyplot as plt
imp1=np.arange(0,2,2/50)
imp1_pdf=np.arange(0,6,6/50)
fig1=plt.figure()
axs1=fig1.add_subplot(111)
axs1.set_xlim(0,2)
axs1.set_ylim(0,6.5)
axs2 = axs1.twiny()
axs1.plot(imp1,imp1_pdf)
new_tick_locations=axs1.get_xticks()
axs2.set_xticks(new_tick_locations)
axs2.set_xticklabels(new_tick_locations/1000)
axs2.axes.ticklabel_format(axis='x',style='sci',scilimits=(0,0))
axs1.grid(b=True, which='major',linestyle='-')
fig1.tight_layout()
fig1.savefig('tickformat.png',dpi=600)
Without the ticklabel formatting, the figure looks like this:
But when I try to format the upper x axis, there is an error like this:
AttributeError: This method only works with the ScalarFormatter.
If I use an alternative method, which is to use the FormatStrFormatter
from matplotlib.ticker import FormatStrFormatter
axs2.xaxis.set_major_formatter(FormatStrFormatter('%.1e'))
The upper x axis value will become the same with the lower x axis value like this:
Could someone tell me how to solve this problem please?
The problem is you are trying to modify the custom labels which are just strings you defined (new_tick_locations/1000). The real values on the twin axis are the same as that on the lower axis. You are just modifying the tick labels. One way to get things done is to construct modified tick labels in Scientific Format using Decimal and then assign them to the upper x-axis. You can then choose any factor instead of 1000, which you want to display
import numpy as np
from decimal import Decimal
import matplotlib.pyplot as plt
imp1=np.arange(0,2,2/50)
imp1_pdf=np.arange(0,6,6/50)
fig1=plt.figure()
axs1=fig1.add_subplot(111)
axs1.set_xlim(0,2)
axs1.set_ylim(0,6.5)
axs2 = axs1.twiny()
axs1.plot(imp1,imp1_pdf)
new_tick_locations=axs1.get_xticks()
ticks = ['%.2E' % Decimal(i) for i in (new_tick_locations/1000)] # <-- make new ticks
axs2.set_xticks(new_tick_locations)
axs2.set_xticklabels(ticks, rotation = 45) # <-- assign new ticks and rotate them
axs1.grid(b=True, which='major',linestyle='-')
fig1.tight_layout()

Plot several boxplots in one figure

I am using python-3.x and I would like to plot several boxplots in one figure, all the data from one numpy array where the shape of this array is (100, 301)
If I use the code below it will plot them all (I will have 301 boxplots in one figure which is too much)
fig, ax = plt.subplots()
ax.boxplot(my_data)
plt.show()
I don't want to plot all the data, I just want to plot 10, 15 or 20 (variable number) of the data by using for loop or any method that work best.
for example, I want to plot boxplots every 50 number of data that mean I will have around 6 boxplots from 301 in my figure, I tried to use for loop but no luck
Any advice would be much appreciated
You can just use indexing to plot every 50th data points using a variable step. To have separate box plots and avoid overlapping, you can specify the positions of individual box plot using the positions parameter. my_data[:, ::step] gives you the desired data to plot. Below is an example using some random data.
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
my_data = np.random.randint(0, 20, (100, 301))
step = 50
posit = range(my_data[:, ::step].shape[1])
ax.boxplot(my_data[:, ::step], positions=posit)
plt.show()

Matplotlib: personalize imshow axis

I have the results of a (H,ranges) = numpy.histogram2d() computation and I'm trying to plot it.
Given H I can easily put it into plt.imshow(H) to get the corresponding image. (see http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.imshow )
My problem is that the axis of the produced image are the "cell counting" of H and are completely unrelated to the values of ranges.
I know I can use the keyword extent (as pointed in: Change values on matplotlib imshow() graph axis ). But this solution does not work for me: my values on range are not growing linearly (actually they are going exponentially)
My question is: How can I put the value of range in plt.imshow()? Or at least, or can I manually set the label values of the plt.imshow resulting object?
Editing the extent is not a good solution.
You can just change the tick labels to something more appropriate for your data.
For example, here we'll set every 5th pixel to an exponential function:
import numpy as np
import matplotlib.pyplot as plt
im = np.random.rand(21,21)
fig,(ax1,ax2) = plt.subplots(1,2)
ax1.imshow(im)
ax2.imshow(im)
# Where we want the ticks, in pixel locations
ticks = np.linspace(0,20,5)
# What those pixel locations correspond to in data coordinates.
# Also set the float format here
ticklabels = ["{:6.2f}".format(i) for i in np.exp(ticks/5)]
ax2.set_xticks(ticks)
ax2.set_xticklabels(ticklabels)
ax2.set_yticks(ticks)
ax2.set_yticklabels(ticklabels)
plt.show()
Expanding a bit on #thomas answer
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mi
im = np.random.rand(20, 20)
ticks = np.exp(np.linspace(0, 10, 20))
fig, ax = plt.subplots()
ax.pcolor(ticks, ticks, im, cmap='viridis')
ax.set_yscale('log')
ax.set_xscale('log')
ax.set_xlim([1, np.exp(10)])
ax.set_ylim([1, np.exp(10)])
By letting mpl take care of the non-linear mapping you can now accurately over-plot other artists. There is a performance hit for this (as pcolor is more expensive to draw than AxesImage), but getting accurate ticks is worth it.
imshow is for displaying images, so it does not support x and y bins.
You could either use pcolor instead,
H,xedges,yedges = np.histogram2d()
plt.pcolor(xedges,yedges,H)
or use plt.hist2d which directly plots your histogram.

Resources