使用matplotlib

时间:2018-04-21 19:35:57

标签: python python-3.x matplotlib

我有一个很棒的人物,在使用jupyter笔记本时显得非常出色,但不会像笔记本中出现那样保存 - 主要问题是最后一个xticklabel总是丢失!我尝试了很多方法,包括使用tight_layout,使用bbox_inches='tight'保存,并使用subplot_adjust调整间距 - 似乎没有任何效果!

这是它在juypyter实验室中出现的方式:

enter image description here

这是数字保存的方式:

enter image description here

以下是重新创建问题的代码段。

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from matplotlib.dates import date2num, DateFormatter
from datetime import timedelta

ts = pd.Series(np.random.randn(len(rng)), index=pd.date_range('1/1/2011', periods=24*60, freq='min'), name='value')

df = pd.DataFrame(ts, columns=['value'])

fig, ax = plt.subplots(1, figsize=(8, 4))
ax.plot(df.value)


ticks = ax.get_xticks()
ax.set_xticks(np.linspace(date2num(df.index[0]), date2num(df.index[0] + timedelta(hours=24)), 5))
ax.set_xticks(np.linspace(date2num(df.index[0]), date2num(df.index[0] + timedelta(hours=24)), 25), minor=True)
ax.xaxis.set_major_formatter(DateFormatter("%I %p"))

ax.set_xlim(df.index[0], df.index[-1])

fig.savefig("tmp.png", bbox_inches='tight')

如何使用matplotlib可靠地保存数字??

我正在使用matplotlib v2.2.2和MacOSX后端。

0 个答案:

没有答案