matplotlib

一、绘图

1.1 绘制散点图
s:表示散点的大小
1.2 绘制柱状图

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
np.random.seed(123)
fig = plt.figure(figsize=(14,5))
font = {
        'size' :15.5,
        'family':'arial',
        'family':'SimSun',
        }
matplotlib.rc('font', **font)

time_sep = [0,11,8,10,10]
ax1 = fig.add_subplot(1,1,1)
#ax1.bar(time_sep,height)
colors=['indigo','lime','red','fuchsia','blue','cyan','yellow','black']
labels=["-1953","1953-1964","1964-1982","1982-1990","1990-2000","2000-2010"]

for i in range(len(time_sep)):
    ax1.bar(i,width=0.15,height=time_sep[i],label=labels[i],color=colors[i])
    
ax1.legend(loc='upper left', fontsize=9.5,ncol=2)
ax1.set_xlabel('The count of index')
ax1.set_ylabel('Value')

柱状图打标签数值

def autolabel(rects, hs):
    for rect in rects:
        height = rect.get_height()
        plt.text(rect.get_x()+rect.get_width()/2.-0.11, hs, '%s' % round(height,2), size=10, family="Times new roman")
        
a = ax1.bar(i, width=widths,height=time_sepa[i],label=labels[i],color="turquoise")
b = ax2.bar(i+widths, width=widths,height=time_sepb[i],label=labels[i+1],color="cyan")
#h1可以自己设定
autolabel(a,time_sepa[i]*h1)
字体

设置中文字体

'family':'SimHei'
#解决显示中文的问题
plt.rcParams['font.sans-serif'] = ['SimHei']
图上添加文字
ax1.text(5,5.7,"休息时间",weight="bold",color="green")
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
np.random.seed(123)
fig = plt.figure(figsize=(14,8))
font = {
        'size' :15.5,
        'family':'SimHei'
        }
matplotlib.rc('font', **font)

time_sep = [0,11,8,10,10]
ax1 = fig.add_subplot(1,1,1)
#ax1.bar(time_sep,height)
colors=['indigo','lime','red','fuchsia','blue','cyan','yellow','black']
labels=["-1953","1953-1964","1964-1982","1982-1990","1990-2000","2000-2010"]

    
#ax1.legend(loc='upper left', fontsize=9.5,ncol=2)
ax1.set_title("派车单时空流程示意图")
#ax1.set_ylim(0,100)


ax1.plot([5]*6,[1,2,3,4,5,6],linestyle='dashed',color=colors[2])
ax1.plot([6]*6,[1,2,3,4,5,6],linestyle='dashed',color=colors[2])
ax1.text(5,5.7,"休息时间",weight="bold",color="green")
ax1.set_xticks([1,2,3,4,5,6,7,8,9,10,11,12,13])
ax1.set_yticks([1,2,3,4,5,6])
ax1.set_xticklabels(['8:00','9:00','10:00','11:00', '12:00', '13:00', '14:00', '15:00','16:00','17:00','18:00', '19:00', ''])
ax1.set_yticklabels(['集结点', '仓库5', '仓库4', 
                     '仓库3', '仓库2', '仓库1'])
调整子图之间的间距
fig.subplots_adjust(left=0.0,bottom=0.0,top=1,right=1)
时间

tdata[“净值日期”] 是datafram中的一个值

index = [datetime.datetime.strptime(tdata["净值日期"][i], '%Y-%m-%d') for i in range(tdata.shape[0])]

修改坐标轴

ax1.set_xticks([1,2,3,4,5,6,7,8,9,10,11,12,13])
ax1.set_yticks([1,2,3,4,5,6])
ax1.set_xticklabels(['8:00','9:00','10:00','11:00', '12:00', '13:00', '14:00', '15:00','16:00','17:00','18:00', '19:00', ''])
ax1.set_yticklabels(['集结点', '仓库5', '仓库4', 
                     '仓库3', '仓库2', '仓库1'])
只保留日期

tdata[“dt”].dt.date

坐标轴角度旋转
ax1.set_xticklabels(["2.20 18:00","2.20 19:00","2.20 20:00","2.20 21:00","2.20 22:00","2.20 23:00","2.21 0:00","2.20 1:00","2.20 2:00","2.20 3:00","2.20 4:00","2.20 5:00"],rotation=30)
绘柱状图
参数
normed:是否直方图归一化,默认为False,如果设为True,将会调整纵坐标的值,使得直方图中每个长方形块的面积之和为1。
图片保存
fig.tight_layout(pad=0,w_pad=1)
fig.savefig("411.png", format='png', dpi=600)
图片显示不全解决方案
坐标轴改为内向
plt.rcParams['xtick.direction'] = 'in'
plt.rcParams['ytick.direction'] = 'in'
解决负号显示方块问题
# 解决负号'-'显示为方块的问题
plt.rcParams['axes.unicode_minus'] = False
sns的结合

sns.distplot 是直方图和核密度图(sns.kdeplot)的结合。用来看单个连续型变量的分布。

ax2 = fig.add_subplot(1,2,2)
sns.distplot(abs(data_test["name"].dropna()), ax=ax2, color="#099DD9",kde_kws={"color":"b", "lw" :0.45 })

二、颜色与标签

参考资料

https://blog.youkuaiyun.com/Sarahing/article/details/126120163 (线性和颜色样式)
https://blog.youkuaiyun.com/qq_40481843/article/details/106231257/(绘图标签和颜色)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值