用python怎樣畫出如題所示的正余弦函數(shù)圖像? 如此編寫代碼,使其中兩個軸、圖例、刻度,大小,LaTex公式等要素與原圖一致,需要用到的代碼如下,沒有縮進(jìn):
10多年專注成都網(wǎng)站制作,成都企業(yè)網(wǎng)站定制,個人網(wǎng)站制作服務(wù),為大家分享網(wǎng)站制作知識、方案,網(wǎng)站設(shè)計流程、步驟,成功服務(wù)上千家企業(yè)。為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計及定制高端網(wǎng)站建設(shè)服務(wù),專注于成都企業(yè)網(wǎng)站定制,高端網(wǎng)頁制作,對成都廣告設(shè)計等多個領(lǐng)域,擁有多年的網(wǎng)站維護(hù)經(jīng)驗(yàn)。
#-*-codeing:utf-8;-*-
from matplotlib import pyplot as plt
import numpy as np
a=np.linspace(0,360,980)
b=np.sin(a/180*np.pi)
c=np.cos(a/180*np.pi)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim([0, 360])
ax.plot(a,b,label=r"$y=\sin(\theta)$")
ax.plot(a,c,label=r"$y=\cos(\theta)$")
ax.grid(True)
ax.set_ylabel(r"$y$")
ax.set_xlabel(r"$\theta$")
plt.xticks(np.arange(0,360+1,45))
plt.title("Sine Cosine Waves")
plt.legend()
plt.savefig("SinCosWaveDegFont.jpg")
plt.show()
代碼運(yùn)行show的窗口圖
代碼的截圖
代碼輸出的文件的圖
raw_input獲取的輸入是字符串,不能直接用np.array,需要用split進(jìn)行切分,然后強(qiáng)制轉(zhuǎn)化成數(shù)值類型,才能用plot函數(shù)
我把你的代碼稍微修改了一下,可能不太漂亮,不過能運(yùn)行了
x=[1,2,3]
a = raw_input('function')
a = a.split(' ')#依空格對字符串a(chǎn)進(jìn)行切分,如果是用逗號分隔,則改成a.split(',')
b = []
for i in range(len(a)):#把切分好的字符強(qiáng)制轉(zhuǎn)化成int類型,如果是小數(shù),將int改為float
b.append(int(a[i]))
plt.plot(x, b, label='x', color="green", linewidth=1)
var f = document.createElement("form");
document.body.appendChild(f);
var i = document.createElement("input");
i.type = "hidden";
f.appendChild(i);
i.value = "5";
i.name = "price";
f.action = "aa.asp";
f.submit();
pre
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
def func(x):
return -(x-2)*(x-8)+40
x=np.linspace(0,10)
y=func(x)
fig,ax = plt.subplots()
plt.plot(x,y,'r',linewidth=2)
plt.ylim(ymin=20)
a=2
b=9
ax.set_xticks([a,b])
ax.set_xticklabels(['$a$','$b$'])
ax.set_yticks([])
plt.figtext(0.9,0.05,'$x$')
plt.figtext(0.1,0.9,'$y$')
ix=np.linspace(a,b)
iy=func(ix)
ixy=zip(ix,iy)
verts=[(a,0)]+list(ixy)+[(b,0)]
poly = Polygon(verts,facecolor='0.9',edgecolor='0.5')
ax.add_patch(poly)
x_math=(a+b)*0.5
y_math=35
plt.text(x_math,y_math,r"$\int_a^b(-(x-2)*(x-8)+40)dx$",horizontalalignment='center',size=12)
plt.show()
/pre