matplotlib 绘制副坐标轴

matplotlib 绘制副坐标轴

    • 代码
    • 效果

代码

	import matplotlib.pyplot as plt
	import numpy as np
	plt.rcParams['font.sans-serif'] = ['SimHei'] # 步骤一(替换sans-serif字体)
	plt.rcParams['axes.unicode_minus'] = False   # 步骤二(解决坐标轴负数的负号显示问题)
	
	
	x = np.arange(0,10,0.1)
	y1 = 0.05*x**2
	y2 = 2*x+1
	
	fig,ax1 = plt.subplots()
	#坐标轴反转
	ax2 = ax1.twinx()
	ax1.plot(x,y1,'r-')
	ax2.plot(x,y2,'b-.')
	
	ax1.set_xlabel('横坐标')
	ax1.set_ylabel('y1纵坐标',color='r')
	ax2.set_ylabel('y2纵坐标',color='b')
	
	plt.show()


效果

matplotlib 绘制副坐标轴_第1张图片

你可能感兴趣的:(matplotlib,python,python)