排序图

编程入门 行业动态 更新时间:2024-10-26 23:40:34

排序图

排序图

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.patches as patches #设置子图需要的包
import matplotlib.patches as patchesplt.rcParams['font.sans-serif']=['Simhei']#显示中文,
plt.rcParams['axes.unicode_minus']=False#显示负号
df_raw = pd.read_csv("C:/Users/GYX/Desktop/mpg_ggplot2.csv")
df = df_raw[["cty","manufacturer"]].groupby("manufacturer").mean() #提取制造商和公里每加仑
df.sort_values('cty',inplace=True)
df.reset_index(inplace=True)
fig, ax = plt.subplots(figsize=(16,10),facecolor="white",dpi=80) #设置画布的颜色和大小和分辨路
ax.vlines(x=df.index,ymin=0,ymax=df.cty,color="firebrick",alpha=0.8,linewidth=20)#设置子图的数据颜色透明度宽度
plt.rcParams['font.sans-serif']=['Simhei']#显示中文for i,cty in enumerate(df.cty):#将数据和序号打包ax.text(i,cty+0.5,round(cty,1),horizontalalignment='center')#text()函数是加注释,text(横坐标,纵坐标,居中)
#装饰4
ax.set_title("城市里程柱状图",fontdict={"size":20})
ax.set(ylabel='城市公里/加仑',ylim=(0,30))
plt.xticks(df.index #横坐标,df.manufacturer.str.upper() #df.manufacturer 是显示标尺的内容,str是转成字符串,upper是大写,rotation=60 #旋转60度,horizontalalignment='center' ,fontsize=12)
p1 = patches.Rectangle((0.57,-0.005),width = .33,height=.13,alpha=0.5,facecolor='green',transform=fig.transFigure)
p2 = patches.Rectangle((0.125,-0.005),width = .445,height = .13,alpha = 0.5,facecolor = 'red',transform=fig.transFigure)
fig.add_artist(p1)
fig.add_artist(p2)

更多推荐

排序图

本文发布于:2023-06-20 23:57:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/808883.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!