ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python作折现图 双坐标轴图、坐标轴单位位置改变、坐标轴刻度旋转、图例设置、设置中文宋体同时英文Times New Roman、设置图片清晰度并保存等作图细节例子

2022-03-18 22:33:35  阅读:182  来源: 互联网

标签:plt 20 temp time 宋体 坐标轴 设置 ax table


从xlxs表格中读取数据画图,本例子包含各种作折线图的细节。若有问题可留言。
表格中数据为三列n行如下图所示。
在这里插入1描述
1.单坐标轴例子

import xlrd
import matplotlib.pyplot as plt
from matplotlib.pyplot import plot,savefig
from pylab import *    
config = {
            "font.family": 'serif',
            "font.size": 15,
            "mathtext.fontset": 'stix',
            "font.serif": ['SimSun'],
         }
rcParams.update(config)
file_name = xlrd.open_workbook('C://Users//自定义路径//表格名称.xlsx')#得到文件
table =file_name.sheets()[0]#得到sheet
nrows = table.nrows         #总行数
ncols = table.ncols         #总列数
i = 0
time = []
y1 = []
y2 = []
while i < nrows:
    ctype_temp = table.cell(i, 1).ctype #得到数字列数据的格式
    time_temp = table.row_values(i)[0] #得到数字列数据
    y1_temp = table.row_values(i)[1]
    y2_temp = table.row_values(i)[2]
    if(ctype_temp == 2):
        time.append(time_temp)
        y1.append(y1_temp)
        y2.append(y2_temp)    
    i=i+1
plt.figure(figsize=(20,10))
plt.xlim(0, 69)
plt.ylim(0.5,3.5)
x_major_locator=MultipleLocator(2)
plt.ylabel(r'标题',labelpad=-15,size=20,position=(-10,1.02),rotation=0)
plt.xticks(rotation=50)
plt.xlabel(r'标题',labelpad=-37,size=20,position=(1.02,-20),rotation=0)
plt.xticks(fontfamily="Times New Roman")
plt.yticks(fontfamily="Times New Roman")
ax=plt.gca()
ax.xaxis.set_major_locator(x_major_locator)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
plt.tick_params(labelsize=20)
plt.plot(time,y1,label=r'标题',linewidth=3,color = 'blue')
plt.plot(time,y2,label=r'$\mathrm{English}$标题',linewidth=3,linestyle = '--',color = 'orange')
plt.legend(loc=2,prop={'size':20})
savefig('C://Users//自定义路径//'+'图片名称.png',dpi = 500)

生成图片如下所示。
在这里插入图片描述
2.双坐标轴例子

file_name = xlrd.open_workbook('C://自定义路径//表格名称.xlsx')#得到文件
table =file_name.sheets()[0]#得到sheet
nrows = table.nrows         #总行数
ncols = table.ncols         #总列数
i = 0
time = []
y1 = []
y2 = []
while i < nrows:
    ctype_temp = table.cell(i, 1).ctype #得到数字列数据的格式
    time_temp = table.row_values(i)[0] #得到数字列数据
    y1_temp = table.row_values(i)[1]
    y2_temp = table.row_values(i)[2]
    if(ctype_temp == 2):
        time.append(time_temp)
        y1.append(y1_temp)
        y2.append(y2_temp)    
    i=i+1
plt.figure(figsize=(20,10))
plt.xlim(0, 69)
plt.ylim(0,1500)
x_major_locator=MultipleLocator(2)
plt.ylabel(r'标题',labelpad=-23,size=20,position=(-6,1.01),rotation=0)
plt.xlabel(r'标题',labelpad=-37,size=20,position=(1.02,-20),rotation=0)
plt.xticks(rotation=50)
plt.xticks(fontfamily="Times New Roman")
plt.yticks(fontfamily="Times New Roman")
ax=plt.gca()
ax.xaxis.set_major_locator(x_major_locator)
ax.spines['top'].set_color('none')
plt.tick_params(labelsize=20)
ax=plt.gca()
ax.xaxis.set_major_locator(x_major_locator)
ax.spines['top'].set_color('none')
plt.plot(time,y1,label=r'$\mathrm{English}$标题',linewidth=3,color = 'blue')
l1 = plt.legend(loc=2,prop={'size':20})
ax2=ax.twinx()
ax2.plot(time,y2,label=r'$\mathrm{English}$标题',linewidth=3,linestyle = '--',color = 'orange')
ax2.set_ylabel(r'%',labelpad=-13,size=20,position=(0,1.047),rotation=0)
ax2.set_ylim([0.5,3])
ax2.tick_params(labelsize=20)
plt.yticks(fontfamily="Times New Roman")
ax=plt.gca()
ax.xaxis.set_major_locator(x_major_locator)
ax.spines['top'].set_color('none')
plt.legend(loc=1,prop={'size':20})

savefig('C://自定义路径//'+'图片名称.png',dpi = 500)

在这里插入图片描述

标签:plt,20,temp,time,宋体,坐标轴,设置,ax,table
来源: https://blog.csdn.net/weixin_44720128/article/details/123585844

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有