matplotlib中的多图层叠加的问题
matplotlib中的多图层叠加的问题
问题的引出
⽼板提出希望在之前绘制的降⽔分布图基础上,叠加上⼀下线路、变电站的分布。简单⼀想这个任务很简单,只需要知道线路,变电站经纬度,再叠加到图层即可。但是在实际操作却遇到⼀个⼩问题,即如何保证叠加的顺序,通过查阅matplotlib⼿册了解到⼀个set_zorder这个属性,可以完美解决图层叠加问题,因此在这⾥记录以下。本博客⾸发在[个⼈⽹站www.sunsoda.fun])
(www.sunsoda.fun)
我们先从下⾯这个例⼦讲起
import numpy as np
import pandas as pd
import matplotlib.lines as mlines
import matplotlib.patches as mpatches
import matplotlib as mpl
>>>>>>>>>>
#  解决中⽂字体显⽰问题
font = {
'family' : 'SimHei'
};
<('font', **font);
>>>>>>>>>>
# 随机设置坐标值
%matplotlib inline
N = 3
x = np.random.rand(N)
y = np.random.rand(N)
>>>>>>>>>>
fig = plt.figure(figsize=[8,4])
ax = fig.add_subplot(121)
# 绘制circle
for xi,yi in zip(x,y):
circle = mpatches.Circle((xi,yi), 0.05, ec="blue",fc='blue')
ax.add_patch(circle)
# 绘制Line
line = mlines.Line2D(x,y,lw=3.,ls='-',alpha=1,color='red')
ax.add_line(line)
ax.set_title('先圆后线')
>>>>>>>>####
ax = fig.add_subplot(122)
# 绘制Line
line = mlines.Line2D(x,y,lw=3.,ls='-',alpha=1,color='red')
ax.add_line(line)
# 绘制circle
for xi,yi in zip(x,y):
circle = mpatches.Circle((xi,yi), 0.05, ec="blue",fc='blue')
ax.add_patch(circle)
ax.set_title('先线后圆')
Text(0.5,1,'先线后圆')
这个例⼦,正是我在作图时遇到的问题,⽆论我是先画圆,还是先画线条,圆圈总是在线条之下,如何才能把圆圈调换到线条上来呢?这⾥我就联想到PS处理图⽚时最终的概念图层,调换不同的图层的叠加顺序,即会呈现出不⼀样的图⽚,那么在matplotlib中,是否也存在这样的接⼝,可以让我来设置图层的叠加顺序呢?通过查阅终于到这个⼤杀器set_zorder
set_zorder
set_zorder顾名思义,set设置,z对象,order,顺序,set_zorder即设置对象的顺序。matplotlib为多个对象都提供了这个函数。在官⽹搜索set_zorder即可看到
matplotlib.artist.Artist.set_zorder (Python method, in matplotlib.artist.Artist.set_zorder)
matplotlib.axes.Axes.set_zorder (Python method, in matplotlib.axes.Axes.set_zorder)
matplotlib.axis.Axis.set_zorder (Python method, in matplotlib.axis.Axis.set_zorder)
matplotlib中subplotmatplotlib.axis.Tick.set_zorder (Python method, in matplotlib.axis.Tick.set_zorder)
matplotlib.axis.XAxis.set_zorder (Python method, in matplotlib.axis.XAxis.set_zorder)
matplotlib.axis.XTick.set_zorder (Python method, in matplotlib.axis.XTick.set_zorder)
matplotlib.axis.YAxis.set_zorder (Python method, in matplotlib.axis.YAxis.set_zorder)-
matplotlib.axis.YTick.set_zorder (Python method, in matplotlib.axis.YTick.set_zorder)
可以看出,针对matplotlib中不同对象,都提供了set_zorder这个method,其规则如下:
Set the zorder for the artist. Artists with lower zorder values are drawn first. order值越⼩,越先画
因此,在我们上⾯那个case,就可以完美破解,希望圆圈画在线上,我们只需要把圆圈设个较⼤的zorder值,线条设个较⼩的zorder值即可
主要添加了circle.set_zorder(1)和line.set_zorder(0)这两⾏代码
for xi,yi in zip(x,y):
circle = mpatches.Circle((xi,yi), 0.05, ec="blue",fc='blue')
circle.set_zorder(0)
ax.add_patch(circle)
# 绘制Line
line = mlines.Line2D(x,y,lw=3.,ls='-',alpha=1,color='red')
line.set_zorder(1)
ax.add_line(line)
ax.set_title('先圆后线')
ax = fig.add_subplot(122)
# 绘制circle
for xi,yi in zip(x,y):
circle = mpatches.Circle((xi,yi), 0.05, ec="blue",fc='blue')
circle.set_zorder(1)
ax.add_patch(circle)
# 绘制Line
line = mlines.Line2D(x,y,lw=3.,ls='-',alpha=1,color='red')
line.set_zorder(0)
ax.add_line(line)
ax.set_title('先线后圆')
_xaxis()
try
最后现学现卖,送朵花花给⼤家,虽然这个代码不⽤zorder似乎也可以~~哈哈哈
x=np.array([0.4,0.5,0.6,0.55,0.45])*2
y=np.array([0.4,0.5,0.4,0.3,0.3])*2
colors=['red','orange','yellow','green','blue']
for i,xi,yi in zip(np.arange(len(x)),x,y):
circle = mpatches.Circle((xi,yi), 0.2, ec='pink',fc='pink')
circle.set_zorder(i)
ax.add_patch(circle)
circle = mpatches.Circle((1,0.8), 0.1, ec="white",fc="white") ax.add_patch(circle)
circle.set_zorder(10)
x=np.array([0.4,0.5,0.6,0.55,0.45])*2+1
y=np.array([0.4,0.5,0.4,0.3,0.3])*2
xl=[1,1]
yl=[0,0.55]
line = mlines.Line2D(xl,yl,lw=3.,ls='-',alpha=1,color='green') line.set_zorder(0)
ax.add_line(line)
colors=['red','orange','yellow','green','blue']
for i,xi,yi in zip(np.arange(len(x)),x,y):
circle = mpatches.Circle((xi,yi), 0.2, ec=colors[i],fc=colors[i])    circle.set_zorder(i)
ax.add_patch(circle)
circle = mpatches.Circle((2,0.8), 0.1, ec="white",fc="white") ax.add_patch(circle)
circle.set_zorder(10)
xl=[2,2]
yl=[0,0.55]
line = mlines.Line2D(xl,yl,lw=3.,ls='-',alpha=1,c='green') line.set_zorder(0)
ax.add_line(line)
ax.set_xlim([0,2.5])
ax.set_ylim([0,2.5])

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。

发表评论