#!/usr/bin/env python3

 ## 以下是一个带误差条的条形图的例子,演示了误差条形图的绘制及中英文字体设置
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties as FP # %matplotlib inline
# %config InlineBackend.figure_format = 'svg' mpl.rcParams['text.usetex'] = False
mpl.rcParams['figure.figsize'] = (7.40, 5.55) # unit: inch
mpl.rcParams['figure.frameon'] = False ## 中文设置
# matplotlib默认不支持ttc,所以可以将ttc转换ttf先。
# 将Windows字体 simsun.ttc上传到 https://transfonter.org/ttc-unpack 在线转换成TTF,
# 得到simsun.ttf和nsimsun.ttf,将两个ttf文件放到PYTHON安装目录的
# Lib\site-packages\matplotlib\mpl-data\fonts\ttf 子目录下。
# 删除字体缓存以便重新生成字体缓存:$HOME/.matplotlib/fontList.py3k.cache # 全局中文设置
mpl.rcParams['font.family'] = 'sans-serif'
mpl.rcParams['font.sans-serif'] = 'NSimSun,Times New Roman' # 局部中文:设置分别为中文和英文设置两个FontProperties,以便局部切换中英文字体
cfp = FP('NSimSun', size=12)
efp = FP('Times New Roman', size=12) fig,ax = plt.subplots() xticklabels = ('G1', 'G2', 'G3')
ylabel = 'd' male_means = (9, 10, 9)
male_std = (4.66, 3.52, 5.32)
female_means = (12, 14, 12)
female_std = (6.96, 5.46, 3.61)
title = 'XX实验结果' N=3
ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars
with plt.style.context(('ggplot')):
rects1 = ax.bar(
ind - 0.02, female_means, width, color='darkgrey', yerr=female_std)
rects2 = ax.bar(
ind + 0.02 + width,
male_means,
width,
color='lightgrey',
yerr=male_std) ax.set_ylabel('d', fontproperties=efp, rotation=0)
# 在label、title中可用参数'fontproperties' 指定字体 ax.yaxis.set_label_coords(-0.05, 0.95)
ax.set_title(title)
ax.set_xticks(ind + width / 2) ax.set_xticklabels(xticklabels, fontproperties=efp) ax.legend((rects1[0], rects2[0]), ('处理A', '处理B'), prop=cfp, framealpha=0)
# 在legend中可用参数'prop'指定字体,注意不是'fontproperties' def autolabel(rects, yerr):
"""
Attach a text label above each bar displaying its height
"""
for i in range(0, N):
rect = rects[i]
height = rect.get_height()
ax.text(
rect.get_x() + rect.get_width() / 2.,
1.05 * height,
'%0.2f' % yerr[i],
ha='left',
va='bottom',
family='Georgia',
fontsize=9)
#在text函数中可用family和fontsize指定字体 autolabel(rects1, female_means)
autolabel(rects2, male_means) ax.text(
1,
20,
'Hello World',
color = 'b',
ha='left',
va='bottom',
fontproperties=efp)
# 在text函数中也可用fontproperties指定字体 fig.tight_layout()
fig.savefig('filename.svg', format='svg')
# 保存为矢量图svg格式,如需插入word,可以用inkscape软件将其转换成emf格式

用matplotlib绘制带误差的条形图及中英文字体设置的更多相关文章

  1. 使用matplotlib绘制带图例的图表

    #coding=utf8 from pylab import * plt.figure(figsize=(8,10), dpi=50) plt.plot(do_tow2[28:508],do_prn2 ...

  2. 3.matplotlib绘制条形图

    plt.bar() # coding=utf-8 from matplotlib import pyplot as plt from matplotlib import font_manager my ...

  3. 用Python的Pandas和Matplotlib绘制股票唐奇安通道,布林带通道和鳄鱼组线

    我最近出了一本书,<基于股票大数据分析的Python入门实战 视频教学版>,京东链接:https://item.jd.com/69241653952.html,在其中给出了MACD,KDJ ...

  4. matplotlib绘制柱状图

    参考自Matplotlib Python 画图教程 (莫烦Python)(11)_演讲•公开课_科技_bilibili_哔哩哔哩 https://www.bilibili.com/video/av16 ...

  5. 用Python的Pandas和Matplotlib绘制股票KDJ指标线

    我最近出了一本书,<基于股票大数据分析的Python入门实战 视频教学版>,京东链接:https://item.jd.com/69241653952.html,在其中给出了MACD,KDJ ...

  6. matplotlib绘制动画

    matplotlib从1.1.0版本以后就开始支持绘制动画,具体使用可以参考官方帮助文档.下面是一个很基本的例子: """ A simple example of an ...

  7. 用Matplotlib绘制二维图像

    唠叨几句: 近期在做数据分析,需要对数据做可视化处理,也就是画图,一般是用Matlib来做,但Matlib安装文件太大,不太想直接用它,据说其代码运行效率也很低,在网上看到可以先用Java做数据处理, ...

  8. Python学习(一) —— matplotlib绘制三维轨迹图

    在研究SLAM时常常需要对其输出的位姿进行复现以检测算法效果,在ubuntu系统中使用Python可以很好的完成相关的工作. 一. Ubuntu下Python的使用 在Ubuntu下使用Python有 ...

  9. Turtle绘制带颜色和字体的图形(Python3)

    转载自https://blog.csdn.net/wumenglu1018/article/details/78184930 在Python中有很多编写图形程序的方法,一个简单的启动图形化程序设计的方 ...

随机推荐

  1. insta php-fpm 的配置

    [global]pid = /www/wdlinux/phps/70/var/run/php-fpm.piderror_log = /www/wdlinux/phps/70/var/log/php-f ...

  2. Oracle数据库,基础知识

    1.Oracle的五大约束条件: 1 主键  primary key2 外键  foreign key,3 唯一  unique,4 检测  check5 非空  not null 实例运用: -- ...

  3. (三)Spring 之AOP 详解

    第一节:AOP 简介 AOP 简介:百度百科: 面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),是软件开发中的一个热点,也是Spring框架中的一个 ...

  4. hdu 5875(单调栈)

    Function Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  5. 洛谷 P2043质因子分解 题解

    题目传送门 N的范围很小,所以使用O(n2)的算法就能过啦! #include<bits/stdc++.h> using namespace std; ],n; int main(){ c ...

  6. Wannafly挑战赛18 C - 异或和

    思路:我刚开始是想旋转四次坐标,每次用bit计算每个点左上角的点到这个点的距离,TLE了.... 这种算曼哈顿距离的可以将x 轴和 y 轴独立开来,分别计算. #include<bits/std ...

  7. oracle创建简单的包

    --规范 create or replace package test_pkg is --test_pkg为包名 procedure showMessage; --声明一个过程 function my ...

  8. Window 下一台机器配置三个Tomcat实例

    下面我们把配置的详细过程写在下面,以供参考:(此例以配置三个Tomcat为例) 1. 下载apache-tomcat-8.0.63,下载下来的文件为apache-tomcat-8.0.63.zip. ...

  9. TCP(传输控制协议)

    三次握手tcp是一种面向链接的.可靠的.基于字节流的传输层通信协议,提供可靠的连接服务,采用三次握手确认建立一个连接.位码即tcp标志位,有6种标示:1.SYN(synchronous建立连接)2.A ...

  10. 牛客网 牛客练习赛43 B.Tachibana Kanade Loves Probability-快速幂加速

    链接:https://ac.nowcoder.com/acm/contest/548/B来源:牛客网 Tachibana Kanade Loves Probability 时间限制:C/C++ 1秒, ...