如何使用matplotlib创建具有3个垂直堆叠的绘图的文件

2024-09-27 23:21:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图用fig, ax = plt.subplot(3,1)将3个图表绘制在彼此的顶部。数据存储在数据帧中。我的代码如下:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

pca_factors = pd.DataFrame(np.random.randn(10,3),columns = ['Outright','Curve','Convexity'])

fig,axes = plt.subplots(3,1)
pca_factors.Outright.plot(ax=axes[0]); axes[0].set_title('Outright')
pca_factors.Curve.plot(ax=axes[1]); axes[1].set_title('Curve'); plt.axhline(linewidth=2, color = 'g')
pca_factors.Convexity.plot(ax=axes[2]); axes[2].set_title('Convexity'); plt.axhline(linewidth=2, color = 'g')
fig.tight_layout()
fig.savefig('convexity.png')

但是,我遇到以下错误消息:

Traceback (most recent call last):
  File "pcahedge1.py", line 193, in <module>
    pca_factors.Convexity.plot(ax=axes[2]); axes[2].set_title('Convexity'); plt.axhline(linewidth=2, color = 'g')
  File "/home/ali/conda/envs/AS6/lib/python2.7/site-packages/pandas/tools/plotting.py", line 3563, in __call__
    **kwds)
  File "/home/ali/conda/envs/AS6/lib/python2.7/site-packages/pandas/tools/plotting.py", line 2642, in plot_series
    **kwds)
  File "/home/ali/conda/envs/AS6/lib/python2.7/site-packages/pandas/tools/plotting.py", line 2438, in _plot
    plot_obj.generate()
  File "/home/ali/conda/envs/AS6/lib/python2.7/site-packages/pandas/tools/plotting.py", line 1031, in generate
    self._post_plot_logic_common(ax, self.data)
  File "/home/ali/conda/envs/AS6/lib/python2.7/site-packages/pandas/tools/plotting.py", line 1159, in _post_plot_logic_common
    fontsize=self.fontsize)
  File "/home/ali/conda/envs/AS6/lib/python2.7/site-packages/pandas/tools/plotting.py", line 1207, in _apply_axis_properties
    labels = axis.get_majorticklabels() + axis.get_minorticklabels()
  File "/home/ali/conda/envs/AS6/lib/python2.7/site-packages/matplotlib/axis.py", line 1161, in get_majorticklabels
    ticks = self.get_major_ticks()
  File "/home/ali/conda/envs/AS6/lib/python2.7/site-packages/matplotlib/axis.py", line 1290, in get_major_ticks
    numticks = len(self.get_major_locator()())
  File "/home/ali/conda/envs/AS6/lib/python2.7/site-packages/matplotlib/dates.py", line 1006, in __call__
    self.refresh()
  File "/home/ali/conda/envs/AS6/lib/python2.7/site-packages/matplotlib/dates.py", line 1026, in refresh
    dmin, dmax = self.viewlim_to_dt()
  File "/home/ali/conda/envs/AS6/lib/python2.7/site-packages/matplotlib/dates.py", line 770, in viewlim_to_dt
    return num2date(vmin, self.tz), num2date(vmax, self.tz)
  File "/home/ali/conda/envs/AS6/lib/python2.7/site-packages/matplotlib/dates.py", line 419, in num2date
    return _from_ordinalf(x, tz)
  File "/home/ali/conda/envs/AS6/lib/python2.7/site-packages/matplotlib/dates.py", line 271, in _from_ordinalf
    dt = datetime.datetime.fromordinal(ix).replace(tzinfo=UTC)
ValueError: ordinal must be >= 1

有人能帮我弄清楚吗


Tags: inpyselfhomeplotlibpackagesline

热门问题