折线图绘制
import pandas as pd
unrate = pd.read_csv('unrate.csv')
unrate['DATE']=pd.to_datetime(unrate['DATE']) #to_datetime()转换成标准日期格式1948-01-01
import matplotlib.pyplot as plt
plt.plot() #空白图
plt.show()
first_twelve=unrate[0:12]
plt.plot(first_twelve['DATE'],first_twelve['VALUE']) #折线图
plt.show()
plt.xticks(rotation=45) #指定x轴刻度旋转角度
plt.xlabel('Month')
plt.ylabel('unemployment Rate')
plt.title('Monthly Unemployment Trends,1948')
plt.show()
子图操作
import matplotlib.pyplot as plt
#fig=plt.figure(figsize=(6,3)) #得到6*3的画图区域
fig=plt.figure() #得到画图区域
ax1=fig.add_subplot(4,3,1) #4*3子图的第一个模块,按行排序
ax2=fig.add_subplot(4,3,2)
plt.show()
ax1.plot(np.random.randint(1,5,5),np.arange(5)) #在子图上画图
绘制多折线图
#取月份
#unrate['MONTH']=unrate['DATE'].dt.month
fig=plt.figure(figsize=(10,6))
colors=['red','blue','green','orange','black']
for i in range(5):
start_index=i*12
end_index=(i+1)*12
subset=unrate[start_index:end_index]
label=str(1948+i) #指定label
plt.plot(subset['MONTH'],subset['VALUE'],c=colors[i],label=label)
plt.legend(loc='best') #
plt.show()
plt.legend(loc=‘best’)
Location String | Location Code |
---|---|
‘best’ | 0 |
‘upper right’ | 1 |
‘upper left’ | 2 |
‘lower left’ | 3 |
‘lower right’ | 4 |
‘right’ | 5 |
‘center left’ | 6 |
‘center right’ | 7 |
‘lower center’ | 8 |
‘upper center’ | 9 |
‘center’ | 10 |
条形图与散点图
import matplotlib.pyplot as plt
import pandas as pd
from numpy import arange
reviews=pd.read_csv('fandango_scores.csv')
cols=['RT_user_norm','Metacritic_user_form','IMDB_norm','Fandango_Ratingvalue','Fandango_stars']
norm_reviews=reviews[cols]
bar_heights=norm_reviews.ix[0,cols].values
bar_positions=arange(5)+0.75
fig,ax=plt.subplots() #fig不用
ax.bar(bar_positions,bar_heights,0.3) #柱形的宽为0.3
ax.set_ylabel('Rating source')
ax.set_xlabel('Average Rating')
ax.set_title('Average User Rating For Avengers(2015)')
plt.show()
#num_cols=['RT_user_norm','Metacritic_user_form','IMDB_norm','Fandango_Ratingvalue','Fandango_stars']
#bar_widths=norm_reviews.ix[0,cols].values
#bar_positions=arange(5)+0.75
#tick_positions=range(1,6)
#ax.barh(bar_positions,bar_widths,0.5) #ax.barh()横着画
#ax.set_yticks(tick_positions)
#ax.set_yticklabels(num_cols) #刻度标签
#plt.show()
散点图
fig控制图,ax具体画图
fig,ax=plt.subplots()
ax.scatter(norm_reviews['Fandango_Ratingvalue'],norm_reviews['RT_user_norm']) #ax.scatter()散点图
plt.show()
子图
fig=plt.figure(figsize=(5,10))
ax1=fig.add_subplot(2,1,1)
ax2=fig.add_subplot(2,1,2)
ax1.scatter(norm_reviews['Fandango_Ratingvalue'],norm_reviews['RT_user_norm'])
ax2.scatter(norm_reviews['Fandango_Ratingvalue'],norm_reviews['RT_user_norm'])
plt.show()
柱形图与盒图
柱形图
fig,ax=plt.subplots()
ax.hist(norm_reviews['Fandango_Ratingvalue'])
#ax.hist(norm_reviews['Fandango_Ratingvalue'],bins=20)
#ax.hist(norm_reviews['Fandango_Ratingvalue'],range=(4,5),bins=20)
plt.show()
ax.set_ylim(0,50) 指定y的区间0-50
盒图
- 盒图指提取出数据某列特征四分之一,二分之一,四分之三处的值
fig,ax=plt.subplots()
ax.boxplot(norm_reviews['RT_user_norm'])
ax.set_xticklabels(['Rotten Tomatoes'])
ax.set_ylim(0,5)
plt.show()
num_cols=('RT_user_norm','Metacritic_user_form','IMDB_norm','Fandango_Ratingvalue')
fig,ax=plt.subplots()
ax.boxplot(norm_reviews[num_cols].values)
ax.set_xticklabels(num_cols,rotation=90)
ax.set_ylim(0,5)
plt.show()
细节设置
去掉图形的框
for key,spine in ax.spines.items():
spine.setvisible(False)
选择是否要刻度线
ax.tick_params(bottom='off',top='off',left='off',right='off')
折线图线的粗细
ax.plot(subset['MONTH'],subset['VALUE'],c=colors[i],label=label,linewidth=10) #linewidth指定线的粗细
在图上添加text
ax.text(2005,87,'Men') #在x=2005,y=87位置添加Men