Set points out of the graph to the upper limit

Perhaps this question already exists, but I could not find it.

I am doing a scatter in Python. For illustrative purposes, I don’t want to set the axis range so that all points are included - there may be some really high or very low values, and all I care about at these points is that they exist, that is, they you need to be in the plot, but not on their actual meaning - rather, somewhere on top of the canvas.

I know there is a nice short syntax for this in the IDL: plot(x,y<value)any value in y greater than valuewill just be placed in y=value.

I am looking for something like this in Python. Can someone help me?

+4
source share
2 answers

you can simply use np.minimumin the data yto set anything above your upper limit to that limit. np.minimumcalculates the minimum values ​​for the elements, so only those values ​​that are greater than ymaxwill be set to ymax.

For instance:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0., np.pi*2, 30)
y = 10. * np.sin(x)

ymax = 5

fig, ax = plt.subplots(1)
ax.scatter(x, np.minimum(y, ymax))

plt.show()

enter image description here

+1
source

There is no equivalent syntactic sugar in matplotlib. You need to pre-process your data, for example:

import numpy as np
import matplotlib.pyplot as plt

ymin, ymax = 0, 0.9
x, y = np.random.rand(2,1000)
y[y>ymax] = ymax
fig, ax = plt.subplots(1,1)
ax.plot(x, y, 'o', ms=10)
ax.set_ylim(ymin, ymax)
plt.show()
+2
source

Source: https://habr.com/ru/post/1656924/


All Articles