spacing value between value in x-axis matplotlib using python - python

I have a FPR and TPR plot. In here I want to ask how to arangge spacing value between the x-axis. My code is following below:
In [85]:
fig, ax = plt.subplots(figsize=(8,6), dpi=80)
ax.plot(x_iter1_TF , y_iter1_TF, label='Iter1', marker='o')
ax.plot(x_iter5_TF, y_iter5_TF ,label='Iter5', marker='v')
ax.plot(x_iter10_TF, y_iter10_TF , label='Iter10', marker='x')
ax.plot(x_iter25_TF, y_iter25_TF , label='Iter20', marker='+')
ax.plot(x_iter50_TF, y_iter50_TF , label='Iter50',marker='D')
ax.legend(loc=1); # upper left corner
ax.set_xlabel('FPR')
ax.set_ylabel('TPR')
ax.set_xlim([0,1, 0.001])
ax.set_ylim([0,1, 0.001])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-85-87b9ef379a9b> in <module>()
8 ax.set_xlabel('FPR')
9 ax.set_ylabel('TPR')
---> 10 ax.set_xlim([0,1, 0.001])
11 ax.set_ylim([0,1, 0.001])
C:\Python27\lib\site-packages\matplotlib\axes\_base.pyc in set_xlim(self, left, right, emit, auto, **kw)
2524
2525 if right is None and iterable(left):
-> 2526 left, right = left
2527
2528 self._process_unit_info(xdata=(left, right))
ValueError: too many values to unpack
In here I used ax.set_xlim([0,1, 0.001]) where 0.001 is spacing value between x-axis. Unfortunately, i faced an error. I think i did the wrong way to set those thing

As mentioned in my comment, set_xlim does not accept a "step" parameter. Also, the method I think you want is set_xticks, which can be used as follows:
In [13]: import numpy as np
...: ticks = np.arange(0, 2, 0.1)
...: ax.set_xticks(ticks)
...: fig
And gives the following result:

Related

Subplots in columns and rows

I would like my sybplots to be generated in 2x columns and 5x rows.
I've also tried adding ncols=2, nrows=5 to the code. didn't work.
And when I change the subplots to plt.subplots(5,2) instead of plt.subplots(10,1) it says (see added picture of code+error message):
AttributeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_9844/709244097.py in
13
14 for ax, afstand, tid in zip(ax, afstande, tider):
---> 15 ax.plot(tid, afstand)
16 ax.set_title("x(t)", fontsize=12)
17 ax.set_xlabel("tid (s)", fontsize=12)
AttributeError: 'numpy.ndarray' object has no attribute 'plot'
My code:
from scipy.optimize import fmin
a = -75.64766759
b = 68.02691163
f = lambda x: a * x + b
afstand1, afstand2, afstand3, afstand4, afstand5, afstand6, afstand7, afstand8, afstand9, afstand10 = f(U1), f(U2), f(U3), f(U4), f(U5),f(U6), f(U7), f(U8), f(U9), f(U10)
afstande = [afstand1, afstand2, afstand3, afstand4, afstand5, afstand6, afstand7, afstand8, afstand9, afstand10]
tider = [tid1, tid2, tid3, tid4, tid5, tid6, tid7, tid8, tid9, tid10]
fig, ax = plt.subplots(10,1, figsize=(7,25))
plt.subplots_adjust(hspace=0.55)
#loop
for ax, afstand, tid in zip(ax, afstande, tider):
ax.plot(tid, afstand)
ax.set_title("x(t)", fontsize=12)
ax.set_xlabel("tid (s)", fontsize=12)
ax.set_ylabel("Position", fontsize=12)
enter image description here
First of all, you're using the same variable name for the array of axis and in the loop, you should change that. Subplot-axes are stored in numpy arrays. If you only have 1 row, then looping over the array gives you the elements, but in a x*y pattern of subplots, you loop over a two-dimensional array of axis, which yields the rows. You can solve that by using .flat to get a one-dimensional view.
fig, axs = plt.subplots(ncols=5, nrows=2)
for ax in axs.flat:
ax.plot(...)

How to set fixed spaces between ticks in maptlotlib

I am preparing a graph of latency percentile results. This is my pd.DataFrame looks like:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
result = pd.DataFrame(np.random.randint(133000, size=(5,3)), columns=list('ABC'), index=[99.0, 99.9, 99.99, 99.999, 99.9999])
I am using this function (commented lines are different pyplot methods I have already tried to achieve my goal):
def plot_latency_time_bar(result):
ind = np.arange(4)
means = []
stds = []
for index, row in result.iterrows():
means.append(np.mean([row[0]//1000, row[1]//1000, row[2]//1000]))
stds.append(np .std([row[0]//1000, row[1]//1000, row[2]//1000]))
plt.bar(result.index.values, means, 0.2, yerr=stds, align='center')
plt.xlabel('Percentile')
plt.ylabel('Latency')
plt.xticks(result.index.values)
# plt.xticks(ind, ('99.0', '99.9', '99.99', '99.999', '99.99999'))
# plt.autoscale(enable=False, axis='x', tight=False)
# plt.axis('auto')
# plt.margins(0.8, 0)
# plt.semilogx(basex=5)
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
fig = plt.gcf()
fig.set_size_inches(15.5, 10.5)
And here is the figure:
As you can see bars for all percentiles above 99.0 overlaps and are completely unreadable. I would like to set some fixed space between ticks to have a same space between all of them.
Since you're using pandas, you can do all this from within that library:
means = df.mean(axis=1)/1000
stds = df.std(axis=1)/1000
means.plot.bar(yerr=stds, fc='b')
# Make some room for the x-axis tick labels
plt.subplots_adjust(bottom=0.2)
plt.show()
Not wishing to take anything away from xnx's answer (which is the most elegant way to do things given that you're working in pandas, and therefore likely the best answer for you) but the key insight you're missing is that, in matplotlib, the x positions of the data you're plotting and the x tick labels are independent things. If you say:
nominalX = np.arange( 1, 6 ) ** 2
y = np.arange( 1, 6 ) ** 4
positionalX = np.arange(len(y))
plt.bar( positionalX, y ) # graph y against the numbers 1..n
plt.gca().set(xticks=positionalX + 0.4, xticklabels=nominalX) # ...but superficially label the X values as something else
then that's different from tying positions to your nominal X values:
plt.bar( nominalX, y )
Note that I added 0.4 to the x position of the ticks, because that's half the default width of the bars bar( ..., width=0.8 )—so the ticks end up in the middle of the bar.

AttributeError: 'ErrorbarContainer' object has no attribute 'set_ylim'

I am plotting the results of some experiments with error bars. I'd like to be able to set the y limit in the case of results with extreme outliers that aren't interesting. This code:
axes = plt.errorbar(feature_data[feature_data.num_unique[feature_of_interest] > 1].index, chi_square_y, yerr=chi_square_y_error, fmt = 'o')
axes.set_ylim([-.2, .2])
plt.plot((min(feature_data[feature_data.num_unique[feature_of_interest] > 1].index), max(feature_data[feature_data.num_unique[feature_of_interest] > 1].index)), (0, 0), 'r--', linewidth = 2)
produces this error:
AttributeError Traceback (most recent call last)
<ipython-input-79-794286dd3c29> in <module>()
18 rcParams['figure.figsize'] = 10, 5
19 axes = plt.errorbar(feature_data[feature_data.num_unique[feature_of_interest] > 1].index, chi_square_y, yerr=chi_square_y_error, fmt = 'o')
---> 20 axes.set_ylim([-.2, .2])
21 plt.plot((min(feature_data[feature_data.num_unique[feature_of_interest] > 1].index), max(feature_data[feature_data.num_unique[feature_of_interest] > 1].index)), (0, 0), 'r--', linewidth = 2)
AttributeError: 'ErrorbarContainer' object has no attribute 'set_ylim'
How can I set the y limits?
Thanks!
Simply use the matplotlib.pyplot.ylim() function.
Your example is not self-contained, so I cannot check that the below code actually works, but at least the mentioned error will be fixed:
plt.errorbar(feature_data[feature_data.num_unique[feature_of_interest] > 1].index, chi_square_y, yerr=chi_square_y_error, fmt = 'o')
plt.ylim(-.2, .2)
plt.plot((min(feature_data[feature_data.num_unique[feature_of_interest] > 1].index), max(feature_data[feature_data.num_unique[feature_of_interest] > 1].index)), (0, 0), 'r--', linewidth = 2)
Since this is a bounty question I'll try to get a bit into more detail here.
plt.errorbar does not return an Axes object (which has the set_ylim method), but rather a collection of (plotline, caplines, barlinecols). I suspect you may have expected the Axes object since this is what pandas.DataFrame.plot returns.
When working directly with matplotlib's pyplot you have two options:
Option 1 - use pyplot directly, without dealing with the axes:
plt.errorbar( ... )
plt.ylim([-.2, .2])
Using plt will set properties to the last subplot selected (by default there is only one). You may prefer this method when dealing with a single figure.
Option 2 - get an axes object from subplots:
fig, ax = plt.subplots(1, 1, figsize=(10, 5))
ax.errorbar( ... )
ax.set_ylim([-.2, .2])
This is may preferred method, partly because it allows setting the figure size without setting it globally in rcParams. It has a few other advantages which I won't get into here.
Notice that when using plt the method is ylim and when using the Axes object it's set_ylim. This is true for many other properties such as titles, labels, etc.

How does the indexing of subplots work

I have the following:
import matplotlib.pyplot as plt
fig = plt.figure()
for i in range(10):
ax = fig.add_subplot(551 + i)
ax.plot([1,2,3,4,5], [10,5,10,5,10], 'r-')
I was imagining that the 55 means that it is creating a grid that is 5 subplots wide and 5 subplots deep - so can cater for 25 subplots?
The for loop will just iterate 10 times - so I thought (obviously wrongly) that 25 possible plots would accomodate those iterations ok but I get the following:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-118-5775a5ea6c46> in <module>()
10
11 for i in range(10):
---> 12 ax = fig.add_subplot(551 + i)
13 ax.plot([1,2,3,4,5], [10,5,10,5,10], 'r-')
14
/home/blah/anaconda/lib/python2.7/site-packages/matplotlib/figure.pyc in add_subplot(self, *args, **kwargs)
1003 self._axstack.remove(ax)
1004
-> 1005 a = subplot_class_factory(projection_class)(self, *args, **kwargs)
1006
1007 self._axstack.add(key, a)
/home/blah/anaconda/lib/python2.7/site-packages/matplotlib/axes/_subplots.pyc in __init__(self, fig, *args, **kwargs)
62 raise ValueError(
63 "num must be 1 <= num <= {maxn}, not {num}".format(
---> 64 maxn=rows*cols, num=num))
65 self._subplotspec = GridSpec(rows, cols)[int(num) - 1]
66 # num - 1 for converting from MATLAB to python indexing
ValueError: num must be 1 <= num <= 30, not 0
In the convience shorthand notation, the 55 does mean there are 5 rows and 5 columns. However, the shorthand notation only works for single-digit integers (i.e. for nrows, ncols and plot_number all less than 10).
You can expand it to full notation (i.e. use commas: add_subplot(nrows, ncols, plot_number)) and then all will work fine for you:
for i in range(10):
ax = fig.add_subplot(5, 5, 1 + i)
ax.plot([1,2,3,4,5], [10,5,10,5,10], 'r-')
From the docs for plt.subplot (which uses the same args as fig.add_subplot) :
Typical call signature:
subplot(nrows, ncols, plot_number)
Where nrows and ncols are used to notionally split the figure into nrows * ncols sub-axes, and
plot_number is used to identify the particular subplot that this
function is to create within the notional grid. plot_number starts at
1, increments across rows first and has a maximum of nrows * ncols.
In the case when nrows, ncols and plot_number are all less than 10, a convenience exists, such that the a 3 digit number can be given
instead, where the hundreds represent nrows, the tens represent ncols
and the units represent plot_number.
Although tom answered your question, in this sort of situation you should be using fig, axs = plt.subplots(n, m). This will create a new figure with the n rows and m columns of subplots. fig is the figure created. axs is a 2D numpy array where each element in the array is the subplot in the corresponding location in the figure. So the top-right element axs is the top-right subplot in the figure. You can access the subplots through normal indexing, or loop over them.
So in your case you can do
import matplotlib.pyplot as plt
# axs is a 5x5 numpy array of axes objects
fig, axs = plt.subplots(5, 5)
# "ravel" flattens the numpy array without making a copy
for ax in axs.ravel():
ax.plot([1,2,3,4,5], [10,5,10,5,10], 'r-')

How can I add a label to colorbar using ImageGrid?

In a previous question,
colobar label matplotlib in ImageGrid,
had a solution for adding a label to the colorbar, but this seems to be broken with the current version.
Platforms I have tried:
Mac w/ Canopy:
python: 2.7
matplotlib: 1.4.3-6
Linux:
python: 2.7
matplotlib: 1.3.1
Below is the code from the previous question, with some extra code for running in an iPython notebook:
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import AxesGrid
def get_demo_image():
import numpy as np
from matplotlib.cbook import get_sample_data
f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False)
z = np.load(f)
# z is a numpy array of 15x15
return z, (-3,4,-4,3)
def demo_grid_with_single_cbar(fig):
"""
A grid of 2x2 images with a single colorbar
"""
grid = AxesGrid(fig, 132, # similar to subplot(132)
nrows_ncols = (2, 2),
axes_pad = 0.0,
share_all=True,
label_mode = "L",
cbar_location = "top",
cbar_mode="single",
)
Z, extent = get_demo_image()
for i in range(4):
im = grid[i].imshow(Z, extent=extent, interpolation="nearest")
#plt.colorbar(im, cax = grid.cbar_axes[0])
#grid.cbar_axes[0].colorbar(im)
cbar = grid.cbar_axes[0].colorbar(im)
cbar.ax.set_label_text("$[a.u.]$")
for cax in grid.cbar_axes:
cax.toggle_label(False)
# This affects all axes as share_all = True.
grid.axes_llc.set_xticks([-2, 0, 2])
grid.axes_llc.set_yticks([-2, 0, 2])
#
F = plt.figure(1, (10.5, 2.5))
F.subplots_adjust(left=0.05, right=0.95)
demo_grid_with_single_cbar(F)
plt.draw()
plt.show()
The error message from the code is of the form:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-1-60ebdb832699> in <module>()
40 F = plt.figure(1, (10.5, 2.5))
41 F.subplots_adjust(left=0.05, right=0.95)
---> 42 demo_grid_with_single_cbar(F)
43
44 plt.draw()
<ipython-input-1-60ebdb832699> in demo_grid_with_single_cbar(fig)
29 #grid.cbar_axes[0].colorbar(im)
30 cbar = grid.cbar_axes[0].colorbar(im)
---> 31 cbar.ax.set_label_text("$[a.u.]$")
32
33 for cax in grid.cbar_axes:
AttributeError: 'CbarAxes' object has no attribute 'set_label_text'
Has the matplotlib interface changed since the original question was asked? If so, how do I add the colorbar label?
Personally, I've always perceived matplotlib as black magic, similar to TeX, so I cannot guarantee that my answer is the "official" way of doing what you want, or that it will continue to work in later versions. But thanks to this gallery example, I could devise the following incantation:
grid[0].cax.colorbar(im)
cax = grid.cbar_axes[0]
axis = cax.axis[cax.orientation]
axis.label.set_text("$[a.u.]$")
(don't forget to remove all your colorbar-related code). This works in the current matplotlib version (1.4.3). The result:

Categories