pandas 如何使图例条变细

ki0zmccv  于 2022-12-16  发布在  其他
关注(0)|答案(1)|浏览(92)

我有这样的数据

Year,stations,Views
2015,1,100
2015,2,85
2015,3,75
2015,4,25
2015,5,10
2015,6,100
.........
2022,12,100

我用这个代码画了柱状图

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import AutoMinorLocator, ScalarFormatter
import matplotlib.pyplot as plt
from matplotlib import ticker
from glob import glob
import numpy as np
import os
import matplotlib as mpl
mpl.rcParams['axes.linewidth'] = 0.8 #set the value globally

plt.rcParams['xtick.major.pad']='1' #control gap betwn axis and its nos
plt.rcParams['ytick.major.pad']='1'
plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = ['Times New Roman'] + plt.rcParams['font.serif']
plt.rcParams['xtick.major.size'] = 3
plt.rcParams['xtick.minor.size'] = 1.6
plt.rcParams['xtick.major.width'] = 0.9
plt.rcParams['xtick.minor.width'] = 0.9
plt.rcParams['ytick.major.size'] = 3
plt.rcParams['ytick.minor.size'] = 1.6
plt.rcParams['ytick.major.width'] = 0.9
plt.rcParams['ytick.minor.width'] = 0.9
plt.rcParams['xtick.bottom'] = True

df=pd.read_csv("input.csv")
print(df)

month_dict = {1 : "BiD", 2 : "BCD", 3 : "BTY", 4 : "HJY", 
              5 : "GGH" , 6 : "GHY", 7 : "JHT", 8 : "JRT", 
              9 : "KTY", 10 : "MNY" ,11 : "NTY",12 : "NRT", 
              13 : "PGH", 14 : "RTY" ,15 : "RER",16 : "RAS",
              17 : "STT", 18 : "TYU" ,19 : "UIU",20 : "UTY",
              21 : "UII", 22 : "UII" ,23 : "UII",24 : "UII",
              25 : "UII", 26 : "UII" ,27 : "UII",28 : "UII",
              29 : "UII", 30 : "UII"}

df.pivot('Year','stations','Views').rename(columns=month_dict).plot.bar(figsize = (10.5,5.8),width=0.7)
plt.legend(bbox_to_anchor=(1.0, 1.0), prop={'size': 7.2},frameon=False)

这段代码生成的图为

我想把图例符号条做的更细一些,但是目前还不能在使用df.pivot的同时使用plt.legend命令,希望Maven能帮我解决这个问题。

ttp71kqs

ttp71kqs1#

您可以添加一些参数:

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import AutoMinorLocator, ScalarFormatter
import matplotlib.pyplot as plt
from matplotlib import ticker
from glob import glob
import numpy as np
import os
import matplotlib as mpl
mpl.rcParams['axes.linewidth'] = 0.8 #set the value globally

plt.rcParams['xtick.major.pad']='1' #control gap betwn axis and its nos
plt.rcParams['ytick.major.pad']='1'
plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = ['Times New Roman'] + plt.rcParams['font.serif']
plt.rcParams['xtick.major.size'] = 3
plt.rcParams['xtick.minor.size'] = 1.6
plt.rcParams['xtick.major.width'] = 0.9
plt.rcParams['xtick.minor.width'] = 0.9
plt.rcParams['ytick.major.size'] = 3
plt.rcParams['ytick.minor.size'] = 1.6
plt.rcParams['ytick.major.width'] = 0.9
plt.rcParams['ytick.minor.width'] = 0.9
plt.rcParams['xtick.bottom'] = True
plt.rcParams['legend.handlelength']=0.2
df=pd.read_csv("thinner.csv")
print(df)

month_dict = {1 : "BiD", 2 : "BCD", 3 : "BTY", 4 : "HJY", 
              5 : "GGH" , 6 : "GHY", 7 : "JHT", 8 : "JRT", 
              9 : "KTY", 10 : "MNY" ,11 : "NTY",12 : "NRT", 
              13 : "PGH", 14 : "RTY" ,15 : "RER",16 : "RAS",
              17 : "STT", 18 : "TYU" ,19 : "UIU",20 : "UTY",
              21 : "UII", 22 : "UII" ,23 : "UII",24 : "UII",
              25 : "UII", 26 : "UII" ,27 : "UII",28 : "UII",
              29 : "UII", 30 : "UII"}

df.pivot('Year','stations','Views').rename(columns=month_dict).plot.bar(figsize = (10.5,5.8),width=0.7)
plt.legend(bbox_to_anchor=(1.0, 1.0), prop={'size': 7.2},frameon=False)

所以我又说:

plt.rcParams['legend.handlelength']=0.2

如果你想使它更薄,添加:

plt.rcParams['legend.handleheight']=0.05 #or whatever height you want

对于图例的其他参数:
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.legend.html

相关问题