import numpy as np
import matplotlib.pyplot as plt
条形图
# 数据准备
= ['ABC', 'DEF', 'GHI', 'JKL', 'MNO']
customers = range(len(customers))
customers_index = [127, 90, 201, 111, 232] sale_amounts
= plt.figure(figsize=(16,9)) # figsize确定像素比
fig # plt.style.use('ggplot') 使用 ggplot 风格
= fig.add_subplot(1, 1, 1) # 创建子图
ax1 # customer_index设置条形图在x轴上的位置;sale_amounts设置条形的高度;align='center'设置x轴标签处于条形中间
='center', color='darkblue')
ax1.bar(customers_index, sale_amounts, align'bottom') # 设置刻度位置
ax1.xaxis.set_ticks_position('left')
ax1.yaxis.set_ticks_position(=0, fontsize='small') # 设置刻度值
plt.xticks(customers_index, customers, rotation'Customer Name') # 设置x轴标签
plt.xlabel('Sale Amount') # 设置y轴标签
plt.ylabel('Sale Amount per Customer') # 设置图形标题
plt.title('bar_plot.png', dpi=100, bbox_inches='tight') # 保存图片;设置图形分辨率;将图形四周空白部分去掉
plt.savefig( plt.show()
直方图
# 数据准备
= 100, 130, 15
mu1, mu2, sigma = mu1 + sigma * np.random.randn(10000)
x1 = mu2 + sigma * np.random.randn(10000) x2
= plt.figure(figsize=(16, 9))
fig 'ggplot')
plt.style.use(= fig.add_subplot(1, 1, 1)
ax1
= ax1.hist(x1, bins=50, color='darkgreen')
n, bins, patches = ax1.hist(x2, bins=50, color='orange', alpha=0.5)
n, bins, patches
'bottom')
ax1.xaxis.set_ticks_position('left')
ax1.yaxis.set_ticks_position('Bins')
plt.xlabel('Number of Values in Bin')
plt.ylabel('Histograms', fontsize=14, fontweight='bold')
fig.suptitle('Two Frequency Distributions')
ax1.set_title( plt.show()
折线图
# 数据准备
= np.random.randn(50).cumsum()
plot_data1 = np.random.randn(50).cumsum()
plot_data2 = np.random.randn(50).cumsum()
plot_data3 = np.random.randn(50).cumsum() plot_data4
= plt.figure(figsize=(16, 9))
fig = fig.add_subplot(1, 1, 1)
ax1 =r'o', color=u'blue', linestyle='-', label='Blue Solid')
ax1.plot(plot_data1, marker=r'+', color=u'red', linestyle='--', label='Red Dashed')
ax1.plot(plot_data2, marker=r'*', color=u'green', linestyle='-', label='Green Dash Dot')
ax1.plot(plot_data3, marker=r's', color=u'orange', linestyle=':', label='Orange Dotted')
ax1.plot(plot_data4, marker'bottom')
ax1.xaxis.set_ticks_position('left')
ax1.yaxis.set_ticks_position('Line Plots: Markers, Colors, and Linestyles')
ax1.set_title('Draw')
plt.xlabel('Random Number')
plt.ylabel(='best') plt.legend(loc