# 绘图和可视化 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()
 ## 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()

fig, axes = plt.subplots(2, 3)
axes[0, 1].hist(randn(500), bins = 50, color = 'k', alpha = 0.5)
plt.
plt.show()

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()

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()
 ## 关于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()
 对于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; }
Genus | A | B | C | D |
---|---|---|---|---|
one | 0.701230 | 0.335190 | 0.256338 | 0.009374 |
two | 0.971342 | 0.121302 | 0.797251 | 0.548977 |
three | 0.936828 | 0.283768 | 0.675905 | 0.460997 |
four | 0.972239 | 0.946890 | 0.887608 | 0.767606 |
five | 0.091438 | 0.192446 | 0.618508 | 0.349543 |
six | 0.485626 | 0.179509 | 0.881206 | 0.753848 |
df.plot(kind = 'barh', stacked = True, alpha = 0.5)
plt.show()
其他各种绘图的方法就不列举了,以后用到好好学
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