我更喜欢使用matplotlibOOP风格:
f, axarr = plt.subplots(2, sharex=True)
axarr[0].plot(...)
axarr[1].plot(...)
Run Code Online (Sandbox Code Playgroud)
这样可以更轻松地跟踪多个图形和子图.
问题:如何使用seaborn这种方式?或者,如何将此示例更改为OOP样式?如何将seaborn绘图函数描述为lmplot哪个Figure或哪个Axes绘图?
%pylab inline
import pandas as pd
import numpy as np
import matplotlib as mpl
import seaborn as sns
typessns = pd.DataFrame.from_csv('C:/data/testesns.csv', index_col=False, sep=';')
mpl.rc("figure", figsize=(45, 10))
sns.factorplot("MONTH", "VALUE", hue="REGION", data=typessns, kind="box", palette="OrRd");
Run Code Online (Sandbox Code Playgroud)

我总是得到一个小尺寸的数字,无论我在figsize指定的大小...如何解决它?
对于情节
sns.countplot(x="HostRamSize",data=df)
Run Code Online (Sandbox Code Playgroud)
我将下面的图表与x轴标签混合在一起,我该如何避免这种情况?我应该更改图表的大小来解决这个问题吗?
我用seaborn创建了一个热图
df1.index = pd.to_datetime(df1.index)
df1 = df1.set_index('TIMESTAMP')
df1 = df1.resample('30min').mean()
ax = sns.heatmap(df1.iloc[:, 1:6:], annot=True, linewidths=.5)
Run Code Online (Sandbox Code Playgroud)
但问题是,当数据帧中有大量数据时,热图太小而且内部的值开始不清楚,如附图中所示.
编辑
我尝试:
df1.index = pd.to_datetime(df1.index)
fig, ax = plt.subplots(figsize=(10,10)) # Sample figsize in inches
sns.heatmap(df1.iloc[:, 1:6:], annot=True, linewidths=.5, ax=ax)
df1 = df1.set_index('TIMESTAMP')
df1 = df1.resample('1d').mean()
ax = sns.heatmap(df1.iloc[:, 1:6:], annot=True, linewidths=.5)
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
KeyError Traceback (most recent call last)
C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\indexes\base.py in get_loc(self, key, method, tolerance)
1944 try:
-> 1945 return self._engine.get_loc(key)
1946 except KeyError:
pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4154)()
pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:4018)()
pandas\hashtable.pyx …Run Code Online (Sandbox Code Playgroud) 我在使用Seaborn增加我的情节数字时遇到了麻烦.我正在使用sns.pairplot来绘制数据帧的列.
%matplotlib inline
plt.rcParams['figure.figsize']=10,10
columns=list(df.columns.values)
g=sns.pairplot(df, kind='reg', x_vars=columns,y_vars = ['Column 1'])
Run Code Online (Sandbox Code Playgroud)
数据填充的数据很好,但数字太小.我认为plot.rCParams ['figure.figsize']会控制数字的大小,但它似乎没有生效.我尝试了一些来自在线主板的不同建议,但似乎没有任何效果.
所有,
我正在尝试使用subplots函数和Seaborn库来绘制两个Factorplots 。我可以使用下面的代码分别绘制两个图。但是,seaborn正在实际图下方生成额外的图(请参见下图)。有没有一种方法可以避免seaborn生成多余的空图?我试图plt.close摆脱地块,但不幸的是,它只是关闭了1个地块。此外,我试图将图例移出地块并在地块旁边显示图例。有没有简单的方法可以做到这一点。我尝试legend_out使用seaborn包装提供的产品,但是没有用。
我的代码:
f,axes=plt.subplots(1,2,figsize=(8,4))
sns.factorplot(x="borough", y="pickups", hue="borough", kind='bar', data=n, size=4, aspect=2,ax=axes[0])
sns.factorplot(x="borough", y="pickups", hue="borough", kind='bar', data=low_pickups, size=4, aspect=2,ax=axes[1])
plt.close(2)
plt.show()
Run Code Online (Sandbox Code Playgroud)
以上代码的输出:
注意:我是python的新手,请在您的代码中提供解释。
数据帧的输出
#n dataframe
{'borough': {0: 'Bronx', 1: 'Brooklyn', 2: 'EWR', 3: 'Manhattan', 4: 'Queens', 5: 'Staten Island', 6: 'Unknown'}, 'pickups': {0: 50.66705042597283, 1: 534.4312687082662, 2: 0.02417683628827999, 3: 2387.253281142068, 4: 309.35482385447847, 5: 1.6018880957863229, 6: 2.0571804140650674}}
#low_pickups dataframe
{'borough': {2: 'EWR', 5: 'Staten Island', 6: 'Unknown'}, 'pickups': …Run Code Online (Sandbox Code Playgroud) 我正在使用带有数据框的 seaborn FacetGrid 制作绘图:df。由于色调功能,我正在使用 sns。
见代码:
import seaborn as sns
import matplotlib.pyplot as plt
grid = sns.FacetGrid(df,hue='var')
grid.map(plt.scatter, x, y).add_legend()
grid.set(ylim=ylim,xlim=xlim)
plt.show()
Run Code Online (Sandbox Code Playgroud)
通常我会使用以下方法访问 figsize:
fig, ax = plt.subplots(figsize=(20, 10))
Run Code Online (Sandbox Code Playgroud)
或者
plt.figure(figsize=(20,10))
Run Code Online (Sandbox Code Playgroud)
但是现在,因为我使用的是 seaborn,所以我不知道如何访问图形对象。
如何访问图形对象以更改绘图的大小?
我正在尝试更改我正在构建的直方图的图形大小.我收到错误:
distplot() got an unexpected keyword argument 'figsize'
Run Code Online (Sandbox Code Playgroud)
我试图运行的代码是这样的:
sns.distplot(MSoft['pct_change'].dropna(), bins=100, color='magenta', figsize=(20,8))
Run Code Online (Sandbox Code Playgroud) 我已经在这里尝试了尽可能多的解决方案,但我对此并不走运。我不确定是不是因为我的一些设置,但我无法重塑我的 Seaborn 计数图。这是我绘制图形的代码:
sns.set_style('whitegrid')
sns.set(font_scale=1.3)
sns.countplot(x=df['QuarterYear'], hue=df['Modifier'])
ax = plt.gca()
for p in ax.patches:
ax.text(p.get_x() + p.get_width()/2., p.get_height(), '%d' % int(p.get_height()),
fontsize=12, color='black', ha='center', va='bottom')
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
Run Code Online (Sandbox Code Playgroud)
我还在同一代码块中编辑图例并标记我的 countplot 列。
我正在使用 Jupyter Notebook %inline。如果有人能解释我所缺少的东西,那就太好了。我已经尝试了许多这些解决方案的变体,但无济于事。
如何在 Seaborn 中更改 regplot 的绘图大小?
任何帮助,将不胜感激。感谢您的时间!