在Jupyter iPython Notebook中使用matplotlib绘制图表

5

我正在尝试在Jupyter Python笔记本中使用matplotlib绘制图形。但是当我分配y轴标签时,它不显示在图形中,并且它绘制了两个图形。我正在使用的代码是:

trans_count_month = df.groupby('month_').TR_AMOUNT.count() 
plt.xlabel('Months')  #X-axis label
plt.ylabel('Total Transactions Count') #Y-axis label 
plt.title("Month wise Total Transaction Count") #Chart title. 
width = 9
height = 5
plt.figure(figsize=(width, height))
trans_count_month.plot(kind='bar')
plt.figure(figsize=(width, height))

我得到的输出是: 在这里输入图片描述 我该如何显示只有一个带y轴标签的图表,如果有其他绘制此图表的方法,请分享解决方案。

1
我猜只需删除第一个 plt.figure(figsize=width, height) 即可。 - ml4294
尝试更改顺序。plt.xlabel/ylable/title 应该在 .plot 之后。此外,使用 plotfigsize 关键字参数,而不是 plt.figure(figsize=()) - Dror
谢谢@Dror,现在它正常工作了。 - neha
1个回答

8
下面是一个最简单的例子:

导入pandas库:

import pandas as pd

%matplotlib inline
from matplotlib import pyplot as plt
import pandas as pd

s = pd.Series([1,2,3], index=['a','b','c'])

s.plot.bar(figsize=(20,10))
plt.xlabel('Foo')
plt.ylabel('Bar')
plt.title("Hello World");

在此输入图片描述

如何更好地利用pandas和matplotlib。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接