最近在用python中的matplotlib画折线图,遇到了坐标轴 “数字+刻度” 混合显示、标题中文显示、批量处理等诸多问题。通过学习解决了,来记录下。如有错误或不足之处,望请指正。

一、最简单的基本框架如下:已知x,y,画出折线图并保存。此时x和y均为数字。

 # -*- coding: utf-8 -*-

 import matplotlib.pyplot as plt #引入matplotlib的pyplot子库,用于画简单的2D图
import random x= range(0,20)
y= [random.randint(0,20) for _ in range(20)] #建立对象
fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot() #画图
plt.plot(x,y,'o-',label=u"线条") #画图
plt.show()
plt.savefig("temp.png")

二、坐标轴增加字母元素:

用到了如下语句和函数【参考:http://matplotlib.org/examples/ticks_and_spines/tick_labels_from_values.html】:

from matplotlib.ticker import FuncFormatter, MaxNLocator

labels = list('abcdefghijklmnopqrstuvwxyz')

def format_fn(tick_val, tick_pos):
    if int(tick_val) in xs:
        return labels[int(tick_val)]
    else:
        return ''

ax.xaxis.set_major_formatter(FuncFormatter(format_fn))

ax.xaxis.set_major_locator(MaxNLocator(integer=True))

稍微改动,用到了之前的程序里:

 # -*- coding: utf-8 -*-

 import matplotlib.pyplot as plt    #引入matplotlib的pyplot子库,用于画简单的2D图

 from matplotlib.ticker import FuncFormatter, MaxNLocator 

 import random

 x= range(20) 

 y= [random.randint(0,20) for _ in range(20)]

 fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot(111) #建立对象 labels = [1,2,3,4,5,6,7,8,9,10,'a','b','cc','d','e','f','g','h','*%$',''] def format_fn(tick_val, tick_pos):
if int(tick_val) in x:
return labels[int(tick_val)]
else:
return '' ax.xaxis.set_major_formatter(FuncFormatter(format_fn))
ax.xaxis.set_major_locator(MaxNLocator(integer=True)) plt.plot(x,y,'o-',label=u"线条") #画图
plt.show()
plt.savefig("temp.png")

这样坐标轴既可以含有字符串,同时也可以含有数字。

三、标题等的中文显示:

用到了如下库和语句:

from matplotlib.font_manager import FontProperties

font1 = FontProperties(fname=r"c:\windows\fonts\simsun.ttc",size=8) #可指定计算机内的任意字体,size为字体大小

plt.title(u"标题",fontproperties=font1)
plt.xlabel(u"x轴)",fontproperties=font1)
plt.ylabel(u"Y轴",fontproperties=font1)
ax.legend(prop=font1, loc="upper right")

这样用到上面程序里,则可以显示中文:

# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt    #引入matplotlib的pyplot子库,用于画简单的2D图
from matplotlib.ticker import FuncFormatter, MaxNLocator
import random
from matplotlib.font_manager import FontProperties x= range(20) y= [random.randint(0,20) for _ in range(20)] fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot(111) #建立对象 labels = [1,2,3,4,5,6,7,8,9,10,'a','b','cc','d','e','f','g','h','*%$',''] def format_fn(tick_val, tick_pos):
if int(tick_val) in x:
return labels[int(tick_val)]
else:
return '' ax.xaxis.set_major_formatter(FuncFormatter(format_fn))
ax.xaxis.set_major_locator(MaxNLocator(integer=True))
#可指定计算机内的任意字体,size为字体大小
font1 = FontProperties(fname=r"c:\windows\fonts\simsun.ttc",size=20)
plt.title(u"标题",fontproperties=font1)
plt.xlabel(u"x轴",fontproperties=font1)
plt.ylabel(u"Y轴",fontproperties=font1) plt.plot(x,y,'o-',label=u"线条") #画图
ax.legend(prop=font1, loc="upper right")
plt.show()
plt.savefig("temp.png")

画出的图如下:

四、不显示图片,以便进行批处理:

import matplotlib
matplotlib.use('Agg')

需加在 import matplotlib.pyplot as plt 之前,然后删掉plt.show()即可。

python中matplotlib画折线图实例(坐标轴数字、字符串混搭及标题中文显示)的更多相关文章

  1. python用matplotlib画折线图

    折线图: import matplotlib.pyplot as plt y1=[10,13,5,40,30,60,70,12,55,25] x1=range(0,10) x2=range(0,10) ...

  2. Matplotlib学习---用matplotlib画折线图(line chart)

    这里利用Jake Vanderplas所著的<Python数据科学手册>一书中的数据,学习画图. 数据地址:https://raw.githubusercontent.com/jakevd ...

  3. python使用matplotlib绘制折线图教程

    Matplotlib是一个Python工具箱,用于科学计算的数据可视化.借助它,Python可以绘制如Matlab和Octave多种多样的数据图形.下面这篇文章主要介绍了python使用matplot ...

  4. 【Python】matplotlib绘制折线图

    一.绘制简单的折线图 import matplotlib.pyplot as plt squares=[1,4,9,16,25] plt.plot(squares) plt.show() 我们首先导入 ...

  5. Python3 使用 matplotlib 画折线图

    ChartUtil.py import matplotlib.pyplot as plt from pylab import mpl def plotLine(xData,yData,xLabel,c ...

  6. SAS 画折线图PROC GPLOT

    虽然最后做成PPT里的图表会被要求用EXCEL画,但当我们只是在分析的过程中,想看看数据的走势,直接在SAS里画会比EXCEL画便捷的多. 修改起来也会更加的简单,,不用不断的修改程序然后刷新EXCE ...

  7. python 中matplotlib 绘图

    python 中matplotlib 绘图 数学建模需要,对于绘图进行简单学习 matpoltlib之类的包安装建议之间用anaconda 绘制一条y=x^2的曲线 #比如我们要绘制一条y=x^2的曲 ...

  8. Matplotlib学习---用matplotlib画雷达图(radar chart)

    雷达图常用于对多项指标的全面分析.例如:HR想要比较两个应聘者的综合素质,用雷达图分别画出来,就可以进行直观的比较. 用Matplotlib画雷达图需要使用极坐标体系,可点击此链接,查看对极坐标体系的 ...

  9. echars画折线图的一种数据处理方式

    echars画折线图的一种数据处理方式 <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...

随机推荐

  1. Oracel基础知识

    1.查看oracle环境变量命令  echo  %path% 2.监听程序:Oracle服务器端的一种网络服务.监听程序创建在数据库的服务器端,主要作用监视客户的连接请求.因此在客户端创建监听毫无意义 ...

  2. python习题 (1):login

    #!/uer/bin/env python # _*_ coding: utf-8 _*_ import sys retry_limit = 3 retry_count = 0 account_fil ...

  3. iOS之九宫格图片

    照片 现在人们的生活越来越丰富了,很多美好的瞬间都定格在一张张色彩绚丽的照片上,或许把照片珍藏在相册里,或许通过社交软件分享给亲朋好友.那社交软件上的照片是以什么形式展现的呢?那么接下来就要说到九宫格 ...

  4. 第二章 编写与设置Servlet

    2.1 第一个Servlet package cc.openhome; import javax.servlet.ServletException; import javax.servlet.http ...

  5. DTD文档模型和HTML基础

    html是超文本标记语言,现在常用到的2中文档格式是html5和XHTML 1.0 Transitiona(过渡). <!DOCTYPE html> <!--当前文档为html5-- ...

  6. Oracle 11g新特性延迟段创建和truncate的增强

    下面测试Oracle 11g开始的新特性truncate的增强和延迟段空间创建. Oracle从11g开始,当用户创建一张空表的时候不会先分配段和空间,只有当对这张表插入第一行数据的时候才分配段和空间 ...

  7. Win10 UI介绍之Titlebar

    活动状态 非活动状态 var titleBar = ApplicationView.GetForCurrentView().TitleBar; titleBar.BackgroundColor = C ...

  8. Expression: is_block_type_valid(header->block_use)

      VS2015 用 openmesh read_mesh 读取网格时,这样一段代码 void CPathFace::test2() { string file = ".\\data\\fa ...

  9. java基础(三)

    1.枚举类,使用enum定义的枚举类默认继承java.lang.Enum,而不是Object类.枚举类的所有实例必须在枚举类中显示列出,否则这个枚举类永远都不能产生实例.相关内容较多,需要后续继续跟进 ...

  10. MySQL - 问题集 - "Waiting for table metadata lock"(待完善)

    待完善.show processlist; 可参考1:http://blog.csdn.net/huochuangchuang/article/details/49423893 可参考2:http:/ ...