首页 > 应用开发 > Python > 怎么用matplotlib画出漂亮的分析图表
# 导入一些常用包import pandas as pdimport numpy as npimport seaborn as sns%matplotlib inlineimport matplotlib.pyplot as pltimport matplotlib as mplplt.style.use('fivethirtyeight')#解决中文显示问题,Macfrom matplotlib.font_manager import FontProperties# 查看本机plt的有效styleprint(plt.style.available)# 根据本机available的style,选择其中一个,因为之前知道ggplot很好看,所以我选择了它mpl.style.use(['ggplot'])# ['_classic_test', 'bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark-palette', 'seaborn-dark', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'seaborn', 'Solarize_Light2']# 数据集导入# 引入第 1 个数据集 Salary_Ranges_by_Job_Classificationsalary_ranges = pd.read_csv('./data/Salary_Ranges_by_Job_Classification.csv')# 引入第 2 个数据集 GlobalLandTemperaturesByCityclimate = pd.read_csv('./data/GlobalLandTemperaturesByCity.csv')# 移除缺失值climate.dropna(axis=0, inplace=True)# 只看中国# 日期转换, 将dt 转换为日期,取年份, 注意map的用法climate['dt'] = pd.to_datetime(climate['dt'])climate['year'] = climate['dt'].map(lambda value: value.year)climate_sub_china = climate.loc[climate['Country'] == 'China']climate_sub_china['Century'] = climate_sub_china['year'].map(lambda x:int(x/100 +1))climate.head()
# 选择上海部分天气数据df1 = climate.loc[(climate['Country']=='China')&(climate['City']=='Shanghai')&(climate['dt']>='2010-01-01')]\ .loc[:,['dt','AverageTemperature']]\ .set_index('dt')df1.head()
# 折线图df1.plot(colors=['lime'])plt.title('AverageTemperature Of ShangHai')plt.ylabel('Number of immigrants')plt.xlabel('Years')plt.show()
# 多条折线图df1 = climate.loc[(climate['Country']=='China')&(climate['City']=='Shanghai')&(climate['dt']>='2010-01-01')]\ .loc[:,['dt','AverageTemperature']]\ .rename(columns={'AverageTemperature':'SH'})df2 = climate.loc[(climate['Country']=='China')&(climate['City']=='Tianjin')&(climate['dt']>='2010-01-01')]\ .loc[:,['dt','AverageTemperature']]\ .rename(columns={'AverageTemperature':'TJ'})df3 = climate.loc[(climate['Country']=='China')&(climate['City']=='Shenyang')&(climate['dt']>='2010-01-01')]\ .loc[:,['dt','AverageTemperature']]\ .rename(columns={'AverageTemperature':'SY'})# 合并df123 = df1.merge(df2, how='inner', >
# 多条折线图df123.plot()plt.title('AverageTemperature Of 3 City')plt.ylabel('Number of immigrants')plt.xlabel('Years')plt.show()
???? 饼图
http://ask.baikezh.com/
df1 = salary_ranges.groupby('SetID', axis=0).sum()
# “低配版”饼图df1['Step'].plot(kind='pie', figsize=(7,7), autopct='%1.1f%%', shadow=True)plt.axis('equal')plt.show()
# “高配版”饼图colors = ['lightgreen', 'lightblue'] #控制饼图颜色 ['lightgreen', 'lightblue', 'pink', 'purple', 'grey', 'gold']explode=[0, 0.2] #控制饼图分离状态,越大越分离df1['Step'].plot(kind='pie', figsize=(7, 7), autopct = '%1.1f%%', startangle=90, shadow=True, labels=None, pctdistance=1.12, colors=colors, explode = explode)plt.axis('equal')plt.legend(labels=df1.index, loc='upper right', fontsize=14)plt.show()
???? 散点图
http://ask.baikezh.com/anhui/
# 选择上海部分天气数据df1 = climate.loc[(climate['Country']=='China')&(climate['City']=='Shanghai')&(climate['dt']>='2010-01-01')]\ .loc[:,['dt','AverageTemperature']]\ .rename(columns={'AverageTemperature':'SH'})df2 = climate.loc[(climate['Country']=='China')&(climate['City']=='Shenyang')&(climate['dt']>='2010-01-01')]\ .loc[:,['dt','AverageTemperature']]\ .rename(columns={'AverageTemperature':'SY'})# 合并df12 = df1.merge(df2, how='inner', >
# 散点图df12.plot(kind='scatter', x='SH', y='SY', figsize=(10, 6), color='darkred')plt.title('Average Temperature Between ShangHai - ShenYang')plt.xlabel('ShangHai')plt.ylabel('ShenYang')plt.show()
???? 面积图
http://zzdxb.baikezh.com/
# 多条折线图df1 = climate.loc[(climate['Country']=='China')&(climate['City']=='Shanghai')&(climate['dt']>='2010-01-01')]\ .loc[:,['dt','AverageTemperature']]\ .rename(columns={'AverageTemperature':'SH'})df2 = climate.loc[(climate['Country']=='China')&(climate['City']=='Tianjin')&(climate['dt']>='2010-01-01')]\ .loc[:,['dt','AverageTemperature']]\ .rename(columns={'AverageTemperature':'TJ'})df3 = climate.loc[(climate['Country']=='China')&(climate['City']=='Shenyang')&(climate['dt']>='2010-01-01')]\ .loc[:,['dt','AverageTemperature']]\ .rename(columns={'AverageTemperature':'SY'})# 合并df123 = df1.merge(df2, how='inner', >
colors = ['red', 'pink', 'blue'] #控制饼图颜色 ['lightgreen', 'lightblue', 'pink', 'purple', 'grey', 'gold']df123.plot(kind='area', stacked=False, figsize=(20, 10), colors=colors)plt.title('AverageTemperature Of 3 City')plt.ylabel('AverageTemperature')plt.xlabel('Years')plt.show()
???? 直方图
http://dxb.myzx.cn/dxbyq/
# 选择上海部分天气数据df = climate.loc[(climate['Country']=='China')&(climate['City']=='Shanghai')&(climate['dt']>='2010-01-01')]\ .loc[:,['dt','AverageTemperature']]\ .set_index('dt')df.head()
# 最简单的直方图df['AverageTemperature'].plot(kind='hist', figsize=(8,5), colors=['grey'])plt.title('ShangHai AverageTemperature Of 2010-2013') # add a title to the histogramplt.ylabel('Number of month') # add y-labelplt.xlabel('AverageTemperature') # add x-labelplt.show()
???? 条形图
http://dxb.myzx.cn/hunansheng/
# 选择上海部分天气数据df = climate.loc[(climate['Country']=='China')&(climate['City']=='Shanghai')&(climate['dt']>='2010-01-01')]\ .loc[:,['dt','AverageTemperature']]\ .set_index('dt')df.head()
df.plot(kind='bar', figsize = (10, 6))plt.xlabel('Month') plt.ylabel('AverageTemperature') plt.title('AverageTemperature of shanghai')plt.show()
df.plot(kind='barh', figsize=(12, 16), color='steelblue')plt.xlabel('AverageTemperature') plt.ylabel('Month') plt.title('AverageTemperature of shanghai') plt.show()
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30239065/viewspace-2742977/ ,如需转载,请注明出处,否则将追究法律责任。