Home > OS >  how to make legend bar to thinner
how to make legend bar to thinner

Time:12-16

I have data like this

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

and i plotted the bar graph using this code

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)

and this code generates the plot as plot

I want to make the legend symbol bar to be thinner,however it is not currently possible using plt.legend command while using df.pivot, I hope experts may help me solving this problem.

CodePudding user response:

You can add some parameters:

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)

so, I added:


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

if you want to mak it thinner add:

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

For other parameters of legend:

enter image description here

  • Related