9

I am trying to create a line chart for a sample data shown in screenshot. I googled quite a bit and looked at some links below and tried to use matplotlib, but I could not get the desired output as shown in the linegraph (screenshot) below, can anyone provide me a sample reference to get started? How to get the line chart with the sample input shown below?

http://www.josechristian.com/programming/smooth-line-plots-python/ http://yaboolog.blogspot.com/2011/07/python-tips-create-line-graph-with.html

linechart

Code:

import matplotlib.pyplot as plt
import numpy as np
# just create some random data
fnx = lambda : np.random.randint(3, 10, 10)
y = np.row_stack((fnx(), fnx(), fnx()))   
# this call to 'cumsum' (cumulative sum), passing in your y data, 
# is necessary to avoid having to manually order the datasets
x = np.arange(10) 
y_stack = np.cumsum(y, axis=0)   # a 3x10 array

fig = plt.figure()

plt.savefig('smooth_plot.png')
Erba Aitbayev
  • 4,167
  • 12
  • 46
  • 81
Ritz
  • 1,193
  • 6
  • 15
  • 26

1 Answers1

20

Using your data provided in screenshot:

import matplotlib.pyplot as plt
import numpy as np

builds = np.array([1, 2, 3, 4])
y_stack = np.row_stack(([1, 2, 3, 4], [5, 2, 9, 1], [20, 10, 15, 1], [5, 10, 15, 20])) 

fig = plt.figure(figsize=(11,8))
ax1 = fig.add_subplot(111)

ax1.plot(builds, y_stack[0,:], label='Component 1', color='c', marker='o')
ax1.plot(builds, y_stack[1,:], label='Component 2', color='g', marker='o')
ax1.plot(builds, y_stack[2,:], label='Component 3', color='r', marker='o')
ax1.plot(builds, y_stack[3,:], label='Component 4', color='b', marker='o')

plt.xticks(builds)
plt.xlabel('Builds')

handles, labels = ax1.get_legend_handles_labels()
lgd = ax1.legend(handles, labels, loc='upper center', bbox_to_anchor=(1.15,1))
ax1.grid('on')

plt.savefig('smooth_plot.png')

Output: enter image description here


If you want to plot just lines (based on random data that was in your code):

import matplotlib.pyplot as plt
import numpy as np

fnx = lambda : np.random.randint(3, 10, 10)
y = np.row_stack((fnx(), fnx(), fnx(), fnx(), fnx())) 

x = np.arange(10) 
y_stack = np.cumsum(y, axis=0)  

fig = plt.figure(figsize=(11,8))
ax1 = fig.add_subplot(111)

ax1.plot(x, y_stack[0,:], label=1)
ax1.plot(x, y_stack[1,:], label=2)
ax1.plot(x, y_stack[2,:], label=3)
ax1.plot(x, y_stack[3,:], label=4)
ax1.plot(x, y_stack[4,:], label=5)
ax1.legend(loc=2)

colormap = plt.cm.gist_ncar 
colors = [colormap(i) for i in np.linspace(0, 1,len(ax1.lines))]
for i,j in enumerate(ax1.lines):
    j.set_color(colors[i])


plt.savefig('smooth_plot.png')

Output: enter image description here


But if you want stacked line graphs with color filling use this (based on random data that was in your code):

import matplotlib.pyplot as plt
import numpy as np

fnx = lambda : np.random.randint(3, 10, 10)
y = np.row_stack((fnx(), fnx(), fnx(), fnx(), fnx())) 


x = np.arange(10) 
y_stack = np.cumsum(y, axis=0)   

fig = plt.figure(figsize=(11,8))
ax1 = fig.add_subplot(111)

ax1.fill_between(x, 0, y_stack[0,:], facecolor="#CC6666", alpha=0.7)
ax1.fill_between(x, y_stack[0,:], y_stack[1,:], facecolor="#1DACD6", alpha=0.7)
ax1.fill_between(x, y_stack[1,:], y_stack[2,:], facecolor="#6E5160", alpha=0.7)
ax1.fill_between(x, y_stack[2,:], y_stack[3,:], facecolor="#CC6666", alpha=0.7)
ax1.fill_between(x, y_stack[3,:], y_stack[4,:], facecolor="#1DACD6", alpha=0.7)

plt.savefig('smooth_plot.png')

Output: enter image description here

UPDATE:

import matplotlib.pyplot as plt
import numpy as np

builds = np.array([1, 2, 3, 4])
y_stack = np.row_stack(([1, 5, 20, 5], [2, 2, 10, 10], [3, 9, 15, 15], [4, 1, 11, 20])) 

fig = plt.figure(figsize=(11,8))
ax1 = fig.add_subplot(111)

ax1.plot(builds, y_stack[0,:], label='Component 1', color='c', marker='o')
ax1.plot(builds, y_stack[1,:], label='Component 2', color='g', marker='o')
ax1.plot(builds, y_stack[2,:], label='Component 3', color='r', marker='o')
ax1.plot(builds, y_stack[3,:], label='Component 4', color='b', marker='o')

plt.xticks(builds)
plt.xlabel('Builds')

handles, labels = ax1.get_legend_handles_labels()
lgd = ax1.legend(handles, labels, loc='upper center', bbox_to_anchor=(1.15,1))
ax1.grid('on')

plt.savefig('smooth_plot.png')
Erba Aitbayev
  • 4,167
  • 12
  • 46
  • 81
  • thanks for the excellent answer,where can I find details/documentation on `add_subplot,xticks,get_legend_handles_labels() etc., – Ritz Sep 27 '16 at 14:38
  • 1
    http://matplotlib.org/api/figure_api.html, http://matplotlib.org/api/axis_api.html, http://matplotlib.org/examples/ticks_and_spines/ticklabels_demo_rotation.html, http://matplotlib.org/users/legend_guide.html – Erba Aitbayev Sep 27 '16 at 14:42
  • I just found a bug,I was looking at the code for `Using your data provided in screenshot:` ,the line graph looks incorrect,for example the value for build#2-component#1 is `5` in the table I provided ,in the graph it shows a value `2` – Ritz Sep 27 '16 at 14:53
  • my output looks like http://i68.tinypic.com/2v99h87.png,i dont see the legend,its getting trimmed ,how to fix that? – Ritz Sep 27 '16 at 15:13
  • change bbox_to_anchor=(1.15,1) to bbox_to_anchor=(0.15,1)) – Erba Aitbayev Sep 27 '16 at 15:15
  • EXCELLENT,PERFECT ANSWER – Ritz Sep 27 '16 at 15:17