zoukankan      html  css  js  c++  java
  • python-matplotlib学习(1)

    1 import matplotlib.pyplot as plt
    2 import numpy as np
    3 
    4 x=np.linspace(-1,1,50)
    5 y=2*x+1
    6 plt.plot(x,y)
    7 plt.show()

    2:绘制y=x*x 的图像

    1 import matplotlib.pyplot as plt
    2 import numpy as np
    3 
    4 x=np.linspace(-1,1,50)
    5 #y=2*x+1
    6 y=x**2
    7 plt.plot(x,y)
    8 plt.show()

    3:figure 的认识

     1 import matplotlib.pyplot as plt
     2 import numpy as np
     3 
     4 x=np.linspace(-1,1,50)
     5 y1=2*x+1
     6 y2=x**2
     7 
     8 plt.figure("第一个figure")
     9 plt.plot(x,y1)
    10 
    11 plt.figure() #看看默认的名字
    12 plt.plot(x,y2)
    13 plt.plot(x,y1,color='red',linewidth=2.0,linestyle='--') # 绘制两条线 颜色为红色,宽度为2,虚线
    14 
    15 
    16 plt.show()

     1 import matplotlib.pyplot as plt
     2 import numpy as np
     3 
     4 x=np.linspace(-3,3,50)
     5 y1=2*x+1
     6 y2=x**2
     7 
     8 plt.figure() #看看默认的名字
     9 plt.plot(x,y2)
    10 plt.plot(x,y1,color='red',linewidth=2.0,linestyle='--') # 绘制两条线 颜色为红色,宽度为2,虚线
    11 plt.xlim(-1,2) #x轴取值范围
    12 plt.ylim(-2,3) #y轴取值范围
    13 
    14 plt.xlabel("x轴")
    15 plt.ylabel("y轴") #描述
    16 
    17 plt.show()
    View Code

    中文乱码出现解决方法

    找到matplot安装路径下的matplotlibrc 文件用文本编辑工具打开并修改 
    #font.family         : sans-serif 去掉 #

    #font.sans-serif 这一行 去点 # 并添加微软雅黑字体 

    这是网上查到的解决办法我试了一下然并卵啊。

    不了了之了暂时,等找到更好的方法后再说吧

    替换坐标轴度量值

     1 import matplotlib.pyplot as plt
     2 import numpy as np
     3 
     4 x=np.linspace(-3,3,50)
     5 y1=2*x+1
     6 y2=x**2
     7 
     8 plt.figure("figure能不能用中文") #看看默认的名字
     9 plt.plot(x,y2)
    10 plt.plot(x,y1,color='red',linewidth=2.0,linestyle='--') # 绘制两条线 颜色为红色,宽度为2,虚线
    11 
    12 plt.xlim(-1,2) #x轴取值范围
    13 plt.ylim(-2,3) #y轴取值范围
    14 
    15 
    16 plt.xlabel("I am x")
    17 plt.ylabel("I am y") #描述
    18 #plt.legend(prop=font)
    19 
    20 new_ticks=np.linspace(-1,2,5)
    21 print(new_ticks)
    22 plt.xticks(new_ticks)
    23 plt.yticks([-2,-1.8,-1,1.22,3],[r"$really bad$",r"$bad$","$normal$","$good$","$really good$"])
    24 
    25 plt.show()

     1 import matplotlib.pyplot as plt
     2 import numpy as np
     3 
     4 x=np.linspace(-3,3,50)
     5 y1=2*x+1
     6 y2=x**2
     7 
     8 plt.figure("figure能不能用中文") #看看默认的名字
     9 plt.plot(x,y2)
    10 plt.plot(x,y1,color='red',linewidth=2.0,linestyle='--') # 绘制两条线 颜色为红色,宽度为2,虚线
    11 
    12 plt.xlim(-1,2) #x轴取值范围
    13 plt.ylim(-2,3) #y轴取值范围
    14 
    15 
    16 plt.xlabel("I am x")
    17 plt.ylabel("I am y") #描述
    18 #plt.legend(prop=font)
    19 
    20 new_ticks=np.linspace(-1,2,5)
    21 print(new_ticks)
    22 plt.xticks(new_ticks)
    23 plt.yticks([-2,-1.8,-1,1.22,3],[r"$really bad$",r"$bad$","$normal$","$good$","$really good$"])
    24 
    25 #gca ='get current axis'
    26 
    27 ax=plt.gca()
    28 ax.spines['right'].set_color('none') #右侧轴隐藏
    29 ax.spines['top'].set_color('none') #上侧轴隐藏
    30 ax.xaxis.set_ticks_position('bottom')
    31 ax.yaxis.set_ticks_position('left')
    32 ax.spines["bottom"].set_position(('data',0)) # 设置x轴位置
    33 ax.spines["left"].set_position(('data',0))
    34 
    35 plt.show()
    View Code

     1 import matplotlib.pyplot as plt
     2 import numpy as np
     3 
     4 x=np.linspace(-5,5,100)
     5 y1=2*x+1
     6 y2=x**2
     7 
     8 plt.xlim(-5,5) #x轴取值范围
     9 plt.ylim(-5,25) #y轴取值范围
    10 plt.xlabel("I am x")
    11 plt.ylabel("I am y") #描述
    12 
    13 l1,=plt.plot(x,y1,label="y=2x+1")
    14 l2,=plt.plot(x,y2,color='red',linewidth=1.0,linestyle='--',label="y=x*x")# 逗号为必须
    15 plt.legend(handles=[l1,l2,],labels=["aaa","bbb"],loc='best')
    16 
    17 plt.show()
    View Code

  • 相关阅读:
    计算机图形学和OpenGL(二)坐标系和绘制点线函数
    计算机图形学和OpenGL(一)OpenGL初步
    C++ 实现链表常用功能
    Cocos2d-x环境搭建
    2014年学习计划
    2013年终总结
    AS3开发必须掌握的内容
    starling性能优化
    后补个2012年的总结吧
    原生javascript实现图片懒加载
  • 原文地址:https://www.cnblogs.com/shuiliuhualuo/p/6520539.html
Copyright © 2011-2022 走看看