我有这样的数据
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能帮我解决这个问题。
1条答案
按热度按时间ttp71kqs1#
您可以添加一些参数:
所以我又说:
如果你想使它更薄,添加:
对于图例的其他参数:
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.legend.html