
正文
python中画函数曲线 python中如何画曲线图
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
Python matplotlib之函数图像绘制、线条rc参数设置
为避免中文显示出错,需导入matplotlib.pylab库
1.2.1 确定数据
1.2.2 创建画布
1.2.3 添加标题
1.2.4 添加x,y轴名称
1.2.5 添加x,y轴范围
1.2.6 添加x,y轴刻度
1.2.7 绘制曲线、图例, 并保存图片
保存图片时,dpi为清晰度,数值越高越清晰。请注意,函数结尾处,必须加plt.show(),不然图像不显示。
绘制流程与绘制不含子图的图像一致,只需注意一点:创建画布。
合理调整figsize、dpi,可避免出现第一幅图横轴名称与第二幅图标题相互遮盖的现象.
2.2.1 rc参数类型
2.2.2 方法1:使用rcParams设置
2.2.3 方法2:plot内设置
2.2.4 方法3:plot内简化设置
方法2中,线条形状,linestyle可简写为ls;线条宽度,linewidth可简写为lw;线条颜色,color可简写为c,等等。
相关问答
Q1: Python如何画函数的曲线
输入以下代码导入我们用到的函数库。
import numpy as np
import matplotlib.pyplot as plt
x=np.arange(0,5,0.1);
y=np.sin(x);
plt.plot(x,y)
采用刚才代码后有可能无法显示下图,然后在输入以下代码就可以了:
plt.show()
Q2: python怎么画曲线
打开Python,使用import导入numpy和matplotlib.pyplot模块。输入函数数据,然后使用plt.show()展示绘制的图像即可。
Q3: 不能直接写出函数的表达式 怎么在python里画函数图象呢?
不写出y=f(x)这样python中画函数曲线的表达式python中画函数曲线,由隐函数的等式直接绘制图像python中画函数曲线,以x²+y²+xy=1的图像为例,使用sympy间接调用matplotlib工具的代码和该二次曲线图像如下(注意python里的乘幂符号是**而不是^,还有,python的sympy工具箱的等式不是a==b,而是a-b或者Eq(a,b),这几点和matlab的区别很大)
直接在命令提示行的里面运行代码的效果
from sympy import *;
x,y=symbols('x y');
plotting.plot_implicit(x**2+y**2+x*y-1);
Q4: python之KS曲线
# 自定义绘制ks曲线python中画函数曲线的函数
def plot_ks(y_test, y_score, positive_flag):
# 对y_test,y_score重新设置索引
y_test.index = np.arange(len(y_test))
#y_score.index = np.arange(len(y_score))
# 构建目标数据集
target_data = pd.DataFrame({'y_test':y_test, 'y_score':y_score})
# 按y_score降序排列
target_data.sort_values(by = 'y_score', ascending = False, inplace = True)
# 自定义分位点
cuts = np.arange(0.1,1,0.1)
# 计算各分位点对应python中画函数曲线的Score值
index = len(target_data.y_score)*cuts
scores = target_data.y_score.iloc[index.astype('int')]
# 根据不同python中画函数曲线的Score值python中画函数曲线,计算Sensitivity和Specificity
Sensitivity = []
Specificity = []
for score in scores:
# 正例覆盖样本数量与实际正例样本量
positive_recall = target_data.loc[(target_data.y_test == positive_flag) (target_data.y_scorescore),:].shape[0]
positive = sum(target_data.y_test == positive_flag)
# 负例覆盖样本数量与实际负例样本量
negative_recall = target_data.loc[(target_data.y_test != positive_flag) (target_data.y_score=score),:].shape[0]
negative = sum(target_data.y_test != positive_flag)
Sensitivity.append(positive_recall/positive)
Specificity.append(negative_recall/negative)
# 构建绘图数据
plot_data = pd.DataFrame({'cuts':cuts,'y1':1-np.array(Specificity),'y2':np.array(Sensitivity),
'ks':np.array(Sensitivity)-(1-np.array(Specificity))})
# 寻找Sensitivity和1-Specificity之差的最大值索引
max_ks_index = np.argmax(plot_data.ks)
plt.plot([0]+cuts.tolist()+[1], [0]+plot_data.y1.tolist()+[1], label = '1-Specificity')
plt.plot([0]+cuts.tolist()+[1], [0]+plot_data.y2.tolist()+[1], label = 'Sensitivity')
# 添加参考线
plt.vlines(plot_data.cuts[max_ks_index], ymin = plot_data.y1[max_ks_index],
ymax = plot_data.y2[max_ks_index], linestyles = '--')
# 添加文本信息
plt.text(x = plot_data.cuts[max_ks_index]+0.01,
y = plot_data.y1[max_ks_index]+plot_data.ks[max_ks_index]/2,
s = 'KS= %.2f' %plot_data.ks[max_ks_index])
# 显示图例
plt.legend()
# 显示图形
plt.show()
# 调用自定义函数python中画函数曲线,绘制K-S曲线
plot_ks(y_test = y_test, y_score = y_score, positive_flag = 1)
Q5: 用Python或MATLAB如何画三元平方和函数曲线???
Python代码 import numpy as npimport matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3Dfig = plt.figure()ax = Axes3D(fig)X = np.arange(-4, 4, 0.25)Y = np.arange(-4, 4, 0.25)X, Y = np.meshgrid(X, Y)R = np.sqrt(X**2 + Y**2)Z = np.sin(R)ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='hot')plt.show() 运行结果:
python中画函数曲线的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于python中如何画曲线图、python中画函数曲线的信息别忘了在本站进行查找喔。








