matplotlib画图基础,华氏温度到摄氏温度的转换,matplotlib摄氏,由于fedora19默认


由于fedora19默认安装的weather app不能显示摄氏温度,想到要画这么张图。此代码中涉及基本的plot、scatter函数,坐标轴的控制等内容。

# -*- coding: utf-8 -*-import numpy as npimport matplotlib.pyplot as pltimport matplotlibF = np.arange(-50, 240, 0.1)C = (F - 32)/1.8# Basicplt.plot(F, C, 'r-', linewidth = 0.5)plt.title('Convert the unit of temperature')# Revise, ticksax = plt.gca()ax.spines['right'].set_color('none')ax.spines['top'].set_color('none')ax.xaxis.set_ticks_position('bottom')ax.spines['bottom'].set_position(('data',0))ax.yaxis.set_ticks_position('left')ax.spines['left'].set_position(('data',0))ax.xaxis.set_major_locator(matplotlib.ticker.MultipleLocator(20))ax.xaxis.set_minor_locator(matplotlib.ticker.MultipleLocator(2))ax.yaxis.set_major_locator(matplotlib.ticker.MultipleLocator(20))ax.xaxis.set_minor_locator(matplotlib.ticker.MultipleLocator(2))plt.grid(b = True)sc = np.array([-40, -20, 0, 20, 37, 60, 80, 100])sf = np.array([-40, 0, 32, 80, 37*1.8+32, 140, 180, 100*1.8+32])plt.yticks(sc); plt.xticks(sf)# Label the specific temperaturesplt.scatter([32, ], [0, ], 50, color = 'blue')plt.plot([0, 37*1.8+32], [37, 37], color = 'blue', linewidth = 2.5, linestyle = '--')plt.plot([37*1.8+32, 37*1.8+32], [0, 37], color = 'blue', linewidth = 2.5, linestyle = '--')plt.scatter([37*1.8+32, ], [37, ], 50, color = 'blue')plt.plot([0, 100*1.8+32], [100, 100], color = 'blue', linewidth = 2.5, linestyle = '--')plt.plot([100*1.8+32, 100*1.8+32], [0, 100], color = 'blue', linewidth = 2.5, linestyle = '--')plt.scatter([100*1.8+32, ], [100, ], 50, color = 'blue')plt.xlabel('Fahrenheit')plt.ylabel('Centigrade')plt.savefig('FtoC.png')plt.close()#该片段来自于http://byrx.net

评论关闭