Python 20190209 绘制方程曲线、设置标签、中文等示例
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()
- Python使用matplotlib绘制正弦和余弦曲线的方法示例
- Python使用matplotlib模块绘制图像并设置标题与坐标轴等信息示例
- Python绘制温度变化曲线
- Python2.7 中文字符编码 & Pycharm utf-8设置、Unicode与utf-8的区别
- Linux ubuntu下python处理中文(汉字及标点)的编码设置
- python matplotlib 绘制双Y轴曲线图
- Python and Matlab绘制曲线图
- python matplotlib 中文显示参数设置
- python读取文本绘制动态速度曲线
- Python实现读取txt文件中的数据并绘制出图形操作示例
- python matplotlib 中文字体设置
- Python实现中文数字转换为阿拉伯数字的方法示例
- java正则匹配HTML中a标签里的中文字符示例
- Python的Django框架中自定义模版标签的示例
- Python基于最小二乘法实现曲线拟合示例
- Android UI设计系列之HTML标签实现TextView设置中文字体加粗效果(6)
- Python使用matplotlib绘制三维图形示例
- 【python图像处理】直线和曲线的拟合与绘制(curve_fit()详解)
- python 连接数据库-设置oracle ,mysql 中文字符问题
- 使用python绘制人人网好友关系图示例