Set points outside plot to upper limit

橙三吉。 提交于 2019-12-12 11:09:50

问题


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

I am making a scatter plot in Python. For illustrative purposes, I don't want to set my axes range such that all points are included - there may be some really high or really low values, and all I care about in those points is that they exist - that is, they need to be in the plot, but not on their actual value - rather, somewhere on the top of the canvas.

I know that in IDL there is a nice short syntax for this: in plot(x,y<value) any value in y greater than value will simply be put at y=value.

I am looking for something similar in Python. Can somebody help me out?


回答1:


you can just use np.minimum on the y data to set anything above your upper limit to that limit. np.minimum calculates the minima element-wise, so only those values greater than ymax will be set to ymax.

For example:

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()




回答2:


There is no equivalent syntactic sugar in matplotlib. You will have to preprocess your data, e.g.:

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()


来源:https://stackoverflow.com/questions/39894896/set-points-outside-plot-to-upper-limit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!