"""
=================
Custom projection
=================
Showcase Hammer projection by alleviating many features of Matplotlib.
"""
import numpy as np
import matplotlib
from matplotlib.axes import Axes
import matplotlib.axis as maxis
from matplotlib.patches import Circle
from matplotlib.path import Path
from matplotlib.projections import register_projection
import matplotlib.spines as mspines
from matplotlib.ticker import FixedLocator, Formatter, NullLocator
from matplotlib.transforms import Affine2D, BboxTransformTo, Transform
rcParams = matplotlib.rcParams
# This example projection class is rather long, but it is designed to
# illustrate many features, not all of which will be used every time.
# It is also common to factor out a lot of these methods into common
# code used by a number of projections with similar characteristics
# (see geo.py).
class GeoAxes(Axes):
"""
An abstract base class for geographic projections
"""
class ThetaFormatter(Formatter):
"""
Used to format the theta tick labels. Converts the native
unit of radians into degrees and adds a degree symbol.
"""
def __init__(self, round_to=1.0):
self._round_to = round_to
def __call__(self, x, pos=None):
degrees = round(np.rad2deg(x) / self._round_to) * self._round_to
return f"{degrees:0.0f}\N{DEGREE SIGN}"
RESOLUTION = 75
def _init_axis(self):
self.xaxis = maxis.XAxis(self)
self.yaxis = maxis.YAxis(self)
# Do not register xaxis or yaxis with spines -- as done in
# Axes._init_axis() -- until GeoAxes.xaxis.clear() works.
# self.spines['geo'].register_axis(self.yaxis)
def clear(self):
# docstring inherited
super().clear()
self.set_longitude_grid(30)
self.set_latitude_grid(15)
self.set_longitude_grid_ends(75)
self.xaxis.set_minor_locator(NullLocator())
self.yaxis.set_minor_locator(NullLocator())
self.xaxis.set_ticks_position('none')
self.yaxis.set_ticks_position('none')
self.yaxis.set_tick_params(label1On=True)
# Why do we need to turn on yaxis tick labels, but
# xaxis tick labels are already on?
self.grid(rcParams['axes.grid'])
Axes.set_xlim(self, -np.pi, np.pi)
Axes.set_ylim(self, -np.pi / 2.0, np.pi / 2.0)
def _set_lim_and_transforms(self):
# A (possibly non-linear) projection on the (already scaled) data
# There are three important coordinate spaces going on here:
#
# 1. Data space: The space of the data itself
#
# 2. Axes space: The unit rectangle (0, 0) to (1, 1)
# covering the entire plot area.
#
# 3. Display space: The coordinates of the resulting image,
# often in pixels or dpi/inch.
# This function makes heavy use of the Transform classes in
# ``lib/matplotlib/transforms.py.`` For more information, see
# the inline documentation there.
# The goal of the first two transformations is to get from the
# data space (in this case longitude and latitude) to axes
# space. It is separated into a non-affine and affine part so
# that the non-affine part does not have to be recomputed when
# a simple affine change to the figure has been made (such as
# resizing the window or changing the dpi).
# 1) The core transformation from data space into
# rectilinear space defined in the HammerTransform class.
self.transProjection = self._get_core_transform(self.RESOLUTION)
# 2) The above has an output range that is not in the unit
# rectangle, so scale and translate it so it fits correctly
# within the axes. The peculiar calculations of xscale and
# yscale are specific to an Aitoff-Hammer projection, so don't
# worry about them too much.
self.transAffine = self._get_affine_transform()
# 3) This is the transformation from axes space to display
# space.
self.transAxes = BboxTransformTo(self.bbox)
# Now put these 3 transforms together -- from data all the way
# to display coordinates. Using the '+' operator, these
# transforms will be applied "in order". The transforms are
# automatically simplified, if possible, by the underlying
# transformation framework.
self.transData = \
self.transProjection + \
self.transAffine + \
self.transAxes
# The main data transformation is set up. Now deal with
# gridlines and tick labels.
# Longitude gridlines and ticklabels. The input to these
# transforms are in display space in x and axes space in y.
# Therefore, the input values will be in range (-xmin, 0),
# (xmax, 1). The goal of these transforms is to go from that
# space to display space. The tick labels will be offset 4
# pixels from the equator.
self._xaxis_pretransform = \
Affine2D() \
.scale(1.0, self._longitude_cap * 2.0) \
.translate(0.0, -self._longitude_cap)
self._xaxis_transform = \
self._xaxis_pretransform + \
self.transData
self._xaxis_text1_transform = \
Affine2D().scale(1.0, 0.0) + \
self.transData + \
Affine2D().translate(0.0, 4.0)
self._xaxis_text2_transform = \
Affine2D().scale(1.0, 0.0) + \
self.transData + \
Affine2D().translate(0.0, -4.0)
# Now set up the transforms for the latitude ticks. The input to
# these transforms are in axes space in x and display space in
# y. Therefore, the input values will be in range (0, -ymin),
# (1, ymax). The goal of these transforms is to go from that
# space to display space. The tick labels will be offset 4
# pixels from the edge of the axes ellipse.
yaxis_stretch = Affine2D().scale(np.pi*2, 1).translate(-np.pi, 0)
yaxis_space = Affine2D().scale(1.0, 1.1)
self._yaxis_transform = \
yaxis_stretch + \
self.transData
yaxis_text_base = \
yaxis_stretch + \
self.transProjection + \
(yaxis_space +
self.transAffine +
self.transAxes)
self._yaxis_text1_transform = \
yaxis_text_base + \
Affine2D().translate(-8.0, 0.0)
self._yaxis_text2_transform = \
yaxis_text_base + \
Affine2D().translate(8.0, 0.0)
def _get_affine_transform(self):
transform = self._get_core_transform(1)
xscale, _ = transform.transform((np.pi, 0))
_, yscale = transform.transform((0, np.pi/2))
return Affine2D() \
.scale(0.5 / xscale, 0.5 / yscale) \
.translate(0.5, 0.5)
def get_xaxis_transform(self, which='grid'):
"""
Override this method to provide a transformation for the
x-axis tick labels.
Returns a tuple of the form (transform, valign, halign)
"""
if which not in ['tick1', 'tick2', 'grid']:
raise ValueError(
"'which' must be one of 'tick1', 'tick2', or 'grid'")
return self._xaxis_transform
def get_xaxis_text1_transform(self, pad):
return self._xaxis_text1_transform, 'bottom', 'center'
def get_xaxis_text2_transform(self, pad):
"""
Override this method to provide a transformation for the
secondary x-axis tick labels.
Returns a tuple of the form (transform, valign, halign)
"""
return self._xaxis_text2_transform, 'top', 'center'
def get_yaxis_transform(self, which='grid'):
"""
Override this method to provide a transformation for the
y-axis grid and ticks.
"""
i
没有合适的资源?快使用搜索试试~ 我知道了~
gallery_python.zip
共511个文件
py:511个
需积分: 0 1 下载量 151 浏览量
2023-09-26
11:06:46
上传
评论
收藏 1.18MB ZIP 举报
温馨提示
gallery_python.zip
资源推荐
资源详情
资源评论
收起资源包目录
gallery_python.zip (511个子文件)
custom_projection.py 16KB
annotation_demo.py 15KB
annotated_cursor.py 13KB
angle_annotation.py 13KB
basic_units.py 11KB
image_annotated_heatmap.py 11KB
pong_sgskip.py 10KB
skewt.py 10KB
embedding_webagg_sgskip.py 9KB
mplcvd.py 9KB
demo_agg_filter.py 9KB
radar_chart.py 8KB
fourier_demo_wx_sgskip.py 8KB
cursor_demo.py 8KB
boxplot_demo.py 8KB
custom_cmap.py 8KB
marker_reference.py 8KB
filled_step.py 7KB
ishikawa_diagram.py 7KB
confidence_ellipse.py 6KB
poly_editor.py 6KB
date_concise_formatter.py 6KB
pick_event_demo.py 6KB
custom_scale.py 6KB
packed_bubbles.py 6KB
legend_demo.py 6KB
subplots_demo.py 6KB
tricontour_demo.py 6KB
style_sheets_reference.py 6KB
tricontour_smooth_delaunay.py 6KB
arrow_demo.py 5KB
inset_locator_demo.py 5KB
tripcolor_demo.py 5KB
leftventricle_bullseye.py 5KB
image_demo.py 5KB
psd_demo.py 5KB
date_precision_and_epochs.py 5KB
secondary_axis.py 5KB
demo_floating_axes.py 5KB
logos2.py 5KB
fill_between_alpha.py 5KB
pie_and_donut_labels.py 5KB
colormap_normalizations.py 5KB
image_antialiasing.py 5KB
triplot_demo.py 5KB
collections.py 5KB
svg_histogram_sgskip.py 4KB
path_editor.py 4KB
embedding_in_wx3_sgskip.py 4KB
subfigures.py 4KB
fill_between_demo.py 4KB
arrow_guide.py 4KB
fancybox_demo.py 4KB
contourf_demo.py 4KB
stock_prices.py 4KB
dolphin.py 4KB
pcolormesh_grids.py 4KB
time_series_histogram.py 4KB
sankey_rankine.py 4KB
barchart_demo.py 4KB
mathtext_wx_sgskip.py 4KB
scatter_hist.py 4KB
image_transparency_blend.py 4KB
timeline.py 4KB
anatomy.py 4KB
menu.py 4KB
axes_box_aspect.py 4KB
mathtext_examples.py 4KB
demo_text_path.py 4KB
firefox.py 4KB
pcolormesh_levels.py 4KB
font_table.py 4KB
demo_curvelinear_grid.py 4KB
violinplot.py 4KB
contour_demo.py 4KB
custom_boxstyle01.py 4KB
pie_features.py 4KB
trigradient_demo.py 4KB
contour_image.py 4KB
date_formatters_locators.py 4KB
scatter_with_legend.py 4KB
demo_annotation_box.py 4KB
text_alignment.py 3KB
sankey_basics.py 3KB
tick-formatters.py 3KB
boxplot.py 3KB
bxp.py 3KB
rasterization_demo.py 3KB
svg_tooltip_sgskip.py 3KB
lasso_selector_demo_sgskip.py 3KB
pcolor_demo.py 3KB
axes_zoom_effect.py 3KB
tickedstroke_demo.py 3KB
polygon_selector_demo.py 3KB
colormap_reference.py 3KB
topographic_hillshading.py 3KB
tex_demo.py 3KB
hist.py 3KB
auto_subplots_adjust.py 3KB
major_minor_demo.py 3KB
共 511 条
- 1
- 2
- 3
- 4
- 5
- 6
资源评论
m0_73051650
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Music-Player +PlayerActivity+ rockplayer+ SeeJoPlayer 播放器JAVA源码
- vscode-1.46.0.tar源码文件
- 最近很火植物大战僵尸杂交版2.08苹果+安卓+PC+防闪退工具V2+修改工具+高清工具+通关存档整合包更新
- 超级好用的截图工具PixPin,可录制Gif图
- Screenshot_2024-05-21-17-06-42-64_2332cb9b27b851b548ba47a91682926c.jpg
- 毕业设计参考 - 基于树莓派、OpenCV及Python的人脸识别
- node-v18.20.2-linux-arm64
- 222222222222
- 16张相机标定图片,可复现本文畸变矫正
- dbeaver-ce-23.3.1-x86_64-setup.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功