Python 绘制图表

目录

计算二维函数曲面,并使用Mayavi绘图。

演示图表的标注

修改坐标轴的文字

绘制柱状图

绘制轮廓图

修改字体

绘制函数曲线图

显示绘制图形

创建多图子图

离散图和阴影图

给曲线添加阴影效果

绘制简单的曲线

演示matplotlib的三维绘图功能

在不同的坐标系中添加文字


计算二维函数曲面,并使用Mayavi绘图。

# -*- coding: utf-8 -*-
"""
计算二维函数曲面,并使用Mayavi绘图。
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm


y, x = np.ogrid[-2:2:200j, -2:2:200j]
z = x * np.exp( - x**2 - y**2) 

extent = [np.min(x), np.max(x), np.min(y), np.max(y)] 

plt.figure(figsize=(10,3))
plt.subplot(121)
plt.imshow(z, extent= extent, origin="lower") 
plt.colorbar()
plt.subplot(122)
plt.imshow(z, extent=extent, cmap=cm.gray, origin="lower")
plt.colorbar()
plt.show()

演示图表的标注

# -*- coding: utf-8 -*-
"""
演示图表的标注。
"""
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['axes.unicode_minus']=False 

def func1(x): 
    return 0.6*x + 0.3

def func2(x): 
    return 0.4*x*x + 0.1*x + 0.2
    
def find_curve_intersects(x, y1, y2):
    d = y1 - y2
    idx = np.where(d[:-1]*d[1:]<=0)[0]
    x1, x2 = x[idx], x[idx+1]
    d1, d2 = d[idx], d[idx+1]
    return -d1*(x2-x1)/(d2-d1) + x1

x = np.linspace(-3,3,100) 
f1 = func1(x)
f2 = func2(x)
plt.figure(figsize=(8,4))
plt.plot(x, f1)
plt.plot(x, f2)

x1, x2 = find_curve_intersects(x, f1, f2) 
plt.plot(x1, func1(x1), "o") 
plt.plot(x2, func1(x2), "o")

plt.fill_between(x, f1, f2, where=f1>f2, facecolor="green", alpha=0.5) 

from matplotlib import transforms
ax = plt.gca()
trans = transforms.blended_transform_factory(ax.transData, ax.transAxes)
plt.fill_between([x1, x2], 0, 1, transform=trans, alpha=0.1) 

a = plt.text(0.05, 0.95, u"直线和二次曲线的交点",  
    transform=ax.transAxes,
    verticalalignment = "top",
    fontsize = 18,
    bbox={"facecolor":"red","alpha":0.4,"pad":10}
)

arrow = {"arrowstyle":"fancy,tail_width=0.6", 
         "facecolor":"gray", 
         "connectionstyle":"arc3,rad=-0.3"}

plt.annotate(u"交点", 
    xy=(x1, func1(x1)), xycoords="data",
    xytext=(0.05, 0.5), textcoords="axes fraction",
    arrowprops = arrow)
                  
plt.annotate(u"交点", 
    xy=(x2, func1(x2)), xycoords="data",
    xytext=(0.05, 0.5), textcoords="axes fraction",
    arrowprops = arrow)

xm = (x1+x2)/2
ym = (func1(xm) - func2(xm))/2+func2(xm)
o = plt.annotate(u"直线大于曲线区域", 
    xy =(xm, ym), xycoords="data",
    xytext = (30, -30), textcoords="offset points",    
    bbox={"boxstyle":"round", "facecolor":(1.0, 0.7, 0.7), "edgecolor":"none"},
    fontsize=16,
    arrowprops={"arrowstyle":"->"}
)
plt.show()

 

 

修改坐标轴的文字

# -*- coding: utf-8 -*-
"""
修改坐标轴的文字
"""
import matplotlib.pyplot as pl
from m
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值