以下是使用matplotlib生成图的python脚本。使用matplotlib生成平滑折线图

#!/usr/bin/python

import matplotlib.pyplot as plt

import time

import numpy as np

from scipy.interpolate import spline

# Local variables

x = []

y = []

# Open the data file for reading lines

datafile = open('testdata1.txt', 'r')

sepfile = datafile.read().split('\n')

datafile.close()

# Create a canvas to place the subgraphs

canvas = plt.figure()

rect = canvas.patch

rect.set_facecolor('white')

# Iterate through the lines and parse them

for datapair in sepfile:

if datapair:

xypair = datapair.split(' ')

x.append(int(xypair[1]))

y.append(int(xypair[3]))

# Define the matrix of 1x1 to place subplots

# Placing the plot1 on 1x1 matrix, at pos 1

sp1 = canvas.add_subplot(1,1,1, axisbg='w')

sp1.plot(x, y, 'red', linewidth=2)

# Colorcode the tick tabs

sp1.tick_params(axis='x', colors='red')

sp1.tick_params(axis='y', colors='red')

# Colorcode the spine of the graph

sp1.spines['bottom'].set_color('r')

sp1.spines['top'].set_color('r')

sp1.spines['left'].set_color('r')

sp1.spines['right'].set_color('r')

# Put the title and labels

sp1.set_title('matplotlib example 1', color='red')

sp1.set_xlabel('matplot x label', color='red')

sp1.set_ylabel('matplot y label', color='red')

# Show the plot/image

plt.tight_layout()

plt.grid(alpha=0.8)

plt.savefig("example6.eps")

plt.show()

它生成的情节一样

HOxjN.jpg

有人可以请建议我的变化?

2014-09-13

AnilJ

+1

你没有做视频建议的事情。例如,看看他在3点20分左右开始创建平滑版本数据的部分。 –

2014-09-13 17:51:55

+0

(a)当你试图实现一个数值方法时,你没有定义x_smooth和y_smooth(b),避免在第一个初始实现中美化绘图,即保持图形简单或默认 –

2014-09-13 18:02:43

+0

关联示例,我的数组x []和y []本质上是动态的(我正在从文件读取数据)。我不知道如何以相同的方式使用np.array。在视频示例中,它们是静态使用的。 –

2014-09-13 18:12:46

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐