注解
点击 here 下载完整的示例代码
从文件打印数据¶
从文件中绘制数据实际上需要两个步骤。
- 解释文件并加载数据。
- 创建实际绘图。
pyplot.plotfile
试着同时做这两件事。但是每个步骤都有太多可能的变化和参数,所以把它们都压缩到一个函数中是没有意义的。因此, pyplot.plotfile
已弃用。
因此,从文件打印数据的推荐方法是使用专用函数,例如 numpy.loadtxt
或 pandas.read_csv
读取数据。它们更强大更快。然后使用matplotlib绘制获得的数据。
注意 pandas.DataFrame.plot
是Matplotlib的一个方便的包装器,用于创建简单的绘图。
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
import numpy as np
import pandas as pd
使用熊猫¶
接下来是一些如何替换的例子 plotfile
具有 pandas
. 所有示例都需要 pandas.read_csv
先打电话。请注意,您可以直接将文件名用作参数:
msft = pd.read_csv('msft.csv')
下面稍微涉及到 pandas.read_csv
调用只是为了使示例的自动呈现生效:
fname = cbook.get_sample_data('msft.csv', asfileobj=False)
with cbook.get_sample_data('msft.csv') as file:
msft = pd.read_csv(file)
处理日期时,另外打电话 pandas.plotting.register_matplotlib_converters
并使用 parse_dates
的参数 pandas.read_csv
::
.. code-block:: default
pd.plotting.register_matplotlib_转换器()
- 与cbook.get_sample_数据('微软.csv')作为文件:
- msft=pd.read_csv文件(文件,解析日期= ['日期'] )
使用索引¶
# Deprecated:
plt.plotfile(fname, (0, 5, 6))
# Use instead:
msft.plot(0, [5, 6], subplots=True)
使用名称¶
# Deprecated:
plt.plotfile(fname, ('date', 'volume', 'adj_close'))
# Use instead:
msft.plot("Date", ["Volume", "Adj. Close*"], subplots=True)
使用符号表示体积¶
# Deprecated:
plt.plotfile(fname, ('date', 'volume', 'adj_close'),
plotfuncs={'volume': 'semilogy'})
# Use instead:
fig, axs = plt.subplots(2, sharex=True)
msft.plot("Date", "Volume", ax=axs[0], logy=True)
msft.plot("Date", "Adj. Close*", ax=axs[1])
使用符号表示体积(按索引)¶
# Deprecated:
plt.plotfile(fname, (0, 5, 6), plotfuncs={5: 'semilogy'})
# Use instead:
fig, axs = plt.subplots(2, sharex=True)
msft.plot(0, 5, ax=axs[0], logy=True)
msft.plot(0, 6, ax=axs[1])
单个子批次¶
# Deprecated:
plt.plotfile(fname, ('date', 'open', 'high', 'low', 'close'), subplots=False)
# Use instead:
msft.plot("Date", ["Open", "High", "Low", "Close"])
使用条形图表示音量¶
# Deprecated:
plt.plotfile(fname, (0, 5, 6), plotfuncs={5: "bar"})
# Use instead:
fig, axs = plt.subplots(2, sharex=True)
axs[0].bar(msft.iloc[:, 0], msft.iloc[:, 5])
axs[1].plot(msft.iloc[:, 0], msft.iloc[:, 6])
fig.autofmt_xdate()
使用numpy¶
fname2 = cbook.get_sample_data('data_x_x2_x3.csv', asfileobj=False)
with cbook.get_sample_data('data_x_x2_x3.csv') as file:
array = np.loadtxt(file)
标记,如果csv文件中没有名称¶
# Deprecated:
plt.plotfile(fname2, cols=(0, 1, 2), delimiter=' ',
names=['$x$', '$f(x)=x^2$', '$f(x)=x^3$'])
# Use instead:
fig, axs = plt.subplots(2, sharex=True)
axs[0].plot(array[:, 0], array[:, 1])
axs[0].set(ylabel='$f(x)=x^2$')
axs[1].plot(array[:, 0], array[:, 2])
axs[1].set(xlabel='$x$', ylabel='$f(x)=x^3$')
每个图形有多个文件¶
# For simplicity of the example we reuse the same file.
# In general they will be different.
fname3 = fname2
# Depreacted:
plt.plotfile(fname2, cols=(0, 1), delimiter=' ')
plt.plotfile(fname3, cols=(0, 2), delimiter=' ',
newfig=False) # use current figure
plt.xlabel(r'$x$')
plt.ylabel(r'$f(x) = x^2, x^3$')
# Use instead:
fig, ax = plt.subplots()
ax.plot(array[:, 0], array[:, 1])
ax.plot(array[:, 0], array[:, 2])
ax.set(xlabel='$x$', ylabel='$f(x)=x^3$')
plt.show()
关键词:matplotlib代码示例,codex,python plot,pyplot Gallery generated by Sphinx-Gallery