python怎么用matplotlib生成图表_如何使用Matplotlib和python创建表格图例

I am plotting a choropleth map in python from a shapefile and i want to customize the legend of the plot, i am using the code bellow:

import pandas as pd

import pysal as ps

import geopandas as gp

import numpy as np

import matplotlib.pyplot as plt

pth = 'outcom.shp'

tracts = gp.GeoDataFrame.from_file(pth)

ax = plot_dataframe(tracts, column='Density', scheme='QUANTILES', k=4, colormap=plt.cm.Blues, legend=True)

plt.show()

Besides, i am using a small patch that i found here http://nbviewer.ipython.org/gist/jorisvandenbossche/d4e6efedfa1e4e91ab65 in order to visualize the legend.

here's my result :

But, i need something similar to this :

so my question now, is how can i have a customized legend

解决方案

You may use a plt.table as a legend.

import matplotlib.pyplot as plt

import numpy as np

valeur = np.array([.1,.45,.7])

text=[["Faible","Ng<1,5" ],["Moyenne","1,52,5"]]

colLabels = ["Exposition", u"Densité"]

tab=plt.table(cellText=text, colLabels=colLabels,

colWidths = [0.2,0.2], loc='lower right',

cellColours=plt.cm.hot_r(np.c_[valeur,valeur]))

plt.show()

In order to link this table to a contourf plot, you may do as follows:

from matplotlib import pyplot as plt

import numpy as np

a = np.sort(np.random.rand(100)).reshape(10,10)*4

levels = np.array([0,1.5,2.5,4])

sm = plt.contourf(a, levels = levels, cmap=plt.cm.hot_r )

text=[["Faible","Ng<1,5" ],["Moyenne","1,52,5"]]

colLabels = ["Exposition", u"Densité"]

col = levels[:-1] + np.diff(levels)/2.

cellcol = sm.cmap(sm.norm(np.c_[col,col]))

tax = plt.gcf().add_axes([0,0,1,1])

tab=tax.table(cellText=text, colLabels=colLabels,

colWidths = [0.2,0.2], loc='lower left',

cellColours=cellcol )

tax.axis("off")

plt.show()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值