B站链接【Python自动化办公--Pandas玩转Excel(全30集)-哔哩哔哩】
9 柱状图
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rc("font", family='Microsoft YaHei') #显示中文代码
df = pd.read_excel('d:/test.xlsx', sheet_name='test2')
df.sort_values(by='number', inplace=True, ascending=False)
print(df)
#生成柱状图
df.plot.bar(x='word', y='number', title='International Word by Number')
plt.tight_layout()
plt.show()
#生成柱状图
plt.bar(df.word, df.number)
plt.xticks(df.word, rotation=90)
plt.xlabel('word')
plt.ylabel('number')
plt.title('International Word by Number', fontsize=16)
10 分组柱状图
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_excel('d:/test.xlsx', sheet_name='test3')
df.sort_values(by=2023, inplace=True, ascending=False)
print(df)
df.plot.bar(x='word', y=[2022,2023], color=['orange','yellow'])
plt.title('Used Number by Word',fontsize=16,fontweight='bold')
plt.xlabel('word',fontweight='bold')
plt.ylabel('number',fontweight='bold')
ax = plt.gca()
ax.set_xticklabels(df['word'], rotation=45, ha='right')
f = plt.gcf()
f.subplots_adjust(left=0.2, bottom=0.2)
# plt.tight_layout()
plt.show()
11 叠加柱状图
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_excel('d:/test.xlsx', sheet_name='test4')
df['total'] = df.oct + df.nov + df.dec
df.sort_values(by='total', inplace=True)
print(df)
#水平柱状图 barh
df.plot.barh(x='name', y=['oct','nov','dec'], stacked=True, title='User Behavior')
plt.tight_layout()
plt.show()
12 饼状图
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_excel('d:/test.xlsx', sheet_name='test5', index_col='from')
# df['2022'].plot.pie(fontsize=8, startangle=-270)
df['2022'].plot.pie(fontsize=8, counterclock=False)
plt.title('Source of Students', fontsize=16, fontweight='bold')
plt.ylabel('2022', fontsize=12, fontweight='bold')
plt.show()
13 折线趋势图 叠加区域图
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_excel('d:/test.xlsx', sheet_name='test6', index_col='week')
print(df)
print(df.columns)
df.plot()
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_excel('d:/test.xlsx', sheet_name='test6', index_col='week')
print(df)
print(df.columns)
df.plot(y=['accessories', 'bikes', 'clothing', 'components'])
plt.title('Sales Weekly Trend', fontsize=16, fontweight='bold')
plt.ylabel('money', fontsize=12, fontweight='bold')
plt.xticks(df.index, fontsize=8)
plt.show()
df.plot.area(y=['accessories', 'bikes', 'clothing', 'components'])
df.plot.bar(y=['accessories', 'bikes', 'clothing', 'components'], stacked=True)
修改参数,生成叠加区域图和叠加柱状图
14 散点图 直方图
import pandas as pd
import matplotlib.pyplot as plt
pd.options.display.max_columns = 10
homes = pd.read_excel("./home_data.xlsx")
print(homes.head())
# homes.plot.scatter(x="sqft_living", y="price")
homes.plot.scatter(y="sqft_living", x="price")
plt.show()
15 密度图 数据相关性
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_excel('E:/PandasVersusExcel-Code/014、015/home_data.xlsx')
pd.options.display.max_columns = 10
print(df.head())
df.sqft_living.plot.hist(bins=100)
plt.xticks(range(0,max(df.sqft_living),500), fontsize=6, rotation=90)
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_excel('E:/PandasVersusExcel-Code/014、015/home_data.xlsx')
pd.options.display.max_columns = 10
print(df.head())
df.sqft_living.plot(kind = 'kde')
plt.xticks(range(0,max(df.sqft_living),500), fontsize=6, rotation=90)
plt.show()
小波折,运行的时候说没有模块scipy,安装完成就行
print(df.corr())
相关性