#!/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. python脚本-实现自动按规则创建指定大小和指定个数的文件案例

    # -*- coding: cp936 -*-#---------------------------------------------------------------------------- ...

  2. Java杂知识汇总(自己积累的)

    [前提] 接下来,可能需要开始学习Java了,所以现在将前辈们传递给我的经验都记录下来,免得再次问他们. [积累] 1. 关于重启Tomcat服务器: 当在开发过程中, | 修改Java代码 -- 需 ...

  3. udev和rules使用规则

    本文以通俗的方法阐述 udev 及相关术语的概念.udev 的配置文件和规则文件,然后以 Red Hat Enterprise Server 为平台演示一些管理设备文件和查询设备信息的实例.本文会使那 ...

  4. HDU 1079 Calendar Game(博弈找规律)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1079 题目大意:给你一个日期(包含年月日),这里我表示成year,month,day,两人轮流操作,每 ...

  5. C语言俄罗斯方块

    #include <windows.h> #include <stdio.h> #include <time.h> #include <conio.h> ...

  6. 【PAT】1008. 数组元素循环右移问题 (20)

    1008. 数组元素循环右移问题 (20) 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0A1……AN- ...

  7. python collection系列

    collection系列 不常用功能,需要进行模块功能导入: import collection Counter 常用方法测试: #!/usr/local/env python3 ''' Author ...

  8. Java 中可变参数

    可变参数 Java 中可变参数 现在需要编写一个求和的功能,但是不知道有几个参数,在调用的时候才知道有几个参数,请问这如何实现呢? Java 给我们提供了一个 JDK 1.5 的新特性---可变参数 ...

  9. fastadmin iframe 表单提交之后跳转

    controller 对应的那个js文件中添加: define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function($, und ...

  10. 微信小程序setData()对数组的操作

    对于setData普通数据类型而言,没什么讲究 但是对于数组而言,再直接修改一个完整的数组显得有些多余,首先写着不简易,其次效率很是滴. 比如 你都能觉得复杂,官方肯定是有对应的优化的. 官方demo ...