python matplotlib 画黑白的柱状图(论文专用)


# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from pylab import *
mpl.rcParams['font.sans-serif']=['SimHei']
def read():
	name_list = ['计算机'.decode("utf-8"),'法律'.decode("utf-8"),'文学'.decode("utf-8"),'医学'.decode("utf-8"),'体育'.decode("utf-8")]
	# name_list = ['引言','相关工作','数据','方法','实验','结果','讨论']
	time1=[0.913,0.465,0.512,0.553,0.779]
	time2=[0.072,0.465,0.268,0.415,0.162]
	time3=[0.014,0.070,0.146,0.011,0.044]
	time4=[0.000,0.000,0.073,0.021,0.015]

	location=np.arange(len(name_list))
	width=0.2

	plt.figure(figsize=(12,4))
	plt.bar(location, time1,tick_label = name_list,width = width,label="1",alpha=0.8,color="w",edgecolor="k")
	for a,b in zip(location, time1):
		plt.text(a, b+0.05, '%.3f' % b, ha='center', va= 'bottom',fontsize=7)
	plt.bar(location+width, time2,tick_label = name_list,width = width,label="1-2",alpha=0.8,color="w",edgecolor="k",hatch=".....")
	for a,b in zip(location+width, time2):
		plt.text(a, b+0.05, '%.3f' % b, ha='center', va= 'bottom',fontsize=7)
	plt.bar(location+width*2, time3,tick_label = name_list,width = width,label="2-3",alpha=0.8,color="w",edgecolor="k",hatch="/")
	for a,b in zip(location+width*2, time3):
		plt.text(a, b+0.05, '%.3f' % b, ha='center', va= 'bottom',fontsize=7)
	plt.bar(location+width*3, time4,tick_label = name_list,width = width,label=">3",alpha=0.8,color="w",edgecolor="k",hatch="\\\\\\\\\\")
	for a,b in zip(location+width*3, time4):
		plt.text(a, b+0.05, '%.3f' % b, ha='center', va= 'bottom',fontsize=7)

	plt.ylim(0,1)
	plt.legend(loc=1)
	plt.show()  

if __name__ == '__main__':
	read()


先看一下效果图
在国内的中文中经常需要黑白的图,在网上中了一大堆,没有发现合适的

于是自己不断的尝试然后发现了其中的奥秘
首先设置参数让填充颜色为白色,边框颜色为黑色,即下图
color="w",edgecolor="k"

然后就是填充,填充的参数为hatch
具体的团有以下几种
但是需要注意的是,如果需要“\”

则需要进行转义,也就是"\\"
如果你想要让图案变得密集那就"......"多打一些图案"/"具体的密集程度根据你的数量控制

 




  • 17
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值