Skip to content

Instantly share code, notes, and snippets.

@dmargala
Last active November 3, 2017 02:02
Show Gist options
  • Save dmargala/4956217549bf11f5c737c86fe0bc2e24 to your computer and use it in GitHub Desktop.
Save dmargala/4956217549bf11f5c737c86fe0bc2e24 to your computer and use it in GitHub Desktop.
Python bar chart demo
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"labels = np.array(['A', 'B', 'C', 'D'])\n",
"values = np.array([4, 6, 9, 2])\n",
"positions = np.array([0, 1, 2, 3])\n",
"bar_width = 0.5"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAW0AAAEKCAYAAADZ8ATAAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAD/tJREFUeJzt3XmQ5GV9x/H3Z1mPRVlAASECi0c8SrQSKoqGJLYYFY+A\n8SLggcajUpZRJDGQVKp2SJnDJCpUNFZMFBUPFKJRjCaWhYOCiAdgBEEhARaERVeEVTlU9ps/+jfr\nMLszvdPdQ+8z835VTc2vf9fz7Wd7P/PM8/v1dKoKSVIbVk26AEnSjjO0JakhhrYkNcTQlqSGGNqS\n1BBDW5IaYmhrp5Tk0iS/M+k6lkqS9UlOn3Qdao+hrR2S5JokdyR5wJz1FyfZkuTAIc55WpI7k2xO\n8uPu+wsBqurgqvriEOdc19Uz72u7C8yfde1tTnJZkucttq0dqOXYJF/rntv3kvxnkt+ctcvY3iSR\n5LgkXxrX+bTzMrS1owq4GjhmZkWSg4E1jBY+b6mqtVW1W/f9zEEHJNlloc1dPRlwmjO69tYCbwQ+\nmGTvHS/7bvVs01aSE4C3AW8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yqa6vq58AZwFHj\nKeued8dV05MuYVmxP8fHvhyv1vtzlNB+MHDdrMfXd+skSUvEC5GS1JBU1XAHJk8EpqrqiO7xSUBV\n1Vvm7DdcA5K0wlVV5q4bJbR3Ab4DPBW4EfgqcExVXT5KkZKk+a0e9sCquivJ64DP0Z9meY+BLUlL\na+iRtiTpnrfiL0QmeW6SLUkeMelaWpfkriQXJbkkyde76x4aUpIHJflIkiuTfC3Jp5M8fNJ1tWjW\na/PSJBcnOSHJNvPFLVjxI+0kZwD7AedU1cmTrqdlSTZX1dpu+enAX1RVb7JVtSvJl4HTqupfu8eP\nBdZW1fmTraw9c16bewEfAc6vqqmJFjaEFT3STnI/4DDglcAxEy5nOZg9ctkduHlShbQuyVOAn80E\nNkBVfcvAHl1VbQJeA7xu0rUMY+gLkcvEUcB/VdVVSTYl+fWqunjSRTVsTZKLgDXAvsDhE66nZQcD\n35h0EctVVV2dZFWSvavqB5OuZzFW9Eib/uj6jG75o8CxE6xlObitqg6pqkcDzwROn3RB0gKanNNe\nsSPtJHvSHwke3L0BaBeggDdNtLBloqq+kmSvJHt1v45qcS4DXjDpIparJA8FftHaKBtW9kj7hcAH\nquohVfXQqloHXJ3ktyZdWMO2jly6v/i4Cvjh5MppV1WdA9w7yatm1iV5bJLDJlhWy2a/NvcG3gX8\n0+TKGd6KHWkDRwNvmbPu4/SnTM6758tZFu7bzWnP/Ad5Wa3025NG8/vAqd2fiLgduAY4fqIVtWvm\ntXlv4Of0B2xvn3BNQ1nxt/xJUktW8vSIJDXH0JakhhjaktQQQ1uSGmJoS1JDDG1JaoihLUkNMbQl\nqSH/D5Q/KhKpuG1QAAAAAElFTkSuQmCC\n",
"text/plain": [
"<matplotlib.figure.Figure at 0x10c9079d0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.bar(positions + 0.5*bar_width, values, width=bar_width, color='dodgerblue')\n",
"plt.xticks(positions + bar_width, labels)\n",
"plt.ylim(0, 10)\n",
"plt.title('My First Bar Chart')\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment