您的位置:首页 > 编程语言 > Python开发

Python 20190209 绘制方程曲线、设置标签、中文等示例

2019-02-09 22:53 721 查看

1.设置中文使用matplotlib.font_manager,并重新定义字体font = FontProperties(fname=r"C:\Windows\Fonts\simhei.ttf", size=14)
2.定义坐标轴标签位置plt.text(5, -5, ‘X axis (Year)…’, ha=‘left’, rotation=0, wrap=True)
3.定义坐标轴位置ax.xaxis.set_ticks_position(‘bottom’)
4.定义坐标轴刻度ax.set_xticklabels([‘2017’,‘2018’,’ ‘,‘2020’,‘2021’],rotation=45,fontsize=‘small’)
5.定义图名标题plt.title(u’测试函数’, fontproperties=font,loc=‘left’)
6.定义曲线上主要坐标点plt.annotate("(%s,%s)" % xy, xy=xy, xytext=(-20, 10), textcoords=‘offset points’)
#-- coding: utf-8 –
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"C:\Windows\Fonts\simhei.ttf", size=14)

x = np.linspace(-5, 5, 50)
x1 = np.linspace(-5,5,5)
y1 = 3 * x1-4
y2 = -2*x * x+33
y3 = 3/x

plt.figure()

plt.text(5, -5, ‘X axis (Year)…’, ha=‘left’, rotation=0, wrap=True) #坐标轴标签重新设置
plt.text(-0.5, 30, ‘Y axis…’, ha=‘left’, rotation=90, wrap=True)

ax = plt.gca() # get current axis 获得坐标轴对象

ax.spines[‘right’].set_color(‘none’)
ax.spines[‘top’].set_color(‘none’) # 将右边 上边的两条边颜色设置为空 其实就相当于抹掉这两条边

ax.xaxis.set_ticks_position(‘bottom’)
ax.yaxis.set_ticks_position(‘left’) # 指定下边的边作为 x 轴 指定左边的边为 y 轴

ax.spines[‘bottom’].set_position((‘data’, 0)) #指定 data 设置的bottom(也就是指定的x轴)绑定到y轴的0这个点上
ax.spines[‘left’].set_position((‘data’, 0))

ax.set_xticks([-4,-2,0,2,4]) # 设置坐标轴的标签显示方式
ax.set_xticklabels([‘2017’,‘2018’,’ ',‘2020’,‘2021’],rotation=45,fontsize=‘small’)

#绘制图形的标题
plt.title(u’测试函数’, fontproperties=font,loc=‘left’) #ax.set_title(‘Demo Figure’)设置图名、字体及位置

plt.plot(x1, y1, linestyle=’–’,marker=‘o’)
for xy in zip(x1, y1): #在曲线上标注坐标点
plt.annotate("(%s,%s)" % xy, xy=xy, xytext=(-20, 10), textcoords=‘offset points’)

plt.plot(x, y2, linestyle=’-.’,marker=’.’)
plt.plot(x, y3, linestyle=’:’, marker=‘1’)

plt.show()

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: