我对这段代码的工作原理有点困惑:
fig, axes = plt.subplots(nrows=2, ncols=2)
plt.show()
Run Code Online (Sandbox Code Playgroud)
在这种情况下,无花果轴如何工作?它有什么作用?
为什么这不能做同样的事情:
fig = plt.figure()
axes = fig.subplots(nrows=2, ncols=2)
Run Code Online (Sandbox Code Playgroud)
谢谢
在ipython Notebook中,首先创建一个pandas Series对象,然后通过调用实例方法.hist(),浏览器显示该图.
我想知道如何将这个数字保存到文件(我的意思是不是通过右键单击并另存为,但脚本中需要的命令).
代码
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
print type(ax)
Run Code Online (Sandbox Code Playgroud)
给出输出
<class 'matplotlib.axes.AxesSubplot'>
Run Code Online (Sandbox Code Playgroud)
然后是代码
import matplotlib.axes
matplotlib.axes.AxesSubplot
Run Code Online (Sandbox Code Playgroud)
引发例外
AttributeError: 'module' object has no attribute 'AxesSubplot'
Run Code Online (Sandbox Code Playgroud)
总而言之,有一个类matplotlib.axes.AxesSubplot
,但模块matplotlib.axes
没有属性AxesSubplot
.到底是怎么回事?
我正在使用Matplotlib 1.1.0和Python 2.7.3.
尝试使用'pandas.core.series.Series'对象中的'pandas.DataFrame.plot'创建的绘图图像时:
%matplotlib inline
type(class_counts) # pandas.core.series.Series
class_counts.plot(kind='bar', figsize=(20, 16), fontsize=26)
Run Code Online (Sandbox Code Playgroud)
像这样:
import matplotlib.pyplot as plt
plt.savefig('figure_1.pdf', dpi=300)
Run Code Online (Sandbox Code Playgroud)
导致空的pdf文件.如何保存用'pandas.DataFrame.plot'创建的图像?
I am attempting to create a dataframe histogram and save it as a file.
Here is my code:
ax=df.hist('ColumnName')
fig=ax.get_figure()
fig.savefig('pictureName.png', dpi=100,
bbox_inches='tight')
Run Code Online (Sandbox Code Playgroud)
The first line works fine; however, the second line returns an error: AttributeError: 'numpy.ndarray' object has no attribute 'get_figure'.
Because this question shows the get_figure() being applied to series.hist(), I have also tried using ax=df['ColumnName'].hist()
, which successfully produced a histogram but led to the same error message when I attempted to implement get_figure().
正如在这个其他问题中所建议的那样,通常我会跳过get_figure()和fig.savefig(),而不是选择plt.savefig,但我正在制作多个数字.根据我的经验,plt.savefig()在保存多个数字时是不可靠的,而是多次保存一个数字,即使我在每个数字创建和保存后使用fig.close(). …
我是新来的matplotlib
,正在lim步。就是说,我没有找到这个问题的明显答案。
我有一个散点图,希望按组进行着色,看起来像通过循环绘制是滚动的方式。
这是我的可复制示例,基于上面的第一个链接:
import matplotlib.pyplot as plt
import pandas as pd
from pydataset import data
df = data('mtcars').iloc[0:10]
df['car'] = df.index
fig, ax = plt.subplots(1)
plt.figure(figsize=(12, 9))
for ind in df.index:
ax.scatter(df.loc[ind, 'wt'], df.loc[ind, 'mpg'], label=ind)
ax.legend(bbox_to_anchor=(1.05, 1), loc=2)
# plt.show()
# plt.savefig('file.png')
Run Code Online (Sandbox Code Playgroud)
取消注释会plt.show()
产生我想要的东西:
到处搜索,看起来就像plt.savefig()
是保存文件的方式。如果我重新注释掉plt.show()
并plt.savefig()
改为运行,则会得到空白的白色图片。这个问题,暗示这是由于show()
之前致电引起的savefig()
,但我已将其完全注释掉了。另一个问题有一条评论,建议我可以ax
直接保存该对象,但这切断了我的图例:
相同的问题也有替代方法使用fig.savefig()
。我得到同样的传说。
有这个问题,这似乎有关,但我不密谋DataFrame
直接,所以我不知道如何应用答案(这里dtf …
python ×5
matplotlib ×4
pandas ×3
figure ×1
histogram ×1
numpy ×1
plot ×1
python-2.7 ×1
subplot ×1