赞
踩
可以参考代码
- import matplotlib.pyplot as plt
- import numpy as np
- #显示中文
- plt.rcParams['font.sans-serif']=['SimHei']
-
- data1=[3,2,4,2]
- data2=[5,4,6,3]
-
- #显示多个柱形图所必须
- x=np.arange(4)
-
- myNames=["姓名1","姓名2","姓名3","姓名4"]
- plt.xlabel("name")
- plt.ylabel("value")
-
- plt.bar(x-0.1,data1,color="red",width=0.2,label="年龄")
- plt.bar(x+0.1,data2,color="blue",width=0.2,label="收入")
-
- #显示柱状图值
- for x1, y1 in zip(x,data1):
- plt.text(x1-0.1, y1, str(y1), ha='center', va='bottom', fontsize=10,color="red")
-
- for x1, y1 in zip(x,data2):
- plt.text(x1+0.1, y1, str(y1), ha='center', va='bottom', fontsize=10,color="blue")
-
- #设置x轴刻度值
- plt.xticks(x,myNames)
- #显示图例
- plt.legend()
-
- plt.show()
效果
-------------------------------------------------------------------------------------------------------------------
pandas分组处理,结果清晰。
- import matplotlib.pyplot as plt
- import numpy as np
- import pandas as pd
- plt.rcParams['font.sans-serif']=['SimHei']
- plt.rcParams['axes.unicode_minus']=False
- df=pd.read_excel("数据可视化-1.xls",sheet_name="成绩")
- mymax=df["收入"].max()
- df1=df[df["收入"]>=mymax]
- print(df1["姓名"].tolist()[0],df1["收入"].tolist()[0])
- #分组操作------饼图--------------------------------------------
- df2=df.groupby(["性别"])
- #male
- dfMale=df2.get_group("male")
- #female
- dfFemale=df2.get_group("female")
-
- mydata=[dfMale["收入"].count(),dfFemale["收入"].count()]
-
- mylab=["male","femal"]
- explode=[0,0.05]
- plt.pie(mydata, labels =mylab,autopct='%1.2f%%',colors=["red","blue"],explode=explode)
- plt.title("男女百分比",fontsize=20)
- plt.show()
- #-----柱状图--------------------------------------------------------
- #查询条件
- dfAge=["less 30","[30,50]","above 50"]
- dfless30=df[df["年龄"]<30]
- df3050=df[(df["年龄"]>=30)&(df["年龄"]<=50)]
- dfabove50=df[df["年龄"]>50]
-
- #x坐标轴分区
- listX=np.arange(3)
- dfMoney=[dfless30["收入"].mean(),df3050["收入"].mean(),dfabove50["收入"].mean()]
- dfScore=[dfless30["积分"].sum(),df3050["积分"].sum(),dfabove50["积分"].sum()]
-
- plt.xlim([-0.5,3])
- plt.bar(listX, dfMoney,color="red",width=0.2,label="收入均值")
- plt.bar(listX+0.2,dfScore,color="blue",width=0.2,label="积分和")
-
- for x1, y1 in zip(listX, dfMoney):
- plt.text(x1, y1, str(y1), ha='center', va='bottom', fontsize=10)
-
- for x1, y1 in zip(listX, dfScore):
- plt.text(x1+0.2, y1, str(y1), ha='center', va='bottom', fontsize=10)
-
- plt.legend(loc="upper right")
- plt.show()
- #--------折线图---------------------------------------------------------------
- plt.plot(dfAge,dfScore,"ro--",color="r",linewidth=0.5,alpha=0.8,label="消费积分")
- plt.plot(dfAge,dfMoney,"ro--",color="b",linewidth=0.5,alpha=0.8,label="平均收入")
-
- plt.legend(loc="upper left")
- plt.title("标题信息",fontsize=20)
- plt.xlabel("年龄阶段",fontsize=10)
- plt.ylabel("数据数量",fontsize=10)
-
- for x1, y1 in zip(listX, dfMoney):
- plt.text(x1, y1-500, str(y1), ha='center', va='bottom', fontsize=10)
-
- for x1, y1 in zip(listX, dfScore):
- plt.text(x1, y1+100, str(y1), ha='center', va='bottom', fontsize=10)
- plt.show()
- exit()
- """
- x=np.arange(-10,10.5,0.5)
- y=x**2
- plt.plot(x,y) #坐标绘图
- #plt.step(x,y) #梯度图
- #plt.bar(x,y) #柱状图
- #plt.scatter(x,y) #散列图
- #plt.pie(x,y)
- plt.grid(True,color='b') #网格线,默认是关闭的
- plt.xlabel("angle") #x轴标签
- plt.ylabel("sin(x)") #y轴标签
- plt.title("sin") #图形标题
- plt.xlim(-15,15) #x轴显示范围
- plt.ylim(-2,120) #y轴显示范围
- #plt.xticks(x) #x轴刻度列表
- plt.show()
- langs = ['C', 'C++', 'Java', 'Python', 'PHP']
- students = [23,17,35,29,12]
- #绘制饼状图
- plt.pie(students, labels = langs,autopct='%1.2f%%')
- plt.show()
- #准备数据
- data = [[30, 25, 50, 20],
- [40, 23, 51, 17],
- [35, 22, 45, 19]]
- X = np.arange(4)
- #绘制柱状图
- plt.bar(X + 0.00, data[0], color = 'b', width = 0.25)
- plt.bar(X + 0.25, data[1], color = 'g', width = 0.25)
- plt.bar(X + 0.50, data[2], color = 'r', width = 0.25)
- plt.show()
- """
- import numpy as np
- import matplotlib.pyplot as plt
-
- countries = ['USA', 'India', 'China', 'Russia', 'Germany']
- bronzes = np.array([38, 17, 26, 19, 15])
- silvers = np.array([37, 23, 18, 18, 10])
- golds = np.array([46, 27, 26, 19, 17])
- # 此处的 _ 下划线表示将循环取到的值放弃,只得到[0,1,2,3,4]
- #ind = [x for x, _ in enumerate(countries)]
- ind = countries
- #绘制堆叠图
- plt.bar(ind, golds, width=0.5, label='golds', color='gold', bottom=silvers+bronzes)
- plt.bar(ind, silvers, width=0.5, label='silvers', color='silver', bottom=bronzes)
- plt.bar(ind, bronzes, width=0.5, label='bronzes', color='#CD853F')
- #设置坐标轴
- plt.xticks(ind, countries)
- plt.ylabel("Medals")
- plt.xlabel("Countries")
- plt.legend(loc="upper right")
- plt.title("2019 Olympics Top Scorers")
- plt.show()
结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。