1.编程实现:利用pandas的DataFrame将下列淡水鱼数据指定行号(默认数字编号),指定列号(鱼号,鱼名、习性、产量)。
已知淡水鱼数据
data = [
['f1','鲤鱼','杂食性',300],
['f2','草鱼','食草性',240],
['f3','鲫鱼','温水性',500],
['f4','鳙鱼','温水性',400],
['f5','黑鱼','肉食性',480],
]
2.编程实现:利用上述淡水鱼数据中鱼名与产量的数据折线图生成淡水鱼产量统计图。
from pandas import DataFrame
data = [
['f1', '鲤鱼', '杂食性', 300], ['f2', '草鱼', '食草性', 240],
['f3', '鲫鱼', '温水性', 500], ['f4', '鳙鱼', '温水性', 400],
['f5', '黑鱼', '肉食性', 480],
]
obj1 = DataFrame(data,columns=('鱼号', '鱼名', '习性', '产量'))
print(obj1)
ming, chan = [], []
for i in range(0, 5):
ming.append(data[i][1])
chan.append(data[i][3])
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
plt.figure(figsize=(12, 6))
plt.plot(ming, chan, label='产量', color='green', linewidth=2)
plt.title("淡水鱼产量统计图")
plt.xlabel("鱼名")
plt.ylabel("产量")
for a, b in zip(ming, chan):
plt.text(a, b, '%d' % b, fontsize=10)
plt.legend()
plt.savefig("plot1.png")
plt.show()