1、折线图
import matplotlib.pyplot as plt
plt.title("全国疫情趋势")
plt.rcParams["font.sans-serif"] = ["SimHei"]
list1 = ["10.8", "10.31", "11.28", "12.18", "1.18", "2.13"]
list2 = [40, 28, 104, 89, 232, 19]
list3 = [21, 21, 11, 14, 12, 7]
plt.plot(list1, list2, "ro-", label="总新增确诊")
plt.plot(list1, list3, "yo-", label="新增境外输入")
plt.xlabel("日期")
plt.ylabel("人数")
plt.legend()
plt.show()
2、饼图
import matplotlib.pyplot as plt
plt.title("城市一个月降雨天数比例")
plt.rcParams["font.sans-serif"] = ["SimHei"]
city = ["武汉", "荆州", "沙市", "武昌"]
data = [100, 50, 20, 150]
e1 = [0, 0, 0, 0.05]
plt.pie(data, labels=city, autopct="%.2f%%", explode=e1, shadow=True)
plt.legend()
plt.show()
3、柱状图
import matplotlib.pyplot as plt
import numpy as np
plt.title("武汉各省市的确诊人数和治愈人数对比")
plt.rcParams["font.sans-serif"] = ["SimHei"]
list1 = ["江岸区", "江汉区", "汉阳区", "武昌区", "青山区", "洪山区"]
list2 = [22, 46, 89, 24, 120, 23]
list3 = [11, 34, 77, 23, 90, 22]
width1 = 0.4
plt.bar(range(len(list1)), list2, color="blue", width=width1, label="确诊人数")
print(range(len(list1)))
plt.bar(np.arange(len(list1)) + width1 + 0.005, list3, color="red", width=width1, label="治愈人数")
print(np.arange(len(list1)) + width1 + 0.005)
for x, y in enumerate(list2):
plt.text(x, y + 5, str(y), ha="center", va="bottom")
for x, y in enumerate(list3):
plt.text(x + width1 + 0.005, y + 5, str(y), ha="center", va="bottom")
plt.xticks(np.arange(len(list1)) + (width1 + 0.005) / 2, list1)
plt.legend()
plt.show()