创建子图而不是单独的图 [英] Creating a subplot instead of separate plots
本文介绍了创建子图而不是单独的图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下数据框:
symbol aaa bbb ccc ddd eee fff ggg
aaa
bbb -0.001
ccc 0.348 -0.025
ddd -0.42 -0.075 -0.701
eee -0.276 0.004 -0.516 0.661
fff 0.175 -0.107 0.363 -0.521 -0.356
ggg 0.469 0.012 0.364 -0.519 -0.306 0.306
我正在基于上述数据框创建两个单独的图,如下所示:
I am creating two separate plots based on the above dataframe as follows:
def plot_bar(self, corr_df):
dfstacked = corr_df.stack().order()
dfstacked.plot(kind='bar', rot=60)
plt.show()
def plot_heatmap(self, corr_df):
corr_df = corr_df.fillna(value=0)
plt.pcolormesh(corr_df.values, cmap=plt.cm.Blues)
plt.yticks(np.arange(0.5, len(corr_df.index), 1), corr_df.index)
plt.xticks(np.arange(0.5, len(corr_df.columns), 1), corr_df.columns)
plt.show()
现在我需要做的不是分别绘制这两个图表,而是需要将它们放在一个网格中.我设法创建了以下子图,该子图需要在左侧有条形图,在右侧需要矩阵/彩色网格:
Now what I need to do is instead of plotting these two charts separately, I need them together in a grid. I have managed to create the following subplot which needs to have the bar chart on the left, and the matrix/color mesh on the right:
fig, axes = plt.subplots(2)
axes[0] = plt.subplot2grid((1,5), (0,0), colspan=3)
axes[1] = plt.subplot2grid((1,5), (0,3), colspan=2)
plt.show()
在我的一生中,我似乎无法正常工作.我需要在 axes [0]
上使用条形图,在 axes [1]
For the life of me I can't seem to get this working. I need the bar chart on axes[0]
and the matrix/color mesh on axes[1]
推荐答案
您尝试过
fig, axes = plt.subplots(2)
plt.subplot2grid((1,5), (0,0), colspan=3)
# here plot something
plt.subplot2grid((1,5), (0,3), colspan=2)
# here plot something
plt.show()
例如
import matplotlib.pyplot as plt
fig, axes = plt.subplots(2)
plt.subplot2grid((1,5), (0,0), colspan=3)
plt.plot([1,2,3]) # plot something
plt.subplot2grid((1,5), (0,3), colspan=2)
plt.plot([1,2,1]) # plot something
plt.show()
<小时>
import pandas as pd
import numpy as np
def plot_bar(corr_df):
dfstacked = corr_df.stack().order()
dfstacked.plot(kind='bar', rot=60)
def plot_heatmap(corr_df):
corr_df = corr_df.fillna(value=0)
plt.pcolormesh(corr_df.values, cmap=plt.cm.Blues)
plt.yticks(np.arange(0.5, len(corr_df.index), 1), corr_df.index)
plt.xticks(np.arange(0.5, len(corr_df.columns), 1), corr_df.columns)
df = pd.DataFrame(range(10))
fig, axes = plt.subplots(2)
plt.subplot2grid((1,5), (0,0), colspan=3)
plot_bar(df)
plt.subplot2grid((1,5), (0,3), colspan=2)
plot_heatmap(df)
plt.show()
这篇关于创建子图而不是单独的图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文