美文网首页
matplotlib双轴图及图例指南

matplotlib双轴图及图例指南

作者: 弦好想断 | 来源:发表于2020-08-22 12:25 被阅读0次

https://matplotlib.org/tutorials/intermediate/legend_guide.html

import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
import numpy as np
%matplotlib inline
plt.rcParams['font.sans-serif'] = ['SimHei']#显示中文
plt.style.use('ggplot')

# 生成数据
month = np.linspace(1, 12, 12)
dau = np.random.randint(200, 300, 12)
ctr = np.random.randint(8, 20, 12) / 100

# 画图
fig, ax1 = plt.subplots(figsize = (10, 5), facecolor='white')

# 左轴
ax1.bar(month, dau, color='g', alpha=0.5,label="日活(万)")
ax1.set_xlabel('月份')
ax1.set_ylabel('日活(万)')
ax1.legend(loc='upper left')
# 右轴
ax2 = ax1.twinx()
ax2.plot(month, ctr, '-or',label='点击率')
ax2.set_ylabel('点击率')
ax2.set_ylim(0, 0.2)
ax2.legend(loc='upper right')

# 将点击率坐标轴以百分比格式显示
def to_percent(temp, position):
    return '%2.1f'%(100*temp) + '%'
plt.gca().yaxis.set_major_formatter(FuncFormatter(to_percent))

# 标题
plt.title('2017年XXX日活及点击率趋势')
plt.show()

https://www.jianshu.com/p/83e171bb80f1

相关文章

  • matplotlib双轴图及图例指南

    https://matplotlib.org/tutorials/intermediate/legend_guid...

  • Python画图&读写csv

    设置字体 设置图例 设置坐标轴的范围和刻度 设置坐标轴的精度 Python读写csv文件 matplotlib在图...

  • 目录

    1.1 Matplotlib 基础2.1 figure 图像2.2 设置坐标轴2.3 Legend 图例2.4 A...

  • Matplotlib之基本点线作图

    在Jupyter Notebook下实现各种Matplotlib用法。 基本作图 坐标轴操作 图例操作 注释操作 其它

  • 我的一生模型构建

    一、知识点 1.matplotlib图例legend语法及设置 loc 设置图例位置 其他用法参考 https:/...

  • matplotlib制作双Y轴图

    import numpy as npimport matplotlib.pyplot as plt fig = p...

  • 常用的python可视化库

    Matplotlib:最经典的Python绘图库,没有之一优点:支持众多图表,API丰富,标题、图例、坐标轴标签、...

  • matplotlib可视化篇legend()--图例

    matplotlib.pyplot.legend()用来显示图例,legend基础语法及用法,官方文档 常用参数列...

  • 几个特别的图表的实现的option

    1. Y轴缺省,x轴分割线缺省,如图 2. 图例文字跟图例颜色同色,如图 3. 横向柱状图,缺省坐标轴,柱子有背景...

  • matplotlib

    1. 在pyqt界面创建matplotlib 图 2. 对matplotlib 图进行优化 去除坐标轴 缩短画布与...

网友评论

      本文标题:matplotlib双轴图及图例指南

      本文链接:https://www.haomeiwen.com/subject/jvkvjktx.html