Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 仅在箱子刻度上显示xticks_Python_Matplotlib_Histogram_Xticks - Fatal编程技术网

Python 仅在箱子刻度上显示xticks

Python 仅在箱子刻度上显示xticks,python,matplotlib,histogram,xticks,Python,Matplotlib,Histogram,Xticks,我有一个带有日期时间数据的数据框,它的列显示了不同假日的天数。我想创建该数据的直方图,并尝试了以下代码: holiday_features = [name for name in df.columns if 'DAYS FROM' in name] for feature in holiday_features: plt.hist(df[feature], bins=12) plt.title(feature) plt.xticks(rotation=60) pl

我有一个带有日期时间数据的数据框,它的列显示了不同假日的天数。我想创建该数据的直方图,并尝试了以下代码:

holiday_features = [name for name in df.columns if 'DAYS FROM' in name]
for feature in holiday_features:
    plt.hist(df[feature], bins=12)
    plt.title(feature)
    plt.xticks(rotation=60)
    plt.show()
当我创建该循环时,出现了如下直方图:

看起来记号代表了每一个特征,而不仅仅是箱子。将柱状图放大到淫秽的比例证实了这一点:

我只是对垃圾桶的滴答声感兴趣,而不是特征滴答声。我如何去除这些记号来制作一个更整洁的柱状图?

我的建议是在课堂上使用并操作XTICK

例如,对于单个绘图:

_, axis = plt.subplots(1, 1) # create an Axes class for a single plot
axis.hist(data, bins=12)
axis.set_xticklabels(axis.get_xticks(), rotation=60)
plt.show()
上述代码生成具有以下美学特征的绘图:

此外,考虑到在您的情况下,正在打印许多要素,可以使用“子地块”功能生成一个网格打印,如下所示:

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import seaborn as sns

holiday_features = [name for name in df.columns if 'DAYS FROM' in name]

# define number of rows and columns to the subplot
ncols = 3  # considering a grid with 3 plots per row
nrows = int(np.ceil(len(holiday_features)/3))

# create subplots
_, axes = plt.subplots(nrows, ncols)
axes = axes.flatten()

# plot features with rotated ticks
for j, feature in enumerate(holiday_features):
    axes[j].hist(df[feature], bins=12)
    axes[j].set_title(feature)
    axes[j].set_xticklabels(axes[j].get_xticks(), rotation=60)
    
# remove empty plots
for k in range(j, len(axes)):
    sns.despine(ax=axes[k], top=True, right=True, left=True, bottom=True)
    axes[k].xaxis.set_major_formatter(ticker.NullFormatter())
    axes[k].xaxis.set_ticks_position('none')
    axes[k].yaxis.set_major_formatter(ticker.NullFormatter())
    axes[k].yaxis.set_ticks_position('none')

# show plot
plt.tight_layout()
plt.show()

看起来您正在同一
matplotlib
图上为每个功能绘制直方图。在调用
hist
之前,请尝试添加
plt.figure()
。要了解如何设置自定义
xticks
,您可以参考上的许多示例,例如@JacoSolari,我明白您的意思,但我只提供了本系列的第一部分。所有的直方图都像这个。关于这个特殊问题,
plt.show()
实际上有效地打破了这个循环,因此直方图不会相互重叠。