Python Forum

Full Version: Barplots
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to create horizontal and vertical bar plots. Below is my code. However, pressing "return" after the first plot command (line 3 of the code), all I get is the first output listed below. And after pressing "return" after the second plot command (line 4 of the code), I get the second output listed below. I do not get a graphical bar plot. I have imported everything I can think of: numpy, matplotlib.pyplot, pandas (i.e. things that I need to plot the graph as well as things I don't need) but horizonal and vertical bar plots are NOT being produced. Could someone help? I'm a complete newbie.


fig, axes = plt.subplots(2,1)
data = pd.Series(np.random.rand(16), index=list('abcdefghijklmnop'))
data.plot.bar(ax=axes[0], color='k', alpha=0.7)
data.plot.barh(ax=axes[1], color='k', alpha=0.7)
Output received:
<matplotlib.axes._subplots.AxesSubplot at 0x2016a51ad30>
<matplotlib.axes._subplots.AxesSubplot at 0x2016a54e438>
Call plt.show() and it seems to give what you want:
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt


ig, axes = plt.subplots(2,1)
data = pd.Series(np.random.rand(16), index=list('abcdefghijklmnop'))
data.plot.bar(ax=axes[0], color='k', alpha=0.7)
data.plot.barh(ax=axes[1], color='k', alpha=0.7)

plt.show()
[attachment=479]