我想为seaborn热图添加一个标题.使用Pandas和iPython Notebook
代码如下,
a1_p = a1.pivot_table( index='Postcode', columns='Property Type', values='Count', aggfunc=np.mean, fill_value=0)
sns.heatmap(a1_p, cmap="YlGnBu")
Run Code Online (Sandbox Code Playgroud)
数据非常简单:
In [179]: a1_p
Out [179]:
Property Type Flat Terraced house Unknown
Postcode
E1 11 0 0
E14 12 0 0
E1W 6 0 0
E2 6 0 0
Run Code Online (Sandbox Code Playgroud) 我正在绘制两个带有seaborn导入的Pandas的数据系列.理想情况下,我希望在左右y轴之间共享水平网格线,但我觉得这很难做到.
作为妥协,我想一起删除网格线.但是,以下代码会生成辅助y轴的水平网格线.
import pandas as pd
import numpy as np
import seaborn as sns
data = pd.DataFrame(np.cumsum(np.random.normal(size=(100,2)),axis=0),columns=['A','B'])
data.plot(secondary_y=['B'],grid=False)
Run Code Online (Sandbox Code Playgroud)

当使用seaborn barplot时,我可以指定一个alpha,它使条形半透明.但是,当我用Seaborn regplot尝试这个时,我得到一个错误,说这是一个意想不到的争论.
我在线阅读文档并没有找到太多,有人能指出我正确的方向......
我有一个带有名为"AXLES"的列的Pandas DataFrame,它可以取3-12之间的整数值.我试图使用Seaborn的countplot()选项来实现以下情节:
下面的代码给出了下面的图表,其中包含实际计数,但我找不到将它们转换为频率的方法.我可以使用频率,df.AXLES.value_counts()/len(df.index)但我不知道如何将这些信息插入Seaborn countplot().
我还找到了注释的解决方法,但我不确定这是否是最佳实现.
任何帮助,将不胜感激!
谢谢
plt.figure(figsize=(12,8))
ax = sns.countplot(x="AXLES", data=dfWIM, order=[3,4,5,6,7,8,9,10,11,12])
plt.title('Distribution of Truck Configurations')
plt.xlabel('Number of Axles')
plt.ylabel('Frequency [%]')
for p in ax.patches:
ax.annotate('%{:.1f}'.format(p.get_height()), (p.get_x()+0.1, p.get_height()+50))
Run Code Online (Sandbox Code Playgroud)
我使用以下代码更接近我需要的东西,使用Pandas的条形图,抛弃Seaborn.感觉就像我使用了很多变通方法,并且必须有一种更简单的方法来实现它.这种方法的问题:
orderPandas的条形图功能中没有关键字,因为Seaborn的countplot()有,所以我不能像在countplot()中那样绘制3-12中的所有类别.即使该类别中没有数据,我也需要显示它们.由于某种原因,辅助y轴会使条形和注释混乱(请参阅在文本和条形图上绘制的白色网格线).
plt.figure(figsize=(12,8))
plt.title('Distribution of Truck Configurations')
plt.xlabel('Number of Axles')
plt.ylabel('Frequency [%]')
ax = (dfWIM.AXLES.value_counts()/len(df)*100).sort_index().plot(kind="bar", rot=0)
ax.set_yticks(np.arange(0, 110, 10))
ax2 = ax.twinx()
ax2.set_yticks(np.arange(0, 110, 10)*len(df)/100)
for p in ax.patches:
ax.annotate('{:.2f}%'.format(p.get_height()), (p.get_x()+0.15, p.get_height()+1))
Run Code Online (Sandbox Code Playgroud)你可以轻松制作的最酷的东西之一seaborn是boxplot+ stripplot组合:
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
tips = sns.load_dataset("tips")
sns.stripplot(x="day", y="total_bill", hue="smoker",
data=tips, jitter=True,
palette="Set2", split=True,linewidth=1,edgecolor='gray')
sns.boxplot(x="day", y="total_bill", hue="smoker",
data=tips,palette="Set2",fliersize=0)
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.);
Run Code Online (Sandbox Code Playgroud)
不幸的是,正如你在上面看到的那样,它产生了双重传奇,一个用于boxplot,一个用于stripplot.显然,它看起来很荒谬和多余.但我似乎无法找到摆脱stripplot传说的方法而只留下boxplot传奇.可能,我可以以某种方式删除项目plt.legend,但我在文档中找不到它.
这似乎是一个微不足道的问题,但我一直在寻找一段时间,似乎无法找到答案.它似乎也应该是这些软件包的标准部分.有没有人知道是否有标准的方法在seaborn中的分布图之间包含统计注释?
例如,在两个盒子或者swarmplots之间?
我用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 Heatmap:
import pandas as pd
data = pd.DataFrame({'x':(1,2,3,4),'y':(1,2,3,4),'z':(14,15,23,2)})
sns.heatmap(data.pivot_table(index='y', columns='x', values='z'))
Run Code Online (Sandbox Code Playgroud)
如何在颜色条中添加标签?
鉴于以下计数图,我如何将百分比放在栏杆上?
import seaborn as sns
sns.set(style="darkgrid")
titanic = sns.load_dataset("titanic")
ax = sns.countplot(x="class", hue="who", data=titanic)
Run Code Online (Sandbox Code Playgroud)
例如,对于"第一",我想要总的第一人/总第一,总第一女人/总第一,总第一个孩子/总第一个在他们各自的酒吧之上.
如果我的解释不清楚,请告诉我.
谢谢!
我有以下代码,通过seaborn创建一个表和一个barplot.
#Building a dataframe grouped by the # of Engagement Types
sales_type = sales.groupby('# of Engagement Types').sum()
#Calculating the % of people who bought the course by # engagement types
sales_type['% Sales per Participants'] = round(100*(sales_type['Sales'] / sales_type['Had an Engagement']), 2)
#Calculating the # of people who didn't have any engagements
sales_type.set_value(index=0, col='Had an Engagement', value=sales[sales['Had an Engagement']==0].count()['Sales'])
#Calculating the % of sales for those who didn't have any engagements
sales_type.set_value(index=0, col='% Sales per Participants',
value=round(100 * (sales_type.ix[0, 'Sales'] / …Run Code Online (Sandbox Code Playgroud) seaborn ×10
python ×9
matplotlib ×5
pandas ×4
heatmap ×2
legend ×1
plot ×1
python-3.x ×1
statistics ×1