python中matplotlib画折线图实例(坐标轴数字、字符串混搭及标题中文显示)
最近在用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画折线图实例(坐标轴数字、字符串混搭及标题中文显示)的更多相关文章
- 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) ...
- Matplotlib学习---用matplotlib画折线图(line chart)
这里利用Jake Vanderplas所著的<Python数据科学手册>一书中的数据,学习画图. 数据地址:https://raw.githubusercontent.com/jakevd ...
- python使用matplotlib绘制折线图教程
Matplotlib是一个Python工具箱,用于科学计算的数据可视化.借助它,Python可以绘制如Matlab和Octave多种多样的数据图形.下面这篇文章主要介绍了python使用matplot ...
- 【Python】matplotlib绘制折线图
一.绘制简单的折线图 import matplotlib.pyplot as plt squares=[1,4,9,16,25] plt.plot(squares) plt.show() 我们首先导入 ...
- Python3 使用 matplotlib 画折线图
ChartUtil.py import matplotlib.pyplot as plt from pylab import mpl def plotLine(xData,yData,xLabel,c ...
- SAS 画折线图PROC GPLOT
虽然最后做成PPT里的图表会被要求用EXCEL画,但当我们只是在分析的过程中,想看看数据的走势,直接在SAS里画会比EXCEL画便捷的多. 修改起来也会更加的简单,,不用不断的修改程序然后刷新EXCE ...
- python 中matplotlib 绘图
python 中matplotlib 绘图 数学建模需要,对于绘图进行简单学习 matpoltlib之类的包安装建议之间用anaconda 绘制一条y=x^2的曲线 #比如我们要绘制一条y=x^2的曲 ...
- Matplotlib学习---用matplotlib画雷达图(radar chart)
雷达图常用于对多项指标的全面分析.例如:HR想要比较两个应聘者的综合素质,用雷达图分别画出来,就可以进行直观的比较. 用Matplotlib画雷达图需要使用极坐标体系,可点击此链接,查看对极坐标体系的 ...
- echars画折线图的一种数据处理方式
echars画折线图的一种数据处理方式 <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...
随机推荐
- 网页设计之jQuery
1.在html中引入css和jQuery <!DOCTYPE html> <html lang="en"> <head> <meta ch ...
- 17. Letter Combinations of a Phone Number
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- 由一段字符串中查找URL引出——正则表达式
需求很简单,是从一段文本中匹配出其中的超链接.基本的做法就是用正则表达式去匹配.但是有这样一个问题. 网上大部分的识别URL的正则表达式url末尾有空格的情况下可以正确识别.比如这样的情况: &quo ...
- Core Data使用
注意:每次修改CoreData的Attribute时记得把应用给删除重装,要不会崩,因为建立的数据库文件还在该目录下,里面的字段没有更改,所以不能匹配就会崩溃,切忌,要不就每次进来把文件先删除,再建立 ...
- python多线程编程
Python多线程编程中常用方法: 1.join()方法:如果一个线程或者在函数执行的过程中调用另一个线程,并且希望待其完成操作后才能执行,那么在调用线程的时就可以使用被调线程的join方法join( ...
- jquery跳出each循环
答案是使用 return false; 切记哦,不是使用break;也不是直接使用return; jQuery中each类似于javascript的for循环 但不同于for循环的是在each里面不 ...
- mysql在linux下修改存储路径
通过下面几步即可修改路径,这里的路径都是测试的路径,一般默认安装路径在/var/lib/mysql下,真正配置按照真实路径配置. 1.修改/etc/sysconfig/selinux文件:#SELIN ...
- [LeetCode] Single Number
Given an array of integers, every element appears twice except for one. Find that single one. Note: ...
- 【TYVJ1864】[Poetize I]守卫者的挑战 概率与期望
[TYVJ1864][Poetize I]守卫者的挑战 描述 打开了黑魔法师Vani的大门,队员们在迷宫般的路上漫无目的地搜寻着关押applepi的监狱的所在地.突然,眼前一道亮光闪过."我 ...
- hibernate的@EmbeddedId嵌入式主键详解
一.前言 在我们的日常开发中,有时候会用到数据库进行设计的时候,采用了复合主键来来保证唯一性,下面介绍一下采用hibernate的@EmbeddedId嵌入式主键. 二.说明 设计一个学生类,包含了三 ...