pandas操作excel-制图

 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())

相关性

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值