primitives基本元素是容器中包含的元素,container是容器用來盛放primitives基本元素。
primitives中包含要繪制的基本元素:曲線、矩陣、圖像、多邊形、集合等;
container中包含了很多primitives基本元素,但同時也有自己的屬性,是繪制的基礎框架
四個對象容器分別為Figure容器,Axes容器,Axis容器,Tick容器。
matplotlib文檔。按照管理范圍的關系:Figure > Axes > Axis > Tick
Figrue:是Artist最頂層的container-對象容器,包含了圖表中的所有元素;
Axes:大量用于繪圖的Artist存放在它內部,是matplotlib的核心;
Axis容器:管理坐標軸;
Tick容器: 最末端的容器對象,包含tick、grid line實例以及對應的label;
# 數據導入代碼
# 導入包
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd# 導入數據集并轉成方便作圖的格式
Dataset = pd.read_csv('data/Drugs.csv')
print(Dataset.head())
group = Dataset.groupby(['YYYY', 'State']).agg('sum').reset_index()
print(group.head())
df = group.pivot(index='YYYY', columns='State', values='DrugReports').reset_index()
print(df.head())
YYYY State COUNTY SubstanceName DrugReports
0 2010 VA ACCOMACK Propoxyphene 1
1 2010 OH ADAMS Morphine 9
2 2010 PA ADAMS Methadone 2
3 2010 VA ALEXANDRIA CITY Heroin 5
4 2010 PA ALLEGHENY Hydromorphone 5YYYY State DrugReports
0 2010 KY 10453
1 2010 OH 19707
2 2010 PA 19814
3 2010 VA 8685
4 2010 WV 2890
State YYYY KY OH PA VA WV
0 2010 10453 19707 19814 8685 2890
1 2011 10289 20330 19987 6749 3271
2 2012 10722 23145 19959 7831 3376
3 2013 11148 26846 20409 11675 4046
4 2014 11081 30860 24904 9037 3280
from matplotlib.lines import Line2D
fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(111)ax.patch.set_facecolor('#E9E9F1')
ax.plot(df.YYYY,df.KY,color='gray',linewidth=0.5)
ax.text(df.YYYY.values[-1],df.KY.values[-1],'KY',fontsize=8,color='gray')
ax.plot(df.YYYY,df.OH,color='gray',linewidth=0.5)
ax.text(df.YYYY.values[-1],df.OH.values[-1],'OH',fontsize=8,color='gray')
ax.plot(df.YYYY,df.PA,color='orange',linewidth=2)
ax.text(df.YYYY.values[-1],df.PA.values[-1],'PA',fontsize=8,color='orange')
ax.plot(df.YYYY,df.VA,color='gray',linewidth=0.5)
ax.text(df.YYYY.values[-1],df.VA.values[-1],'VA',fontsize=8,color='gray')
ax.plot(df.YYYY,df.WV,color='gray',linewidth=0.5)
ax.text(df.YYYY.values[-1],df.WV.values[-1],'WV',fontsize=8,color='gray')
ax.grid(color='#FFFFFF')
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.tick_params(bottom=False,right=False,left=False)
ax.set_xlabel("Year")
ax.set_ylabel("DrugReports")
ax.text(2009.7,49000,'Evolution of PA vs other states',color='orange',fontsize=8)
plt.show()
matplotlib讀音?
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
x = np.linspace(0,10,1000)
y = -(x-2)*(x-8)+10
ax1.set_xlim(-0.5,10.5)
ax1.set_ylim(0,20.0)
ax1.plot(x,y,color='red',linewidth=2)
x2 = np.linspace(2,9,1000)
y2 = -(x2-2)*(x2-8)+10
ax1.fill_between(x2,0,y2,facecolor='#D3D3D3')
<matplotlib.collections.PolyCollection at 0x7ffb1cb785e0>
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
x = np.linspace(0,10,1000)
y = -(x-2)*(x-8)+10
ax1.set_xlim(-0.5,10.5)
ax1.set_ylim(0,20.0)
ax1.plot(x,y,color='red',linewidth=2)
for i in np.arange(2,9,0.2):rect = plt.Rectangle((i,0),0.1,-(i-2)*(i-8)+10)rect.set_color('#D3D3D3')ax1.add_patch(rect)
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态