绘制X^X函数
2017-09-10 本文已影响11人
b485c88ab697
练习:绘制X^X函数
\
#coding:utf-8
#/usr/bin/python
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
def f(x):
y = np.ones_like(x)
i = x > 0
y[i] = np.power(x[i], x[i])
i = x < 0
y[i] = np.power(-x[i], -x[i])
return y
if __name__ == '__main__':
mpl.rcParams['font.sans-serif'] = [u'SimHei']
mpl.rcParams['axes.unicode_minus'] = False # win正常显示中文设置
plt.figure(facecolor='w')
x = np.linspace(-1.3, 1.3, 101)
y = f(x)
plt.plot(x, y, 'g-', label='x^x', linewidth=2)
plt.grid()
plt.legend(loc='upper left')
plt.show()