python 03,matplotlib高级

import numpy as np
import matplotlib .pyplot as plt
import pandas as pd
from pandas import Series,DataFrame
# 处理中文正常显示
plt.rcParams['font.sans-serif'] = ['FangSong']
# 处理负号显示
plt.rcParams['axes.unicode_minus']=False

柱状图

# 取出一张纸
plt.figure(1)
# plt.subplot( x,y,z)  x行y列的z号位置
ax1=plt.subplot(1,1,1)
y_data=[15,20,18,25]
x_data=np.arange(4)
# 画图返回四个柱子
rect=ax1.bar(x_data,y_data,width=0.5,color='#93ccd3')for each in rect:
#     获取自身的左下角坐标
    x=each.get_x()
#     获取y轴值
    height=each.get_height()
    '''
        ax.text(x,y,str) 写一个文本
        x,y默认为0
    '''
    ax1.text(x+0.15,height+0.3,f'{height}W')
​
ax1.set_xticks(x_data)
ax1.set_xticklabels([f'第{each}季度' for each in list('一二三四')])
​
ax1.set_ylim(0,30)
# 网格显示
ax1.grid()
​
ax1.set_ylabel('销量(单位:万件)')
ax1.set_title('xxx2020年销售季度统计')
plt.show()

在这里插入图片描述

一个画布多个图

plt.figure(figsize=(12,8))
​
ax2=plt.subplot(2,2,1)
ax2.plot([1,2,3],[1,2,3])
​
ax3=plt.subplot(2,2,2)
ax3.bar([1,2,3],[1,2,3])
​
ax4=plt.subplot(2,2,3)
ax4.pie([1,2,3])
​
ax5=plt.subplot(2,2,4)
ax5.scatter([1,2,3],[1,2,3])

<matplotlib.collections.PathCollection at 0x207d15e90b8>
在这里插入图片描述

plt.figure(2)
​
plt.subplot(1,2,1)
plt.plot([1,2,3],[2,1,3])
​
plt.subplot(1,2,2)
plt.bar([1,2,3],[2,1,3])

<BarContainer object of 3 artists>
在这里插入图片描述

pandas 对象 直接使用画图函数

s=Series(np.random.randn(10))
s.plot()

AxesSubplot:
在这里插入图片描述

s.plot(kind='bar')

AxesSubplot:
在这里插入图片描述

s.plot(kind='barh')

AxesSubplot:
在这里插入图片描述

s2=Series(np.random.randn(10),index=list('abcdefghij'))
s2.plot()

AxesSubplot:
在这里插入图片描述

Series([1,2,3,4],index=list('abcd')).plot(kind='pie')

AxesSubplot:ylabel=‘None’
在这里插入图片描述

df=DataFrame(np.random.randint(1,10,(4,5)),
            index=list('abcd'),
            columns=list('一二三四五') 
            )
df

一 二 三 四 五
a 6 6 9 7 8
b 4 9 7 1 1
c 9 8 9 5 3
d 9 5 5 8 2

df.plot(kind='line')
plt.show()

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值