有一对夫妇的选择,这里是代码和输出:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Dummy data - value ranges differ a lot between columns
X = pd.DataFrame()
for i in range(18):
X['COL0{0}'.format(i+38)]=(2**i)*np.random.random(1000)
# Method 1 - just using the hist function to generate each plot
X.hist(layout=(3, 6), figsize=(20, 10), sharey=False, sharex=False, bins=50)
plt.title('Method 1')
plt.show()
# Method 2 - generate each plot separately
cols = plt.cm.spectral(np.arange(1,255,13))
fig, axes = plt.subplots(3,6,figsize=(20,10))
for index, column in enumerate(X.columns):
ax = axes.flatten()[index]
ax.hist(X[column],bins=50, label=column, fc=cols[index])
ax.legend(loc='upper right')
ax.set_ylim((0,1.2*ax.get_ylim()[1]))
fig.suptitle('Method 2')
fig.show()
第一个情节:
第二个情节:
我肯定会推荐第二种方法,因为你有更多的控制更多的控制个别情节,例如你可以改变轴scal es,标签,网格参数,以及其他任何东西。
我找不到任何可以让您修改原始plot.hist分档以接受单独计算的分档。
我希望这有助于!
非常感谢!我采用了第二种方法,但由于列数不符合网格,我必须删除一些子图。我用'fig.delaxes'来删除未使用的子图。我不知道是否还有其他方法,因为使用第一种方法显示的轴没有多余,但没有提及Pandas代码库中的“delaxes”。 – rubik
您也可以使用** fig.add_subplot **向图中添加轴,请参阅https://pythonprogramming.net/subplot2grid-add_subplot-matplotlib-tutorial/。很高兴帮助! – Robbie