Python 绘制图表

使用matplotlib.pyplot库

 

1.简单图表(柱状图,点线图)

from matplotlib.pyplot import *

x = [1,2,3,4]
y = [5,4,3,2]

#创建新的图表
figure()


#将图表分割成2*3的表格,或者subplot(2,3,1),第三个参数为凸显标号
subplot(231)
plot(x, y)

#创建新垂直柱状图
subplot(232)
bar(x, y)

#创建新水平柱状图
subplot(233)
barh(x, y)

#bar  bottom : 标量或类似数组,可选条形基座的y坐标
subplot(234)
bar(x, y)
y1 = [7,8,5,3]
bar(x, y1, bottom=y, color = 'r')

#创建箱线图
subplot(235)
boxplot(x)

#创建散点图 scatter
subplot(236)
scatter(x,y)

show()

 

2.简单的正弦与余弦

import matplotlib.pyplot as pl
import numpy as np

# generate uniformly distributed 
# 256 points from -pi to pi, inclusive
x = np.linspace(-np.pi, np.pi, num=256, endpoint=True)

# compute cos for every x
y = np.cos(x)

# compute sin for every x
y1 = np.sin(x)

# plot both cos and sin
pl.plot(x, y)
pl.plot(x, y1)

pl.show()

 

使图标更为完整

from pylab import *
import numpy as np


x = np.linspace(-np.pi, np.pi, num=256, endpoint=True)


y = np.cos(x)
y1 = np.sin(x)


plot(x, y)
plot(x, y1)

# define plot title   $\sin$表示希腊字母
title("Functions $\sin$ and $\cos$")

# set x limit
xlim(-3.0, 3.0)
# set y limit
ylim(-1.0, 1.0)

# format ticks at specific values
xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
          [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$'])
yticks([-1, 0, +1],
          [r'$-1$', r'$0$', r'$+1$'])

show()

 

3. 设置坐标轴的长度和范围

使用 antoscale() 方法,该方法会自动计算坐标轴的最佳大小以适应数据的显示。

向图形中增加新的坐标轴,调用axes() 方法。

添加一条线,可以调用matplotlib.pyplot.axhline()和matplotlib.pyplot.axvline()。两种方法会根据给定的x和y值相应地绘制出相对于坐标轴的水平线和垂直线。

pyplot.grid() 切换网格的状态:

matplotlib.pyplot.grid(b=Nonewhich='major'axis='both'**kwargs)

 

4.设置图表的线型.属性和格式化字符串

 

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值