matplotlib笔记


使用中持续更新…

1 指定位置进行文本标记

指定位置进行文本标记
法一:plt.text( )

fig = plt.figure(figsize=(5,4), dpi=80)
ax = fig.add_subplot(1,1,1)
plt.subplot(1, 1, 1)
plt.bar(data_all1_liucun.iloc[:, 0] - 0.006, data_all1_liucun.iloc[:, 4]/10000, color='lightblue', width = 0.012, label='XXXXXXXXX')
plt.bar(data_all2_liucun.iloc[:, 0] + 0.006, data_all2_liucun.iloc[:, 4]/10000, color='bisque', width = 0.012, label='YYYYYYYYYY')

ax.set_xlabel('XXXXXXX')
ax.set_ylabel('XXXXXXXX')
ax.legend(loc='upper left', framealpha = 0.4)
ax.xaxis.set_major_locator(MultipleLocator(0.1))
ax.xaxis.set_major_formatter(ticker.PercentFormatter(xmax=1, decimals=0))

# 次坐标
ax2 = ax.twinx()
ax2.set_ylabel('XXXXXXX')    

plt.plot(data_all1_liucun.iloc[:, 0], data_all1_liucun.iloc[:, 3], label='XXXXXXXXXX', marker='.')
plt.plot(data_all2_liucun.iloc[:, 0], data_all2_liucun.iloc[:, 3], label='XXXXXXXXXXXXx', marker='.')
# 辅助竖直虚线
plt.vlines(0.7, 0, data_all2_liucun.iloc[14, 3], colors='lightgrey', linestyle='--')
plt.vlines(0.8, 0, data_all2_liucun.iloc[16, 3], colors='lightgrey', linestyle='--')
# 指定位置文本标记坐标
plt.text(0.7,data_all2_liucun.iloc[14, 3],('{:.0%}'.format(0.7),'{:.1%}'.format(data_all2_liucun.iloc[14, 3])), verticalalignment="baseline", horizontalalignment="right")
plt.text(0.8,data_all2_liucun.iloc[16, 3],('{:.0%}'.format(0.8),'{:.1%}'.format(data_all2_liucun.iloc[16, 3])), verticalalignment="bottom", horizontalalignment="right")
plt.text(0.7,data_all1_liucun.iloc[7, 3],('{:.0%}'.format(0.7),'{:.1%}'.format(data_all1_liucun.iloc[7, 3])), verticalalignment="top", horizontalalignment="left")
plt.text(0.8,data_all1_liucun.iloc[8, 3],('{:.0%}'.format(0.8),'{:.1%}'.format(data_all1_liucun.iloc[8, 3])), verticalalignment="center_baseline", horizontalalignment="right")

plt.title('XXXXXXX')
plt.ylim((0, 1))

ax2.yaxis.set_major_locator(MultipleLocator(0.1))
ax2.yaxis.set_major_formatter(ticker.PercentFormatter(xmax=1, decimals=0))
ax2.legend(loc='upper left', framealpha = 0.4)

法一结果:
在这里插入图片描述
法二:plt.annotatet( )

# 替换法一中标记部分
plt.annotate(('{:.0%}'.format(0.7),'{:.1%}'.format(data_all2_liucun.iloc[14, 3])), xy=(0.7, data_all2_liucun.iloc[14, 3]), xytext=(0.5, data_all2_liucun.iloc[14, 3]+0.05), arrowprops=dict(arrowstyle='->',connectionstyle='arc3',color='lightgrey')) 
plt.annotate(('{:.0%}'.format(0.8),'{:.1%}'.format(data_all2_liucun.iloc[16, 3])), xy=(0.8, data_all2_liucun.iloc[16, 3]), xytext=(0.6, data_all2_liucun.iloc[16, 3]+0.1), arrowprops=dict(arrowstyle='->',connectionstyle='arc3',color='lightgrey'))

plt.annotate(('{:.0%}'.format(0.7),'{:.1%}'.format(data_all1_liucun.iloc[7, 3])), xy=(0.7, data_all1_liucun.iloc[7, 3]), xytext=(0.5, data_all1_liucun.iloc[7, 3]-0.2), arrowprops=dict(arrowstyle='->',connectionstyle='arc3',color='lightgrey')) 
plt.annotate(('{:.0%}'.format(0.8),'{:.1%}'.format(data_all1_liucun.iloc[8, 3])), xy=(0.8, data_all1_liucun.iloc[8, 3]), xytext=(0.7, data_all1_liucun.iloc[8, 3]-0.15), arrowprops=dict(arrowstyle='->',connectionstyle='arc3',color='lightgrey'))

法二效果:
在这里插入图片描述

2 坐标轴显示问题

#指定默认字体-windows
matplotlib.rcParams['font.family']='sans-serif'
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
#解决负号'-'显示为方块的问题
matplotlib.rcParams['axes.unicode_minus'] = False
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值