- 积分
- 3632
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2014-10-21
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
1、需求缘起
平时画散点图,如果直接压着散点的最左、最右、最高、最低的点来设置宽高范围,显得挤,
边距留大了,又显得空。
于是我想,如何设置xlim和ylim才能使散点图松紧适宜,显得美观得体呢?
我的思路是:按散点的【平均横向间隔】和【平均纵向间隔】留出边距。
2、源代码
import numpy as np
def appropriate_xylim(lng,lat):
n=len(lng)
gapx=(np.max(lng)-np.min(lng))/(n-1) # 平均横向间隔
gapy=(np.max(lat)-np.min(lat))/(n-1) # 平均纵向间隔
# 返回值是一个元组,4个元素分别是最小经度,最大经度,最小纬度,最大纬度
return (np.min(lng)-gapx,
np.max(lng)+gapx,
np.min(lat)-gapy,
np.max(lat)+gapy)
if __name__=="__main__":
import matplotlib.pyplot as plt
lng=np.random.randn(10)+120
lat=np.random.randn(10)+30
result=appropriate_xylim(lng,lat)
plt.scatter(lng,lat)
plt.xlim(result[0],result[1])
plt.ylim(result[2],result[3])
plt.show()
效果图:
|
|