#!/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. POJ 1511 Invitation Cards(Dijkstra(优先队列)+SPFA(邻接表优化))

    题目链接:http://poj.org/problem?id=1511 题目大意:给你n个点,m条边(1<=n<=m<=1e6),每条边长度不超过1e9.问你从起点到各个点以及从各个 ...

  2. 关于Windows中的硬链接

    https://zhidao.baidu.com/question/748233720330351012.html linux中使用硬链接 ln a.txt b.txt 查看硬链接 ls -il 关于 ...

  3. (转载)Linux入门:操作目录和文件的命令

    PATH   每个用户的PATH都是不一样的: PATH中不包含“当前目录”: (1)echo $PATH:显示PATH环境变量: (2)PATH = "$PATH":/home/ ...

  4. NIO-3网络通信(非阻塞)

    import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import ja ...

  5. 4. CMake 系列 - 项目添加编译选项

    目录 1. 项目目录结构 2. 相关代码 2.1 add 模块 2.2 sub 模块 2.3 example 模块 2.4 顶层 CMakeLists.txt 3. 配置&编译 1. 项目目录 ...

  6. 添加到sudo组里

    $visudo     //切记,此处没有vi和sudo之间没有空格 1.移动光标,到最后一行(最好是找到root ALL=(ALL) ALL,在下面添加一行) 2.按a,进入append模式3.输入 ...

  7. 【C#】字段总结

    前沿: 字段(field)是一种数据成员,其中容纳了一个值类型的实例或者一个引用类型的引用. 正文: CLR支持类型(静态)字段和实例(非静态)字段.对于类型字段,用于容纳字段数据的动态内存是在类型对 ...

  8. [hdu4667]Building Fence 计算几何 瞎瘠薄搞

    大致题意: 给出n个圆和m个三角形,求最小的的,能将所有图形覆盖的图形的周长. 正解为求所有三角形顶点与圆的切点以及圆和圆的切点构造凸包,再求路径. 因为要求结果误差<=1e-3 所以 我们可以 ...

  9. 图解安装CLion编写程序并进行单元测试

    我对JetBrains公司的IDE情有独钟,于是这里使用的IDE是CLion,CLion是以IntelliJ为基础,专为开发C及C++所设计的跨平台IDE,可以在Windows.Linux及MacOS ...

  10. 基于libevent和unix domain socket的本地server

    https://www.pacificsimplicity.ca/blog/libevent-echo-server-tutorial 根据这一篇写一个最简单的demo.然后开始写client. cl ...