与这个问题非常相似,但不同之处在于我的身材可以达到需要的大小.
我需要在matplotlib中生成一堆垂直堆叠的图.结果将使用figsave保存并在网页上查看,因此我不关心最终图像的高度,只要子图间隔开,这样它们就不会重叠.
无论我有多大的数字,子图总是似乎重叠.
我的代码目前看起来像
import matplotlib.pyplot as plt
import my_other_module
titles, x_lists, y_lists = my_other_module.get_data()
fig = plt.figure(figsize=(10,60))
for i, y_list in enumerate(y_lists):
plt.subplot(len(titles), 1, i)
plt.xlabel("Some X label")
plt.ylabel("Some Y label")
plt.title(titles[i])
plt.plot(x_lists[i],y_list)
fig.savefig('out.png', dpi=100)
Run Code Online (Sandbox Code Playgroud) import numpy as np
y = np.array(((1,2,3),(4,5,6),(7,8,9)))
OUTPUT:
print(y.flatten())
[1 2 3 4 5 6 7 8 9]
print(y.ravel())
[1 2 3 4 5 6 7 8 9]
Run Code Online (Sandbox Code Playgroud)
两个函数都返回相同的列表.那么两个不同功能执行相同工作的需求是什么.
我有一些Pandas DataFrames共享相同的值规模,但具有不同的列和索引.在调用时df.plot(),我会获得单独的绘图图像.我真正想要的是将它们全部放在与次要情节相同的情节中,但遗憾的是我没有想出一个如何并且非常欣赏一些帮助的解决方案.
我在matplotlib上找到了以下示例
import numpy as np
import matplotlib.pyplot as plt
x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)
plt.subplot(2, 1, 1)
plt.plot(x1, y1, 'ko-')
plt.title('A tale of 2 subplots')
plt.ylabel('Damped oscillation')
plt.subplot(2, 1, 2)
plt.plot(x2, y2, 'r.-')
plt.xlabel('time (s)')
plt.ylabel('Undamped')
plt.show()
Run Code Online (Sandbox Code Playgroud)
我的问题是:我需要改变什么,让这些情节并排?
我正在使用pandas从数据框生成一个图,我想将其保存到文件中:
dtf = pd.DataFrame.from_records(d,columns=h)
fig = plt.figure()
ax = dtf2.plot()
ax = fig.add_subplot(ax)
fig.savefig('~/Documents/output.png')
Run Code Online (Sandbox Code Playgroud)
似乎最后一行,使用matplotlib的savefig,应该可以解决问题.但该代码产生以下错误:
Traceback (most recent call last):
File "./testgraph.py", line 76, in <module>
ax = fig.add_subplot(ax)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/figure.py", line 890, in add_subplot
assert(a.get_figure() is self)
AssertionError
Run Code Online (Sandbox Code Playgroud)
或者,尝试直接在绘图上调用savefig也会出错:
dtf2.plot().savefig('~/Documents/output.png')
File "./testgraph.py", line 79, in <module>
dtf2.plot().savefig('~/Documents/output.png')
AttributeError: 'AxesSubplot' object has no attribute 'savefig'
Run Code Online (Sandbox Code Playgroud)
我想我需要以某种方式将plot()返回的子图添加到图中以便使用savefig.我也想知道如果这或许与该做魔术的AxesSubPlot类的后面.
编辑:
以下作品(没有提出任何错误),但留下了一个空白页面图像....
fig = plt.figure()
dtf2.plot()
fig.savefig('output.png')
Run Code Online (Sandbox Code Playgroud) 我试图绘制两个显示击球和保龄球计数的计票图.我尝试了以下代码:
l=['batting_team','bowling_team']
for i in l:
sns.countplot(high_scores[i])
mlt.show()
Run Code Online (Sandbox Code Playgroud)
但通过使用这个,我得到两个一个在另一个下面的情节.我如何让他们并排订购?
我正在尝试使用此代码并排绘制两个分布图
fig,(ax1,ax2) = plt.subplots(1,2)
sns.displot(x =X_train['Age'], hue=y_train, ax=ax1)
sns.displot(x =X_train['Fare'], hue=y_train, ax=ax2)
Run Code Online (Sandbox Code Playgroud)
它返回以下结果(两个空的子图,后跟一个分布在两行上的图)-
如果我用 violinplot 尝试相同的代码,它会按预期返回结果
fig,(ax1,ax2) = plt.subplots(1,2)
sns.violinplot(y_train, X_train['Age'], ax=ax1)
sns.violinplot(y_train, X_train['Fare'], ax=ax2)
Run Code Online (Sandbox Code Playgroud)
为什么 displot 返回不同类型的输出,我该怎么做才能在同一行上输出两个图?
我有一个dt:
>>> dt
sales mg
ID 600519 600809 600519 600809
RPT_Date
20060331 13.5301 5.8951 9.4971 3.0408
20060630 6.6048 2.4081 4.3088 1.4040
20060930 12.3889 3.6053 9.1455 2.0754
20061231 16.5100 3.3659 12.4682 1.8810
20070331 15.8754 5.9129 11.5833 3.6736
20070630 10.4155 3.5759 7.8966 2.2812
20070930 18.2929 3.5280 14.3552 2.1584
20071231 27.7905 5.4510 23.7820 3.2568
Run Code Online (Sandbox Code Playgroud)
并使用pandas函数plot创建一个barplot对象
>>> fig = dt.plot(kind='bar', use_index=True)
>>> fig
<matplotlib.axes.AxesSubplot object at 0x0B387150>
Run Code Online (Sandbox Code Playgroud)
但是我想要一个<matplotlib.figure.Figure object>intsead传递给其他函数,就像下面的对象类型一样:
>>> plt.figure()
<matplotlib.figure.Figure object at 0x123A6910>
Run Code Online (Sandbox Code Playgroud)
所以,我怎么能转换<matplotlib.axes.AxesSubplot object> …
我已经构建了一组饼图,其中有一些帮助将图像插入饼图切片 我的图表看起来很精彩,现在我需要将它们中的所有6个放在一个2x3图中,在共享的x和y轴上有共同的刻度线.为了开始,我正在看子图,并认为我可以让它工作.我下载了一些例子并开始尝试一些事情.
f, (a) = (plt.subplots(nrows=1, ncols=1, sharex=True, sharey=True))#,
#squeeze=False, subplot_kw=None, gridspec_kw=None))
print(type(f),'\n',type(a),'\n')#,type(b))
Run Code Online (Sandbox Code Playgroud)
收益率:
class'matplotlib.figure.Figure'
class'matplotlib.axes._subplots.AxesSubplot'
而:
f, (a) = (plt.subplots(nrows=1, ncols=1, sharex=True, sharey=True, squeeze=False, subplot_kw=None, gridspec_kw=None))
print(type(f),'\n',type(a),'\n')#,type(b))
Run Code Online (Sandbox Code Playgroud)
收益:
class'matplotlib.figure.Figure'
'numpy.ndarray'
当我这样做:
f, (a,b) = (plt.subplots(nrows=2, ncols=1, sharex=True, sharey=True, squeeze=False, subplot_kw=None, gridspec_kw=None))
print(type(f),'\n',type(a),'\n',type(b))
Run Code Online (Sandbox Code Playgroud)
我得到了类似的结果,但是如果nrows = 1且ncols = 2我得到一个错误:
f, (a,b) = (plt.subplots(nrows=1, ncols=2, sharex=True, sharey=True, squeeze=False, subplot_kw=None, gridspec_kw=None))
print(type(f),'\n',type(a),'\n',type(b))
Run Code Online (Sandbox Code Playgroud)
ValueError:没有足够的值来解包(预期2,得1)
但是这个:
f, (a , b) = (
plt.subplots(nrows=1, ncols=2, sharex=True, sharey=True))#,
#squeeze=False, subplot_kw=None, gridspec_kw=None))
print(type(f),'\n',type(a),'\n',type(b))
Run Code Online (Sandbox Code Playgroud)
给出'matplotlib.figure.Figure'类
class'matplotlib.axes._subplots.AxesSubplot'
class'matplotlib.axes._subplots.AxesSubplot'
为什么它或阵列或轴,以及为什么2X1工作和1X2不工作?我希望天堂高,我可以更好地理解文档.谢谢.