《利用python进行数据分析》学习笔记第八章

# 绘图和可视化 matplotlib API入门
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
plt.plot(np.arange(10))
plt.show()
![png](output_3_0.png) ## FIgures 和 Subplot
fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
from numpy.random import randn
plt.plot(randn(50).cumsum(), 'k--')
_ = ax1.hist(randn(100), bins = 20, color = 'k', alpha = 0.3)
ax2.scatter(np.arange(30), np.arange(30) + 3*randn(30))
plt.show()
![png](output_6_0.png)
fig, axes = plt.subplots(2, 3)
axes[0, 1].hist(randn(500), bins = 50, color = 'k', alpha = 0.5)
plt.
plt.show()
![png](output_7_0.png)
fig, axes = plt.subplots(2, 2, sharex = True, sharey = True)
for i in range(2):
    for j in range(2):
        axes[i, j].hist(randn(500), bins = 50, color = 'b', alpha = 0.5)
plt.subplots_adjust(wspace = 0, hspace = 0)
plt.show()
![png](output_8_0.png)
help(cumsum)
Help on function cumsum in module numpy.core.fromnumeric: cumsum(a, axis=None, dtype=None, out=None) Return the cumulative sum of the elements along a given axis. Parameters ———- a : array_like Input array. axis : int, optional Axis along which the cumulative sum is computed. The default (None) is to compute the cumsum over the flattened array. dtype : dtype, optional Type of the returned array and of the accumulator in which the elements are summed. If `dtype` is not specified, it defaults to the dtype of `a`, unless `a` has an integer dtype with a precision less than that of the default platform integer. In that case, the default platform integer is used. out : ndarray, optional Alternative output array in which to place the result. It must have the same shape and buffer length as the expected output but the type will be cast if necessary. See `doc.ufuncs` (Section “Output arguments”) for more details. Returns ——- cumsum_along_axis : ndarray. A new array holding the result is returned unless `out` is specified, in which case a reference to `out` is returned. The result has the same size as `a`, and the same shape as `a` if `axis` is not None or `a` is a 1-d array. See Also ——– sum : Sum array elements. trapz : Integration of array values using the composite trapezoidal rule. diff : Calculate the n-th discrete difference along given axis. Notes —– Arithmetic is modular when using integer types, and no error is raised on overflow. Examples ——– >>> a = np.array([[1,2,3], [4,5,6]]) >>> a array([[1, 2, 3], [4, 5, 6]]) >>> np.cumsum(a) array([ 1, 3, 6, 10, 15, 21]) >>> np.cumsum(a, dtype=float) # specifies type of output value(s) array([ 1., 3., 6., 10., 15., 21.]) >>> np.cumsum(a,axis=0) # sum over rows for each of the 3 columns array([[1, 2, 3], [5, 7, 9]]) >>> np.cumsum(a,axis=1) # sum over columns for each of the 2 rows array([[ 1, 3, 6], [ 4, 9, 15]]) 颜色、标记和线型
plt.plot(randn(30).cumsum(), 'ko--')
plt.show()
![png](output_11_0.png) ## 关于plot的笔记 首先,终于搞明白了,原来plt.plot()是matplotlib.pyplot里的绘图函数,同时 pandas里dataFrame也有plot方法,只是里面参数不太一样 1, Figure 和 Subplot matplot里绘图图像都位于Figure对象中,利用plt.figure创建一个新的Figure: fig = plt.figure() ax1 = fig.add_subplot(2,2,1) ax2 = fig.add_subplot(2,2,2) ax3 = fig.add_subplot(2,2,3) from numpy.random import randn plt.plot(randn(50).cumsum(), ‘k–’) _ = ax1.hist(randn(100), bins = 20, color = ‘k’, alpha = 0.3) ax2.scatter(np.arange(30), np.arange(30) + 3*randn(30)) plt.show() 这是绘制各种图的不同的写法,效果在上面,关于各种其他线型等其他设置都有详细用法 2, pandas中的绘图函数用的是plot方法 柱状图
fig, axes = plt.subplots(2, 1)
data = Series(np.random.rand(16), index = list('abcdefghijklmnop'))
data
a 0.515924 b 0.975604 c 0.114507 d 0.718997 e 0.614152 f 0.667405 g 0.915805 h 0.116472 i 0.265011 j 0.575984 k 0.025078 l 0.145539 m 0.636154 n 0.484365 o 0.123298 p 0.944101 dtype: float64
data.plot(kind = 'bar', ax = axes[0], color = 'k', alpha = 0.7)
data.plot(kind = 'line', ax = axes[1], color = 'k', alpha = 0.7)
plt.show()
![png](output_17_0.png) 对于dataFrame
df = DataFrame(np.random.rand(6, 4), index = ['one', 'two', 'three', 'four', 'five', 'six'], columns = pd.Index(['A', 'B', 'C', 'D'], name = 'Genus'))
df
.dataframe thead tr:only-child th { text-align: right; } .dataframe thead th { text-align: left; } .dataframe tbody tr th { vertical-align: top; }
GenusABCD
one0.7012300.3351900.2563380.009374
two0.9713420.1213020.7972510.548977
three0.9368280.2837680.6759050.460997
four0.9722390.9468900.8876080.767606
five0.0914380.1924460.6185080.349543
six0.4856260.1795090.8812060.753848
df.plot(kind = 'barh', stacked = True, alpha = 0.5)
plt.show()

png

其他各种绘图的方法就不列举了,以后用到好好学

figure、 show 和subplot、 plot

help(plt.figure)
Help on function figure in module matplotlib.pyplot:

figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True, FigureClass=<class 'matplotlib.figure.Figure'>, **kwargs)
    Creates a new figure.

    Parameters
    ----------

    num : integer or string, optional, default: none
        If not provided, a new figure will be created, and the figure number
        will be incremented. The figure objects holds this number in a `number`
        attribute.
        If num is provided, and a figure with this id already exists, make
        it active, and returns a reference to it. If this figure does not
        exists, create it and returns it.
        If num is a string, the window title will be set to this figure's
        `num`.

    figsize : tuple of integers, optional, default: None
        width, height in inches. If not provided, defaults to rc
        figure.figsize.

    dpi : integer, optional, default: None
        resolution of the figure. If not provided, defaults to rc figure.dpi.

    facecolor :
        the background color. If not provided, defaults to rc figure.facecolor

    edgecolor :
        the border color. If not provided, defaults to rc figure.edgecolor

    Returns
    -------
    figure : Figure
        The Figure instance returned will also be passed to new_figure_manager
        in the backends, which allows to hook custom Figure classes into the
        pylab interface. Additional kwargs will be passed to the figure init
        function.

    Notes
    -----
    If you are creating many figures, make sure you explicitly call "close"
    on the figures you are not using, because this will enable pylab
    to properly clean up the memory.

    rcParams defines the default values, which can be modified in the
    matplotlibrc file
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值