matplotlib图例与配色方案

配置图例

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
plt.style.use('classic')
x=np.linspace(0,10,100)
fig,ax=plt.subplots()
ax.plot(x,np.sin(x),'-b',label='Sine')
ax.plot(x,np.cos(x),'--g',label='Cosine')
ax.axis=('equal')
leg=ax.legend()

在这里插入图片描述

ax.legend(loc='lower center',frameon=True,ncol=2)
fig

在这里插入图片描述

ax.legend(fancybox=True,framealpha=1,shadow=True,borderpad=1)
fig

在这里插入图片描述

y=np.sin(x[:,np.newaxis]+np.pi*np.arange(0,2,0.5))
lines=ax.plot(x,y)#不能只写y,因为y只会以range(len(y))为x坐标。
ax.legend(lines[:2],['first','second'])
fig

在这里插入图片描述

import pandas as pd
cities=pd.read_csv('H:\data\california_cities.csv')
lat,lon=cities['latd'],cities['longd']#经纬度。
population, area = cities['population_total'], cities['area_total_km2']
plt.scatter(lon, lat, label=None, 
            c=np.log10(population), cmap='viridis', 
            s=area, linewidth=0, alpha=0.5) 
plt.axis(aspect='equal') 
plt.xlabel('longitude') 
plt.ylabel('latitude') 
plt.colorbar(label='log$_{10}$(population)') 
plt.clim(3, 7)#对colorbar 范围进行了控制。

#下面画图例:
for area in [100,300,500]:
    plt.scatter([],[],c='k',s=area,
                alpha=0.3,label=str(area)+'km$^2$')#label参数:matplotlib.collection.Collections.
#这里scatter创建了图形但是没有描绘,赋予标签后,可以不在legend中赋予。
plt.legend(
           scatterpoints=1,#marker的数量。与其对应的line2D,legend entry 是numpoints。
           frameon=True,#边框。
           fancybox=True,#圆角,
           labelspacing=1,#每行的距离
           columnspacing=3,#每列的距离,注意一个图形和一个对应的text,为一列而不是两列
           ncol=1,#列数
           handlelength=1,#handle(即图形),到右边界距离
           handletextpad=1,#handle和文字之间的距离
           borderpad=1,#边界与内容的距离
           borderaxespad=1,#和边界的距离,右上。
           markerfirst=False,#marker在text前面。
           markerscale=1,#marker大小
           title='City Area',#legend的标题
           title_fontsize=15,#标题字体大小。
           facecolor='b',#legend颜色
           edgecolor='r',#legend边界颜色
           shadow=True,#边框阴影
           scatteryoffsets=[0.5,0.5,0.5],#垂直偏移,我还不知道有啥用。
          )
#另有参数,handles,labels指定标签。
plt.title('California Cities:Area and Population')
    
Text(0.5, 1.0, 'California Cities:Area and Population')

在这里插入图片描述

fig,ax=plt.subplots()
lines=[]
style=['-','--','-.',':']
x=np.linspace(0,10,1000)
for i in range(4):
    lines+=ax.plot(x,np.sin(x-i*np.pi/2),style[i],color='k')
ax.axis('equal')
ax.legend(lines[:2],['line1','line2'],loc='upper right',frameon=True)
from matplotlib.legend import Legend
leg=Legend(ax,lines[2:],['line3','line4'],loc='lower right',frameon=True)
# 创建legend的底层艺术家图例对象。 
#基本只有第一个参数(parent,需要一个artist对象,如这里的ax)和ax.legend()不一样。
ax.add_artist(leg)
#将这个对象加入到ax(艺术家对象)
#这里其实采用了面向对象的接口来创建两个图例,因为标准的legend接口只能创建一个图例。
<matplotlib.legend.Legend at 0x1c039dd7520>

在这里插入图片描述

配色方案

x=np.linspace(0,10,1000)
img=np.sin(x)*np.cos(x[:,np.newaxis])
plt.imshow(img,cmap=plt.cm.get_cmap('Blues',6))
#plt.cm.get_cmap可做离散型颜色。
plt.colorbar()
<matplotlib.colorbar.Colorbar at 0x1c039e2e2b0>

在这里插入图片描述

  • 顺序配色方案
    由一组连续的颜色构成的配色方案(例如 binary 或 viridis)。
  • 互逆配色方案
    通常由两种互补的颜色构成,表示正反两种含义(例如 RdBu 或 PuOr)。
  • 定性配色方案
    随机顺序的一组颜色(例如 rainbow 或 jet)。
    定性配色方案在亮度较高时,颜色难以分辨,同时,重点容易被灰度较亮的那部分影响,视觉效果不佳。
#设置噪点。
speckles = np.random.random(img.shape) < 0.01 
img[speckles] = np.random.normal(0, 3, np.count_nonzero(speckles)) 
plt.figure(figsize=(10, 3.5)) 
plt.subplot(1, 2, 1) 
plt.imshow(img, cmap='RdBu') 
plt.colorbar() 
plt.subplot(1, 2, 2)
plt.imshow(img, cmap='RdBu') 
plt.colorbar(extend='both') #把超出范围的设为三角形。'min','max'可选。
plt.clim(-1, 1)#bar的范围。更确切的说是图形颜色的范围,colorbar只是根据图形颜色情况生成的。
#控制范围可以减轻噪点的影响

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值