找回密码
 会员注册
查看: 32|回复: 0

Python做统计图之美

[复制链接]

2万

主题

0

回帖

7万

积分

超级版主

积分
71772
发表于 2024-9-6 19:02:46 | 显示全部楼层 |阅读模式
Python数据分析可视化案例效果图importpandasaspdimportmatplotlib.pyplotaspltimportmatplotlib#数据data={"房型":[1,2,3,4,5,6,7,8,9,10,11],"住宅类型":["普通宅","普通宅","普通宅","非普通宅","非普通宅","非普通宅","非普通宅","非普通宅","其他","其他","非普通宅"],"容积率":["列入","列入","列入","列入","列入","列入","列入","列入","不列入","不列入","不列入"],"开发成本":[4263,4323,4532,5288,5268,5533,5685,4323,2663,2791,2982],"房型面积":[77,98,117,145,156,167,178,126,103,129,133],"建房套数":[250,250,150,250,250,250,250,75,150,150,75],"开发成本(元/)":[4263,4323,4532,5288,5268,5533,5685,4323,2663,2791,2982],"售价(元/)":[12000,10800,11200,12800,12800,13600,14000,10400,6400,6800,7200]}#创建DataFramedf=pd.DataFrame(data)#设置中文字体plt.rcParams['font.sans-serif']=['SimHei']#中文显示plt.rcParams['axes.unicode_minus']=False#负号显示#创建柱状图plt.figure(figsize=(12,7))#华尔街日报风格的颜色colors=['#003f5c','#2f4b7c','#665191','#a05195','#d45087','#f95d6a','#ff7c43','#ffa600','#ffd700','#f0e0d0','#c2c2c2']#绘制柱状图bars=plt.bar(df["房型"].astype(str),df["建房套数"],color=colors,edgecolor='black')#添加数据标签forbarinbars:yval=bar.get_height()plt.text(bar.get_x()+bar.get_width()/2,yval+10,f'{yval}',ha='center',va='bottom',fontsize=10,fontweight='bold',color='black')#设置x轴刻度标签plt.xticks(df["房型"].astype(str),[f'房型{i}'foriindf["房型"]],fontsize=12)#设置轴标签和plt.ylabel('建房套数',fontsize=12)plt.title('不同房型的建房套数',fontsize=14,fontweight='bold')#添加网格线plt.grid(axis='y',linestyle='--',alpha=0.7)#自动调整图表边距以确保内容显示完整plt.tight_layout()#显示图形plt.show()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354图形效果:这个图的配色比较好看。importpandasaspdimportmatplotlib.pyplotaspltfrommatplotlibimportrcParams#数据data={"房型":[1,2,3,4,5,6,7,8,9,10,11],"住宅类型":["普通宅","普通宅","普通宅","非普通宅","非普通宅","非普通宅","非普通宅","非普通宅","其他","其他","非普通宅"],"容积率":["列入","列入","列入","列入","列入","列入","列入","列入","不列入","不列入","不列入"],"开发成本":[4263,4323,4532,5288,5268,5533,5685,4323,2663,2791,2982],"房型面积":[77,98,117,145,156,167,178,126,103,129,133],"建房套数":[250,250,150,250,250,250,250,75,150,150,75],"开发成本(元/平方米)":[4263,4323,4532,5288,5268,5533,5685,4323,2663,2791,2982],"售价(元/平方米)":[12000,10800,11200,12800,12800,13600,14000,10400,6400,6800,7200]}#创建DataFramedf=pd.DataFrame(data)#设置字体rcParams['font.sans-serif']=['SimHei']#例如,使用SimHei字体显示中文rcParams['axes.unicode_minus']=False#显示负号#创建散点图plt.figure(figsize=(14,7))scatter=plt.scatter(df["房型面积"],df["售价(元/平方米)"],c=df["开发成本"],cmap='viridis',s=100,edgecolors='k')plt.colorbar(scatter,label='开发成本(元/平方米)')plt.xlabel('房型面积(平方米)')plt.ylabel('售价(元/平方米)')plt.title('房型面积与售价以及成本的关系')plt.grid(True)#在每个数据点上标记房型,保持一定距离foriinrange(len(df)):plt.text(df["房型面积"][i]+2,df["售价(元/平方米)"][i]+200,f'房型{i+1}',fontsize=9,ha='left')plt.show()1234567891011121314151617181920212223242526272829303132333435363738散点图效果如下:散点图可以同时反应3个关系。importpandasaspdimportmatplotlib.pyplotasplt#数据data={"房型":[1,2,3,4,5,6,7,8,9,10,11],"建房套数":[250,250,150,250,250,250,250,75,150,150,75]}#创建DataFramedf=pd.DataFrame(data)#饼图绘制plt.figure(figsize=(10,8))#高级感配色方案colors=['#6C5B7B','#C06C84','#F67280','#F8B195','#F9D5A8','#F3B6A3','#E1C6C1','#D9B8C4','#C9A7B4','#B68583','#A9A5A0']#绘制饼图plt.pie(df["建房套数"],labels=[f'房型{i}'foriindf["房型"]],colors=colors,autopct='%1.1f%%',startangle=140,wedgeprops={'edgecolor':'black'})#添加plt.title('不同房型建造套数的占比')#显示图形plt.show()12345678910111213141516171819202122232425262728一个简单的饼图:看起来比较清爽。importmatplotlib.pyplotaspltimportnumpyasnpfrommatplotlibimportcm,colors#设置支持中文的字体(使用默认的或者系统中可用的字体)plt.rcParams['font.sans-serif']=['SimHei']#SimHei是黑体字的中文字体plt.rcParams['axes.unicode_minus']=False#解决负号'-'显示问题#提供的数据labels=['房型1','房型2','房型3','房型4','房型5','房型6','房型7','房型8','房型9','房型10','房型11']heights=[0.0606,0.0909,0.0758,0.0909,0.1061,0.1212,0.1364,0.0909,0.0303,0.0455,0.0606]#按高度排序数据(顺时针递增)sorted_indices=np.argsort(heights)sorted_labels=[labels[i]foriinsorted_indices]sorted_heights=[heights[i]foriinsorted_indices]#创建反转渐变色cmap=cm.get_cmap('plasma_r')#使用反转的渐变色norm=colors.Normalize(vmin=min(sorted_heights),vmax=max(sorted_heights))colors_map=[cmap(norm(height))forheightinsorted_heights]#设置图形和极坐标plt.figure(figsize=(12,12))ax=plt.subplot(111,polar=True)#设置高度和宽度width=2*np.pi/len(sorted_heights)angles=[i*widthforiinrange(len(sorted_heights))]#绘制条形图bars=ax.bar(x=angles,height=sorted_heights,width=width,bottom=0,linewidth=1,edgecolor="white",color=colors_map)#标签设置labelPadding=0.02#调整标签与条形的距离forbar,angle,height,labelinzip(bars,angles,sorted_heights,sorted_labels):rotation=np.rad2deg(angle)alignment="left"ifangle>=np.pi/2andangle
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

QQ|手机版|心飞设计-版权所有:微度网络信息技术服务中心 ( 鲁ICP备17032091号-12 )|网站地图

GMT+8, 2025-1-10 14:01 , Processed in 0.456125 second(s), 26 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表