25, python draw graphics

Basic idea: use python motolib library to draw graphics

 # 折线图


# encoding=utf-8
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import FuncFormatter

from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']

index_name= ['未','主','核','信']
index=np.arange(4)
plt.rcParams.update({'font.size': 8})
y =  np.array([43.71, 93.3,92.1,94.7])
plt.plot(index, y)

def to_percent(temp, position):
    return '%1.0f'%(1*temp) + '%'
plt.gca().yaxis.set_major_formatter(FuncFormatter(to_percent))
#plt.gca().xaxis.set_major_formatter(FuncFormatter(to_percent))
plt.xlabel(u'核',fontproperties='SimHei')
plt.xticks(index,index_name)
plt.ylabel(u'百',fontproperties='SimHei')#也可以直接显示中文。
plt.title('召')
plt.savefig("fi2lename.png")
plt.show()

from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']

index_name= ['未','主','核','信']

index=np.arange(4)
plt.rcParams.update({'font.size': 8})
GDP=[43.71, 93.3,92.1,94.7]
# 绘图
plt.bar(range(4), GDP, align = 'center',color='steelblue', alpha = 0.8)
# 添加轴标签
plt.ylabel('百')
# 添加标题
plt.title('召')
# 添加刻度标签
plt.xticks(range(4),['未','主','核','信'])
# 设置Y轴的刻度范围
def to_percent(temp, position):
    return '%1.0f'%(1*temp) + '%'
plt.gca().yaxis.set_major_formatter(FuncFormatter(to_percent))
# 为每个条形图添加数值标签    enumerate可以同时获得索引和值
for x,y in enumerate(GDP):
   plt.text(x,y+100,'%s' %round(y,1),ha='center')
# 显示图形
plt.savefig("filename.png")
plt.show()

 

import  pandas  as pd
from pylab import *

import os
mpl.rcParams['font.sans-serif'] = ['SimHei']

for root,dirs,files in os.walk("./"):
    for file in files:
        path_file=os.path.join(root,file)
        list_ug=[]
        list_gf=[]
        if file.endswith(".xlsx"):
            df=pd.read_excel(path_file,index_col=False)
            train_data = np.array(df)  # np.ndarray()
            train_x_list = train_data.tolist()  # list
            print(train_x_list)
            for item in train_x_list:
                list_ug.append(item[0])
                list_gf.append(item[-1])

            fig, ax_f = plt.subplots()
            # 这步是关键
            ax_d = ax_f.twiny()
            ax_f.set_xlim(0, 10)
            ax_f.set_ylabel(u'基', fontproperties='SimHei', color='k')
            ax_f.set_xlabel(u'黄', fontproperties='SimHei', color='k')

            plt.xlabel(u'量/(ug/mL)', fontproperties='SimHei', color='k')
            legends = {'黄': '^', 'VC': 's'}
            plt.tick_params(top='on')


            x= list_ug
            y =  np.array(list_gf)
            plt.plot(x, y, lw=1, c='k', marker='^',  label='Y1')  # 绘制y1
            y2 =  np.array([5.1, 8,9,9,9])
            plt.plot(x, y2, lw=1, c='k', marker='s', label='Y2')  # 绘制y2

            plt.legend(legends.keys(),loc=2)

            plt.savefig("filename.png")
            plt.show()

 

Guess you like

Origin blog.csdn.net/sxj731533730/article/details/105882377