目录
(1)今有2018年1月1日—15日的猪肉价格和牛肉价格的数据,它们存在于一个Excel表格中,如下表所示。将其读入Python中并用一个数据框变量df保存。
(2)分别绘制1月1日—10日的猪肉价格和牛肉价格走势图。
(3)在同一个figure界面中,用一个2×1的子图分别绘制2018年1月前半个月的猪肉价格和牛肉价格走势图。
文件下载链接 提取码:fw2nhttps://pan.baidu.com/s/1w7bvsdjvily9yb35sTWMCg?pwd=fw2n
In:import matplotlib.pyplot as plt
from matplotlib import font_manager
x1 = df.iloc[:10,:][["日期"]].values[:,0]
y1 = df.iloc[:10,:][["猪肉价格"]].values[:,0]
plt.rcParams['font.sans-serif']='SimHei'
plt.figure(figsize=(25,10))
plt.subplot(1, 2, 1)
plt.title("猪肉价格走势图",fontsize=20)
plt.xlabel("日期",fontsize=20)
plt.ylabel("猪肉价格",fontsize=20)
plt.legend
plt.xticks(rotation=45)
plt.plot(x1,y1,'r*--')
In:import pandas as pd
import numpy as np
df=pd.read_excel('data.xlsx')
df
Out: 日期 猪肉价格 牛肉价格
0 2018-01-01 11.0 38.0
1 2018-01-02 12.0 39.0
2 2018-01-03 11.5 41.3
3 2018-01-04 12.0 40.0
4 2018-01-05 12.0 43.0
5 2018-01-06 11.2 44.0
6 2018-01-07 13.0 47.0
7 2018-01-08 12.6 43.0
8 2018-01-09 13.5 42.3
9 2018-01-10 13.9 42.0
10 2018-01-11 13.8 43.1
11 2018-01-12 14.0 42.0
12 2018-01-13 13.5 39.0
13 2018-01-14 14.5 38.0
14 2018-01-15 14.8 37.5
In:import matplotlib.pyplot as plt
from matplotlib import font_manager
x1 = df.iloc[:10,:][["日期"]].values[:,0]
y1 = df.iloc[:10,:][["猪肉价格"]].values[:,0]
plt.rcParams['font.sans-serif']='SimHei'
plt.figure(figsize=(25,10))
plt.subplot(1, 2, 1)
plt.title("猪肉价格走势图",fontsize=20)
plt.xlabel("日期",fontsize=20)
plt.ylabel("猪肉价格",fontsize=20)
plt.legend
plt.xticks(rotation=45)
plt.plot(x1,y1,'r*--')
Out:
In:x2 = df.iloc[:10,:][["日期"]].values[:,0]
y2 = df.iloc[:10,:][["牛肉价格"]].values[:,0]
plt.rcParams['font.sans-serif']='SimHei'
plt.figure(figsize=(25,10))
plt.subplot(1, 2, 1)
plt.title("牛肉价格走势图",fontsize=20)
plt.xlabel("日期",fontsize=20)
plt.ylabel("牛肉价格",fontsize=20)
plt.xticks(rotation=45)
plt.plot(x2,y2,'b*--')
Out:
In:x1 = df[['日期']].values[:,0]
y1 = df[["猪肉价格"]].values[:,0]
plt.figure(figsize=(25,22))
plt.subplot(2, 1, 1)
plt.title("猪肉价格走势图",fontsize=20)
plt.xlabel("日期",fontsize=20)
plt.ylabel("猪肉价格",fontsize=20)
plt.xticks(rotation=45)
plt.plot(x1,y1,'r*--')
Out:
In:x1 = df[['日期']].values[:,0]
y1 = df[["牛肉价格"]].values[:,0]
plt.figure(figsize=(25,22))
plt.subplot(2, 1, 1)
plt.title("牛肉价格走势图",fontsize=20)
plt.xlabel("日期",fontsize=20)
plt.ylabel("牛肉价格",fontsize=20)
plt.xticks(rotation=45)
plt.plot(x1,y1,'b*--')
Out: