如何使用Matplotlib创建折线图。

9
我正在尝试为屏幕截图中显示的示例数据创建折线图。 我进行了相当多的谷歌搜索,并查看了下面的一些链接,并尝试使用matplotlib,但我无法获得所需的输出,如下面的折线图(屏幕截图)所示,是否有人可以提供一个样本参考来入门?如何使用下面显示的示例输入获取折线图?

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

linechart

代码:

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')
1个回答

20

使用您在屏幕截图中提供的数据:

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')

输出: 在此输入图像描述


如果您只想绘制线条(基于代码中的随机数据):

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')

输出: 在这里输入图像描述


但是,如果您想要带有颜色填充的堆叠折线图,请使用以下代码(基于您代码中的随机数据):

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')

输出: 这里输入图片描述

更新:

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')

感谢您的出色回答,请问我在哪里可以找到add_subplot,xticks,get_legend_handles_labels()等的详细说明/文档? - Ritz
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
我刚刚发现了一个错误,我正在查看“使用您在屏幕截图中提供的数据:”的代码,线图看起来不正确。例如,在我提供的表格中,构建#2-组件#1的值为“5”,但在图表中显示为“2”。 - Ritz
我的输出看起来像http://i68.tinypic.com/2v99h87.png,我看不到图例,它被裁剪了,怎么解决? - Ritz
将 bbox_to_anchor=(1.15,1) 更改为 bbox_to_anchor=(0.15,1))。 - Erba Aitbayev
卓越的,完美的答案 - Ritz

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