matplotlib的一些简单用法 英语很重要

本文介绍使用Python的matplotlib库绘制正弦和余弦曲线的方法。通过设置坐标轴、图例、网格线等属性,实现了美观且易于理解的数学函数图像展示。特别地,文章展示了如何调整坐标轴的位置,使得原点处于图表中央,并通过不同颜色和样式来区分正弦和余弦函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#encoding = utf-8#
 import numpy as np
 import matplotlib.pyplot as plt


def main():

    #line#
     x = np.linspace(-np.pi,np.pi, 256 endpoint = True#-pi --- pi 256numbers#
     c,s = np.cos(x),np.sin(x)#confine sin \ cos函数#
     plt.figure( 1#first figure#
     plt.plot(x,c, color = “blue” linewidth = 1.0 linestyle = “ - ” label = “COS” alpha = 0.2
    plt.plot(x,s,“r *”label = “SIN”#r *  -  point#
     ax = plt.gca()#     coordinate axis#
 ax.spines [ “right” ] .set_color(“none “
    ax.spines [ “top” ] .set_color(“none”#使右轴和顶轴消失#
     ax.spines [ “left” ] .set_position((“data”0))#向左移动底部到[0,0]#
     ax.spines [ “bottom” ] .set_position((“data”0))
    ax.xaxis.set_ticks_position(“bottom”
    ax.yaxis.set_ticks_position(“左”
    plt.xticks([ - np.pi,-np.pi / 20,np.pi / 2,np.pi]
               [ r'$  -  \ pi $'r'$  -  \ pi / 2 $'r'$ 0 $'r'$ + \ pi / 2 $'r'$ + \ pi $' ])
    plt.yticks(np.linspace( - 115端点 = ))
     标签 ax.get_xticklabels()+ ax.get_yticklabels():
        label.set_fontsize(16
        label.set_bbox(dictfacecolor = “white”edgecolor = “None”alpha = 0.3))
    plt.legend(LOC = “左上”#the LOC位置#
     plt.grid() 网格线#
     plt.axis([ - 11, - 0.51 ])#show范围#
     plt.fill_between(X ,x < 0.5,c,c> 0.5color = “green”alpha = 0.4#fill#
     t = 1
     plt.plot([t,t],[ 0,np.cos(t)],“y “linewidth = 3linestyle = ” - “#find point(t,t),然后y = cos(t)#
     t.annotate( “cos(1)” xy =(t,np.cos( 1)), xycoords = “data” xytext =( + 10,+ 30),
                textcoords = “offset points” arrowprops = dict arrowstyle = “ - >” connectionstyle = “arc3,rad = 0.2”)) # arrows radian#
 plt.show()
    

如果 __name __ == __main__
     主要()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值