美文网首页
matplotlib

matplotlib

作者: 九章9405 | 来源:发表于2020-06-10 11:17 被阅读0次

导入库

import matplotlib.pyplot as plt
import numpy as np
测试数据
x = [i for i in range(1,100)]
y1 = [np.log(i) for i in range(1,100)]
y2 = [-np.log(i) for i in range(1,100)]

设置字体

plt.rcParams['font.sans-serif'] = ['SimHei'] # 替换sans-serif字体
plt.rcParams['axes.unicode_minus'] = False   # 解决坐标轴负数的负号显示问题

新建图像

fig = plt.figure(figsize = (15,8))

在figure中新增2个坐标系

ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)

添加网格

ax1.grid(color='g',linestyle='--')

绘图

ax1.plot(x,y1,label='inline label')
ax1.plot(x,y2,label='inline labe2')

设置标题

ax1.set_title("测试")
#设置x轴标题
ax1.set_xlabel("X轴标题")
#设置y轴标题
ax1.set_ylabel("Y轴标题")

设置x轴范围

ax1.set_xlim([0,80])    
#设置y轴标题
ax1.set_ylim([0,6])

显示图例

#loc设置位置,ncol设置列数,title设置图例名称
ax1.legend(loc = 0, title='图例名称',ncol = 2)

保存图像

#保存图像,一定要在plt.show前面
plt.savefig('figpath.jpg') 

显示图像

plt.show()

上传图片至七牛

from qiniu import Auth, put_file
access_key = '**********************************'
secret_key = '**********************************'
bucket_name = '*******'
q = Auth(access_key, secret_key)
def get_url(localfile):
    key = localfile
    token = q.upload_token(bucket_name,key,3600)
    ret, info = put_file(token, key, localfile)
    url = r'http://********.com/'+key
    return url
url_jpg = get_url('figpath.jpg')

相关文章

网友评论

      本文标题:matplotlib

      本文链接:https://www.haomeiwen.com/subject/dymutktx.html