matplotlib.pyplot画图并导出保存的实例

yipeiwu_com6年前Python基础

我就废话不多说了,直接上代码吧!

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
bar_positions=[1,2,3,4]
bar_heights=[1,2,3,4]
print(np.arange(len([2,2,3,4,5])+1))
ax.bar(np.arange(len([2,2,3,4,5])),[1,2,3,4,5], 0.5)#设置x,y数据,区间
ax.set_xticks([1,2,3,4,5,6])#设置x轴刻度
ax.set_xticklabels([1,2,3,4,5], rotation=45)#设置x轴标签,旋转45度
ax.set_yticks([1,2,3,4,5,6])#设置x轴刻度
ax.set_yticklabels([1,2,3,4,5], rotation=45)#设置y轴标签,旋转45度
ax.set_ylim(0, 7)#设置y轴范围
ax.set_xlim(0, 7)#设置x轴范围,当然轴数据范围跟 坐标刻度不要冲突就好
ax.set_facecolor("orange")#设置背景颜色为红色
for a,b in zip(bar_positions,bar_heights):#显示数据标签
  plt.text(a, b+0.05, '%.0f' % b, ha='center', va= 'bottom',fontsize=7)
plt.savefig('D:\\python_practice\\导出的图片.png')#保存图片
plt.show()
 
 
 
在matplotlib一般使用plt.figure来设置窗口尺寸。
plt.figure(figsize=(10, 10)) 
但是如果使用plt.subplots,那么这种方法就无效,只能通过subplots自己设置窗口大小。
fig, ax1 = plt.subplots(figsize=(10, 10)) 

以上这篇matplotlib.pyplot画图并导出保存的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python 移动光标位置的方法

如下所示: x = file('1.txt','r') print x.tell() #显示当前光标位置 x.seek(3) print x.tell() #没有设置光标位置,...

Python使用Turtle库绘制一棵西兰花

Turtle库是Python中一个强大的绘制图像的函数库,灵活使用Turtle库可以绘制各种好看的图像。 下面介绍使用Turtle库绘制一棵西兰花。 绘制一棵西兰花,从主干出发以一定的角...

解析Python3中的Import

Python import的搜索路径 import的搜索路径为: 搜索「内置模块」(built-in module) 搜索 sys.path 中的路径 而sys.path在...

Python2.x版本中cmp()方法的使用教程

 cmp()方法返回两个数的差的符号: -1 如果 x < y, 0 如果 x == y, 或者 1 如果 x > y . 语法 以下是cmp()方法的语法:...

Python3多线程操作简单示例

本文实例讲述了Python3多线程操作。分享给大家供大家参考,具体如下: python3 线程中常用的两个模块为: _thread threading(推荐使用) thread 模块已被...