[英]Dataframe Bar plot with Seaborn
我正在尝试从带有日期时间索引的 DataFrame 创建一个条形 plot。 这是一个示例工作代码:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
sns.set()
index = pd.date_range('2012-01-01', periods=48, freq='M')
data = np.random.randint(100, size = (len(index),1))
df = pd.DataFrame(index=index, data=data, columns=['numbers'])
fig, ax = plt.subplots()
ax.bar(df.index, df['numbers'])
如您所见,白条在背景方面无法很好地区分(为什么?)。
我尝试使用:
df['numbers'].plot(kind='bar')
import matplotlib.ticker as ticker
ticklabels = df.index.strftime('%Y-%m')
ax.xaxis.set_major_formatter(ticker.FixedFormatter(ticklabels))
但是通过这种方式,我失去了 6 个月间隔的自动 xticks 标签(和网格)。
任何想法?
您可以更改样式:
import matplotlib.pyplot as plt
index = pd.date_range('2012-01-01', periods=48, freq='M')
data = np.random.randint(100, size = (len(index),1))
df = pd.DataFrame(index=index, data=data, columns=['numbers'])
plt.figure(figsize=(12, 5))
plt.style.use('default')
plt.bar(df.index,df['numbers'],color="red")
您实际上并未使用seaborn
。 将ax.bar(df.index, df['numbers'])
替换为
sns.barplot(df.index, df['numbers'], ax=ax)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.