08 Drawing and Visualization

Chapter 8, Drawing and Visualization

Best Learning Resource: The sample library and official documentation for matplotlib

import matplotlib.pyplot as plt

8.1 Getting started with matplotlib

Figure和Subplot

All matplotlib images are located in the figure object.

# 创建一个figure
fig = plt.figure()
# 添加子图:在2*2的结构里的第1个位置
ax1 = fig.add_subplot(2,2,1)
# plt.plot默认在最后一个添加的图中绘制,以后可以ax1.plot也可以plt.plot
plt.plot(np.random.randn(50).cumsum(),'k--')
# 在定义好的子图ax中绘制
ax1.hist(np.random.randn(50),bins=10,color='k',alpha=0.3)
# 定义子图
fig,axes = plt.sub_plot(2,3)
# 索引定义的子图
axes2 = axes[0,1]
# 利用figure的subplots_adjust方法调整间距
# hspace和wspace为subplot的间距百分比
plt.subplots_adjust(wspace=0,hspace=0)

Colors, markers and line styles

The plot function accepts an x, y (x is optional), and a style abbreviation.

# 画普通的x与y的散点图,如果y为多维的,则每个都会画出
plot([x],y,[fmt])
# fmt = '[color][maker][line]'
# color:r红b蓝g绿y黄w白k黑
# marker:.点,像素o圆v倒三角^正三角
# line:-实线--短横线-.点横线:点线

Ticks, labels and legends

# 改变轴的范围
plt.xlim([0,10])
# 改变刻度
ax.set_xticks([0,250,500,750,1000])
# 设置刻度标签
ax.set_xticklabels(['one','two','three','four','five'],rotation=30)
# 设置x标签的名称
ax.set_xlabel(u'??')
# 设置图片的名称
ax.set_title(u'??')
# 添加图例,label参数
ax.plot(randn(100).cumsum(),'k--',label='one')
# 图例的位置
ax.legend(loc='best')

Annotate and draw on Subplot

text、arrow、annotate

# 在指定位置添加文字
ax.text(x,y,'??',family='?',fontsize=?)
# 添加注解。。。没整明白
ax.annotate(annotation_string,xy=(x,y),xytext=(x,y),arrowprops=dict(facecolor='black'),horizontalalignment='left',varticalalignment='top')

Save chart to file

# 文件名和像素,根据后缀推断文件类型
plt.savefig('xxx.jpg',dpi=??)

matplotlib configuration

Some image formats can be configured globally.

plt.rc('??',??)
# 第一个问号可以是figure axes  xtick  ytick  grid  legent
# 第二个可以定义一个字典
font_options={'family':'monospace','weight':'bold','size':'small'}
plt.rc('font',**font_options)

8.2 Plotting functions in pandas

matplotlib is a relatively low-level plotting tool, and you have to manually code names, labels, legends, etc. pandas comes with it. The default is a line graph.

line graph

  • Series: Plot the data, the default index is the x-axis
    obj.plot(ax=?,xlim=?,ylim=?,xticks=?,yticks=?,kind=?,use_index=?)

    • ax: subplot object, which can be implanted
    • xlim ylim: range
    • xticks yticks: tags
    • kind: type
    • use_index: whether to use index as the x-axis
  • DataFrame: Plot each column of data, the same as Series

Histogram (bar)

  • kind='bar': vertical bar chart
  • kind='barh': horizontal histogram
  • stacked=True: stacked column chart

Histograms and Density Plots

  • histogram (kind='hist')

    A histogram that can be displayed as a binned representation of the frequency of values. Data points are scrambled into discrete, evenly spaced bins, and the number of data points in each bin is plotted.

  • density map (kind='kde')

    Produced by computing "likely to produce estimates of continuous probability distributions of observed data". Half the process is to approximate the distribution as a set of kernels (Gaussian distribution). Therefore, the density map also becomes kde.

Scatter plot

An efficient means of observing the relationship between two one-dimensional data. Use the scatter function in matplotlib.

  • plt.scatter(x_series,y_series)
  • plt.scatter_matrix(df,diagonal=’kde’)

    Diagonal refers to a drawing that adds one more kde to the diagonal.

8.3 Mapping: Graphically Love You Are Undersea Earthquake Crisis Data

8.4 The python graphical tool ecosystem

Chaco

mayavi

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324627458&siteId=291194637