PY学习记录#12

可视化去你妈的

 1 import matplotlib.pyplot as plt
 2 import numpy as np
 3 def label(x):
 4     sinx = np.sin(x)
 5     cosx = np.cos(x)
 6     plt.plot(x,cosx,color="blue",marker="o",linewidth=2.5,linestyle="-",label="cos(x)")
 7     plt.plot(x,sinx,color="red",linewidth=2.5,linestyle="--",label="sin(x)")
 8     plt.legend(loc='lower right')
 9 def title():
10     plt.title('正弦余弦函数曲线', fontproperties="SimHei")
11     plt.xlabel(u'x(弧度)', fontproperties="SimHei")
12     plt.ylabel(u'y', fontproperties="SimHei")
13 def drawlim():
14     plt.ylim(-1.1,1.1)
15     plt.yticks([-1,0,+1])
16     plt.xticks([0,np.pi/2, np.pi, 3*np.pi/2, 2*np.pi], [r'$0$', r'$\pi/2$', r'$\pi$', r'$3\pi/2$', r'$2\pi$'])
17     plt.axhline(0, linestyle='--', color='black', linewidth=1)
18     plt.axvline(0, linestyle='--', color='black', linewidth=1)
19 def fill(x):
20     plt.fill_between(x,np.cos(x),where=((x>=np.pi/2) & (x <=3*np.pi/2)),facecolor='grey',alpha=0.25)
21     plt.axhspan(0.25,0.5,0,0.5,color='green',alpha = 0.5)
22     plt.axhspan(-0.5,-0.25,color='green',alpha = 0.25)
23     plt.axvspan(3*np.pi/2-0.3,3*np.pi/2+0.3, color='red', alpha = 0.25)
24     plt.axvspan(np.pi/2-0.3,np.pi/2+0.3,0.5,1.0, color='red', alpha = 0.5)
25 def annotate():
26     t=2*np.pi/3
27     plt.plot([t, t], [0, np.sin(t)], color='red', linewidth=2.5, linestyle="--")
28     plt.scatter([t, ], [np.sin(t), ], 50, color='red')
29     plt.annotate(r'$\sin(\frac{2\pi}{3})=\frac{\sqrt{3}}{2}$', xy=(t, np.sin(t)), xycoords='data', xytext=(+30, 0), textcoords='offset points', fontsize=16, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
30 x=np.linspace(0,2*np.pi,50)
31 label(x)
32 title()
33 drawlim()
34 fill(x)
35 annotate()
36 plt.show()

 放类里面还可以

import numpy as np
import matplotlib.pyplot as plt
ans=[]
class pltt:
    def __init__(self):
        self.x=0
    def open(self):
        fh=open("text.txt","r+",encoding="utf-8")
        for i in fh:
            ans.append(i.rstrip())
    def setx(self,a,b):
        self.x=np.linspace(a,2*np.pi,b)
    def draw(self):
        xs=np.sin(self.x)
        ys=np.cos(self.x)
        plt.plot(self.x,xs)
        plt.plot(self.x,ys)
    def save(self,fi):
        plt.savefig(fi)

plttt=pltt()
plttt.open()
a,b=map(int,ans)
plttt.setx(a,b)
plttt.draw()
plttt.save("G:\\代码\\PythonNew\\ans.jpg")

 

posted @ 2021-12-16 20:43  JS_Pierre  阅读(102)  评论(0编辑  收藏  举报