Skip to content

Tag: python

Make matplotlib autoscaling ignore some of the plots

I use matplotib’s Axes API to plot some figures. One of the lines I plot represents the theoretical expected line. It has no meaning outside of the original y and x limits. What I want, is for matlplotlib to ignore it when autoscaling the limits. What I used to do, is to check what are the current limit…

Add single element to array in numpy

I have a numpy array containing: I want to create an array containing: That is, I want to add the first element on to the end of the array. I have tried the obvious: But I get an error saying ValueError: arrays must have same number of dimensions I don’t understand this – the arrays are both just …

Print a list in reverse order with range()?

How can you produce the following list with range() in Python? Answer use reversed() function: It’s much more meaningful. Update: If you want it to be a list (as btk pointed out): Update: If you want to use only range to achieve the same result, you can use all its parameters. range(start, stop, step) F…

List from string of digits

I’m getting a string from input() which consists of digits separated by spaces (1 5 6 3). First I’m trying to check that the input only consists of digits with the isdigit() function but because of the spaces I can’t get it to work. Then I convert the string to a list using the split() funct…

What does a . in an import statement in Python mean?

I’m looking over the code for Python’s multiprocessing module, and it contains this line: instead of the subtle difference being the period before _multiprocessing. What does that mean? Why the period? Answer That’s the syntax for explicit relative imports. It means import from the current p…

How to replace values at specific indexes of a python list?

If I have a list: And then declare two other lists: How can I take to_modify’s elements as index to indexes, then set corresponding elements in to_modify to replacements, i.e. after running, indexes should be [0,0,3,0,1,0]. Apparently, I can do this through a for loop: But is there other way to do this?…

background function in Python

I’ve got a Python script that sometimes displays images to the user. The images can, at times, be quite large, and they are reused often. Displaying them is not critical, but displaying the message associated with them is. I’ve got a function that downloads the image needed and saves it locally. R…