zoukankan      html  css  js  c++  java
  • python画图

    包含头文件:

    import numpy as np

    import matplotlib.pyplot as plt

    直接将文件中的二维数组读进来:

    data = np.loadtxt('data.txt')

    画图:

    plt.plot(data[:,0], data[:,1], 'ro')

    plt.show()

    其中r为红色,o为形状. 默认是b-.

    颜色表:

    Allias Color
    'b' blue
    'g' green
    'r' red
    'c' cyan
    'm' magenta
    'y' yellow
    'k' black
    'w' white


    颜色也可以用参数color='r'或者c='r'指定.

    颜色的表示可以是'#ff0000', 或者(1,0,0) 或者red.

    线条样式表:

    . , o v ^ < > 1 2 3 4 s(正方形) p(五边形) * h(六边形1) H(六边形2) + x D(菱形) d(瘦菱形) | _

    TICKLEFT TICKRIGHT TICKUP TICKDOWN CARETLEFT CARETRIGHT CARETUP CARETDOWN None

    plot参数:

    aa 抗锯齿

    c 颜色

    ls 线条风格

    lw 线条宽度

    mec

    mfc

    ms

    mew

    直方图:

    plt.hist([1, 2, 3, 2, 3, 4, 3])

    如果不想要黑色边框,可以在参数中增加histtype='stepfilled'.

    原型:

    hist(x, bins=10, range=None, normed=False, cumulative=False,
    bottom=None, histtype='bar', align='mid',
    orientation='vertical', rwidth=None, log=False, **kwargs)

    网格:

    plt.grid()

    标题:

    plt.title('分布图')

    要显示中文,必须from matplotlib.font_manager import FontProperties,

    再font = FontProperties(fname=r'c:windowsfontsSimsun.ttc',size=14),

    最后plt.title(u'分布图', fontproperties=font)

    坐标轴:

    标注坐标轴:

    plt.xlabel('时间')

    plt.ylabel('距离')

    支持Latex,例如xlabel(r"$frac{x^2}{y^4}$"),只需在字符串前加r即可.

    设置坐标轴范围:

    plt.xlim(0.0, 10.)

    plt.axis([-1, 1, 0, 10])

    对数坐标轴:

    ax.semilogx(x,y) #x轴为对数坐标轴
    ax.semilogy(x,y) #y轴为对数坐标轴
    ax.loglog(x,y) #双对数坐标轴

  • 相关阅读:
    剑指Offer-用两个栈实现队列
    剑指Offer-从尾到头打印链表
    滑动门技术实现
    解决keil5中文注释乱码方法
    faker数据填充详解
    redis在微博与微信等互联网应用笔记
    JDBC插入中文数据出现?号地解决问题
    idea配置less自动编译
    idea使用Vue的v-bind,v-on报错
    idea 代码部分格式化
  • 原文地址:https://www.cnblogs.com/saieuler/p/3237220.html
Copyright © 2011-2022 走看看