matplotlib日常练习(2)

表图

import numpy as np
import matplotlib.pyplot as plt

# Represent years in rows in the table
rows = ['2011', '2012', '2013', '2014', '2015']
# Represent battery rating in columns of the table
columns = ('7Ah', '35Ah', '40Ah', '135Ah', '150Ah')

# Number of units sold each year, each rating. e.g. 75 units of 7Ah batteris sold in 2011
data = [[75, 144, 114, 102, 108],
        [90, 126, 102,  84, 126],
        [96, 114,  75, 105, 135],
        [105, 90, 150,  90,  75],
        [90,  75, 135,  75,  90]]

# Define the range and scale for y-axis
values = np.arange(0, 600, 100)

# Specify the color map to be used
colors = plt.cm.OrRd(np.linspace(0, 0.5, len(rows)))
n_rows = len(data)
index = np.arange(len(columns)) + 0.3
bar_width = 0.5

# Initialize the vertical-offset for the stacked bar chart.
y_offset = np.zeros(len(columns))
fig, ax = plt.subplots()

# Plot bars and create text labels for the table
cell_text = []

for row in range(n_rows):
    plot = plt.bar(index, data[row], bar_width, bottom=y_offset, color=colors[row])
    y_offset = y_offset + data[row]
    cell_text.append(['%1.1f' % (x) for x in y_offset])
    i=0
    for rect in plot:
        ax.text(rect.get_x() + rect.get_width()/2, y_offset[i],'%d' % int(y_offset[i]), 
                ha='center', va='bottom')
        i = i+1   

# Add a table at the bottom of the axes
the_table = plt.table(cellText=cell_text,
                      rowLabels=rows,
                      rowColours=colors,
                      colLabels=columns,
                      loc='bottom')

plt.ylabel("Units Sold")
plt.title('Number of Batteries Sold/Year')

# No ticks on X-axis, as table below will cover the labels
plt.xticks([])

箱型图

wine_quality = pd.read_csv('winequality.csv', delimiter=';')
data = [wine_quality['alcohol'], wine_quality['fixed acidity'], wine_quality['quality']]
plt.boxplot(data)

小提琴图

wine_quality = pd.read_csv('winequality.csv', delimiter=';')
data = [wine_quality['alcohol'], wine_quality['fixed acidity'], wine_quality['quality']]
plt.violinplot(data, showmeans=True)

你可能感兴趣的:(matplotlib日常练习(2))