Python画图基本方法总结

import pandas as pd

import numpy as np


frommatplotlib.ticker import MultipleLocator, FormatStrFormatter
import matplotlib as mpl
# matplotlib.use('Agg')
import matplotlib.pyplot as plt  


1.设置画布大小

   plt.figure(figsize=(19, 12))    #设置画布尺寸大小,会影响自动弹出来图框的大小
    ax = plt.subplot(1, 1, 1)           # 画子图

2. 取不同颜色个数
    col_num = df.shape[1]
    colormap = plt.cm.gist_ncar   # 颜色轴连续的,python自带的字母颜色只有7中,有时不够用

    colors = [colormap(i) for i in np.linspace(0, 0.9, col_num)]

3.画点图,折线图,label会对应legend
    for i in range(1, df.shape[1]):
        plt.plot(df.iloc[:, 0], df_total_guodu_3_relative30up.iloc[:, i], color=colors[i],
                 linestyle='-', linewidth=6, label='%s' % df.columns[i])

4.显示中文时指定路径

  用fc-list : lang=zh在终端里查询包含哪些字体

    myfont = mpl.font_manager.FontProperties(fname='/usr/share/fonts/truetype/arphic/ukai.ttc')   #指定中文字体路径
    myfont1 = mpl.font_manager.FontProperties(fname='/usr/share/fonts/opentype/noto/NotoSansCJK.ttc',size=20)

   ax.legend(legend, loc='best', bbox_to_anchor=(1.03, -0.04),ncol=8, frameon=False,prop=myfont1

   # 不能单独设置字体大小,fontsize,prop不能同时用,如果只显示英文数字,不需要指定字体,则可直接用fontsize

5.设置标签是横向摆放,定义函数flip

 ----------------------------------------------------------------------------------------------------------  

import itertools

 def flip(items, ncol):
     return itertools.chain(*[items[i::ncol] for i in range(ncol)])

-------------------------------------------------------------------------------------------------------

   handles, labels = ax.get_legend_handles_labels()   
    plt.legend(flip(handles, 8), flip(labels, 8), loc='best', fontsize=17, bbox_to_anchor=(0.9, -0.02), ncol=8,
               frameon=False)

6.标题
    plt.title(u'过渡', fontproperties=myfont, fontsize=25)

7.轴的各种设置
    xmajorLocator = MultipleLocator(1)                     # 将x主刻度标签设置为1的倍数
    ax.xaxis.set_major_locator(xmajorLocator)

    ymajorFormatter = FormatStrFormatter('%.2f')     # 设置y轴标签文本的格式两位小数(‘%.2f%%’)百分号格式
    ax.yaxis.set_major_formatter(ymajorFormatter)


  ticklab = ax.yaxis.get_ticklabels()[0]                          #设置y轴标签的位置
  trans = ticklab.get_transform()
  ax.yaxis.set_label_coords(-0.06, 6000, transform=trans)


    ax.set_ylim([0, 5])  #设置y轴取值范围
    ax.set_xlim([0, 11])  # 设置x轴取值范围

    for tick in ax.xaxis.get_major_ticks():  # 设置x轴刻度文本的大小
        tick.label1.set_fontsize(18)

    for tick in ax.yaxis.get_major_ticks():  # 设置y轴刻度文本的大小
        tick.label1.set_fontsize(18)

    ax.yaxis.grid(True)  # y坐标轴的网格

8.保存图片

    plt.savefig('./total.jpg', format='jpg') # 保存图片


9.累计柱状图

# 每个比例柱形图的个数,方便后面画个数
N = df[1]-1
left = np.arange(N)
width = 0.55

height = []

height.append(df[0, 1:3]/1000000)
plt.bar(left, height[0], width,  facecolor = colors[0], edgecolor = 'white', align='center')

for i in range(1, df[0]):
    height.append(df_total_owing.iloc[i, 1:3]/1000000)
    plt.bar(left, height


10. 设置横轴标签是其中文,日期,并旋转角度

N = df.shape[0]                       #设置x轴刻度标签
left = np.arange(N)
x_ticks = df.iloc[:, 0]          # 设置x轴的刻度值并旋转,文本刻度值太长,放在后面不管用
plt.xticks(left, x_ticks, rotation=90)          # left是每个刻度开始的位置



  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值