Asked  4 Months ago    Answers:  5   Viewed   1.9k times

I am trying to make a simple box plot of a variable 'x' contained in two dataframes, df1 and df2. To do this I am using the following code:

fig, axs = plt.subplots()
axs[0, 0].boxplot([df1['x'], df2['x']])
plt.show();

However, I get this:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-108-ce962754d553> in <module>()
----> 2 axs[0, 0].boxplot([df1['x'], df2['x']])
      3 plt.show();
      4 

TypeError: 'AxesSubplot' object is not subscriptable

Any ideas?

 Answers

92
fig, axs = plt.subplots()

returns a figure with only one single subplot, so axs already holds it without indexing.

fig, axs = plt.subplots(3)

returns a 1D array of subplots.

fig, axs = plt.subplots(3, 2)

returns a 2D array of subplots.

Note that this is only due to the default setting of the kwarg squeeze=True.
By setting it to False you can force the result to be a 2D-array, independant of the number or arrangement of the subplots.

Friday, August 13, 2021
 
LOKESH
answered 4 Months ago
46

I think you want

listb.pop()[0]

The expression listb.pop is a valid python expression which results in a reference to the pop method, but doesn't actually call that method. You need to add the open and close parentheses to call the method.

Wednesday, June 30, 2021
 
bumperbox
answered 6 Months ago
23

The expression list[int] is attempting to subscript the object list, which is a class. Class objects are of the type of their metaclass, which is type in this case. Since type does not define a __getitem__ method, you can't do list[...].

To do this correctly, you need to import typing.List and use that instead of the built-in list in your type hints:

from typing import List

...


def twoSum(self, nums: List[int], target: int) -> List[int]:

If you want to avoid the extra import, you can simplify the type hints to exclude generics:

def twoSum(self, nums: list, target: int) -> list:

Alternatively, you can get rid of type hinting completely:

def twoSum(self, nums, target):
Tuesday, August 17, 2021
 
test12345
answered 4 Months ago
44

Just remove handles keyword

Use it like that:

import matplotlib.pyplot as plt
line_up, = plt.plot([1,2,3], label='Line 2')
line_down, = plt.plot([3,2,1], label='Line 1')
plt.legend([line_up, line_down])
Tuesday, August 24, 2021
 
Naveed S
answered 4 Months ago
79

From the documentation of raw_input:

The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.

So what happens is that you try to multiply a string with a float, something like y="3" * x - 0.5 * "3" *x**2, which is not defined.

The easiest way to circumvent this is to cast the input string to float first.

x = np.linspace(0., 9., 10)
a = float(raw_input('Acceleration ='))
v = float(raw_input('Velocity = '))
y=v*x-0.5*a*x**2

Mind that if you're using python 3, you'd need to use inputinstead of raw_input,

a = float(input('Acceleration ='))
Thursday, October 28, 2021
 
Zizoo
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share