Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Matplotlib条形图\获取意外图表_Python_Matplotlib_Bar Chart - Fatal编程技术网

Python Matplotlib条形图\获取意外图表

Python Matplotlib条形图\获取意外图表,python,matplotlib,bar-chart,Python,Matplotlib,Bar Chart,我有如下的df 我尝试使用matplotlib绘制收入条形图,但我的图表显示的不是预期的 fig = plt.figure(figsize=(10,15)) ax1 = plt.subplot2grid((6,7),(0,0),colspan=3,rowspan=2) ax1.bar(df.index,df['revenue'],width=30,label='Revenue',align='center') plt.show() 我希望我的图表是这样的 感谢您的帮助。非常感谢尝试使用较

我有如下的df

我尝试使用matplotlib绘制收入条形图,但我的图表显示的不是预期的

fig = plt.figure(figsize=(10,15))
ax1 = plt.subplot2grid((6,7),(0,0),colspan=3,rowspan=2)
ax1.bar(df.index,df['revenue'],width=30,label='Revenue',align='center')
plt.show()

我希望我的图表是这样的


感谢您的帮助。非常感谢

尝试使用较小的宽度并调用ax.legend()


另一个可能使事情更简单的选项是使用熊猫绘图选项。根据您的使用情况,它有相当好的默认值,如果您想进行更改,您仍然可以访问所有底层mpl设置

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np


df = pd.DataFrame({'Year': [2014, 2015, 2016, 2017, 2018],
                   'revenue': [15756726, 25252733, 44613332.0, 66339804.0, 22763520.0],
                   'gross_income': [2396119, 3922431, 7214106, 11141780, 3974763],
                   'profit_before_tax': [868197, 1385783, 2005910, 28809313, 1042985],
                   'Expected_PBT': [np.NaN, 886000, 1388000, 2200000, 2603000]
                   }).set_index('Year')

fig = plt.figure(figsize=(10, 15))
ax1 = plt.subplot2grid((6, 7), loc=(0, 0), colspan=3, rowspan=2)
df['revenue'].plot(kind='bar', ax=ax1, legend=True, rot=0, title='Requested Plot')

ax2 = plt.subplot2grid((6, 7), loc=(0, 3), colspan=3, rowspan=2)
df.plot(kind='bar', ax=ax2, legend=True, rot=0, title='Additional Example')
plt.show()

嗨,肯,它仍然不能很好地工作,因为我希望每年都有单独的酒吧。@DuongBui你能把你的数据框复制到你的问题上吗?或者快速尝试“fig,ax1=plt.subplot(1,1)”,看看问题是否得到解决。@DuongBui我只是通过进一步减小宽度来完成。干杯,不客气。我为您添加了另一个示例,希望对您有所帮助。通常我会遍历并重命名这些列,因为它们用于带有str.title的标签,并去掉下划线。
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np


df = pd.DataFrame({'Year': [2014, 2015, 2016, 2017, 2018],
                   'revenue': [15756726, 25252733, 44613332.0, 66339804.0, 22763520.0],
                   'gross_income': [2396119, 3922431, 7214106, 11141780, 3974763],
                   'profit_before_tax': [868197, 1385783, 2005910, 28809313, 1042985],
                   'Expected_PBT': [np.NaN, 886000, 1388000, 2200000, 2603000]
                   }).set_index('Year')

fig = plt.figure(figsize=(10, 15))
ax1 = plt.subplot2grid((6, 7), loc=(0, 0), colspan=3, rowspan=2)
df['revenue'].plot(kind='bar', ax=ax1, legend=True, rot=0, title='Requested Plot')

ax2 = plt.subplot2grid((6, 7), loc=(0, 3), colspan=3, rowspan=2)
df.plot(kind='bar', ax=ax2, legend=True, rot=0, title='Additional Example')
plt.show()