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

本文详细介绍了如何使用Python的matplotlib库从Excel表格中读取数据,制作单坐标轴折线图,并演示了如何创建双坐标轴图表,包括设置坐标轴范围、标签、图例和保存图片。适合初学者和数据分析师快速上手数据可视化。
摘要由CSDN通过智能技术生成

从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)

在这里插入图片描述

  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值