美文网首页生物信息学生物信息学与算法生物信息学习
matplotlib绘图基础(2)—— 基础图形

matplotlib绘图基础(2)—— 基础图形

作者: WooWoods | 来源:发表于2019-09-28 19:07 被阅读0次

    histogram

    np.random.seed(19680801)
    
    # example data
    mu = 100  # mean of distribution
    sigma = 15  # standard deviation of distribution
    x = mu + sigma * np.random.randn(437)
    
    num_bins = 50
    
    fig, ax = plt.subplots()
    
    # the histogram of the data
    n, bins, patches = ax.hist(x, num_bins, density=1)
    
    # add a 'best fit' line
    y = ((1 / (np.sqrt(2 * np.pi) * sigma)) *
         np.exp(-0.5 * (1 / sigma * (bins - mu))**2))
    ax.plot(bins, y, '--')
    ax.set_xlabel('Smarts')
    ax.set_ylabel('Probability density')
    ax.set_title(r'Histogram of IQ: $\mu=100$, $\sigma=15$')
    
    # Tweak spacing to prevent clipping of ylabel
    fig.tight_layout()
    plt.show()
    

    piechart

    # Pie chart, where the slices will be ordered and plotted counter-clockwise:
    labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
    sizes = [15, 30, 45, 10]
    explode = (0, 0.1, 0, 0)  # only "explode" the 2nd slice (i.e. 'Hogs')
    
    fig1, ax1 = plt.subplots()
    ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
            shadow=True, startangle=90)
    ax1.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.
    
    plt.show()
    
    image.png

    scatter

    with cbook.get_sample_data('goog.npz') as datafile:
        price_data = np.load(datafile)['price_data'].view(np.recarray)
    price_data = price_data[-250:]  # get the most recent 250 trading days
    
    delta1 = np.diff(price_data.adj_close) / price_data.adj_close[:-1]
    
    # Marker size in units of points^2
    volume = (15 * price_data.volume[:-2] / price_data.volume[0])**2
    close = 0.003 * price_data.close[:-2] / 0.003 * price_data.open[:-2]
    
    fig, ax = plt.subplots()
    ax.scatter(delta1[:-1], delta1[1:], c=close, s=volume, alpha=0.5)
    
    ax.set_xlabel(r'$\Delta_i$', fontsize=15)
    ax.set_ylabel(r'$\Delta_{i+1}$', fontsize=15)
    ax.set_title('Volume and percent change')
    
    ax.grid(True)
    fig.tight_layout()
    
    plt.show()
    
    image.png

    date

    years = mdates.YearLocator()   # every year
    months = mdates.MonthLocator()  # every month
    years_fmt = mdates.DateFormatter('%Y')
    
    # Load a numpy structured array from yahoo csv data with fields date, open,
    # close, volume, adj_close from the mpl-data/example directory.  This array
    # stores the date as an np.datetime64 with a day unit ('D') in the 'date'
    # column.
    with cbook.get_sample_data('goog.npz') as datafile:
        data = np.load(datafile)['price_data']
    
    fig, ax = plt.subplots()
    ax.plot('date', 'adj_close', data=data)
    
    # format the ticks
    ax.xaxis.set_major_locator(years)
    ax.xaxis.set_major_formatter(years_fmt)
    ax.xaxis.set_minor_locator(months)
    
    # round to nearest years.
    datemin = np.datetime64(data['date'][0], 'Y')
    datemax = np.datetime64(data['date'][-1], 'Y') + np.timedelta64(1, 'Y')
    ax.set_xlim(datemin, datemax)
    
    # format the coords message box
    ax.format_xdata = mdates.DateFormatter('%Y-%m-%d')
    ax.format_ydata = lambda x: '$%1.2f' % x  # format the price.
    ax.grid(True)
    
    # rotates and right aligns the x labels, and moves the bottom of the
    # axes up to make room for them
    fig.autofmt_xdate()
    
    plt.show()
    
    image.png

    相关文章

      网友评论

        本文标题:matplotlib绘图基础(2)—— 基础图形

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