英文:
Using the same color for multiple plots if each dataset contains a common column
问题
我有两个数据框,其中包含多列。我将df#1绘制为堆叠条形图,并将df#2叠加为线图。如果df#1中的数据系列存在于df#2中,我希望条形段和线的颜色相同,以便进行比较。在下面的例子中,我希望“FO”的颜色相同。
数据框:
df#1
FO MI
Date
08-Jan-23 0.4925 0.0000
15-Jan-23 0.9075 0.0000
22-Jan-23 0.9750 0.0250
29-Jan-23 0.9625 0.0375
05-Feb-23 0.9825 0.0000
df#2
FO AR
Jan-23 1.0 1.1
Feb-23 1.0 2.2
这是我的代码:
# 构建数据框列的并集,以便线条和条形系列可以具有相同的颜色,以便进行比较
df_Plot1 = vg.dc_dF_Groups[employee]
cols = df_Plot.columns.union(df_Plot1.columns)
cmap = matplotlib.colormaps['Set1']
colors = dict(zip(cols, cmap.colors))
# 绘制员工的费用
df_Plot.iloc[0:plotFinish].reindex(columns=cols).plot.bar(ax=ax1, color=df_Plot.columns.map(colors), rot=45, legend=True, xlabel='Date', ylabel='Actuals', alpha=0.5, stacked=True)
# 绘制员工的组分配
df_Plot1.iloc[0:plotFinish].reindex(columns=cols).plot.line(ax=ax2, color=df_Plot1.columns.map(colors), ylabel='Allocation', legend=True, linewidth=3)
ax1.legend(title='Actuals', bbox_to_anchor=(1.05,1.0), loc='upper left', ncol=1)
ax2.legend(title='Allocations', bbox_to_anchor=(1.05,0.5), loc='upper left', ncol=1)
fig.suptitle(employee)
<details>
<summary>英文:</summary>
I have two data frames with several columns. I plot df#1 as as stacked bar plot and overlay df #2 as a line plot. If a data series in df#1 is present in df#2 I want the color of the bar segment & line to be the same, for ease of comparison. How would I do that? Below, I would want the colors of 'FO' to be the same.
Data Frames:
df#1
FO MI
Date
08-Jan-23 0.4925 0.0000
15-Jan-23 0.9075 0.0000
22-Jan-23 0.9750 0.0250
29-Jan-23 0.9625 0.0375
05-Feb-23 0.9825 0.0000
df#2
FO AR
Jan-23 1.0 1.1
Feb-23 1.0 2.2
Here's my code:
# Build a union of the dataframe columns so the line and bar series can have the same color
# for ease of comparison
df_Plot1 = vg.dc_dF_Groups[employee]
cols = df_Plot.columns.union(df_Plot1.columns)
cmap = matplotlib.colormaps['Set1']
colors = dict(zip(cols, cmap.colors))
# Plot the employee's charges
df_Plot.iloc[0:plotFinish].reindex(columns=cols).plot.bar(ax=ax1, color=df_Plot.columns.map(colors), rot=45, legend=True, xlabel='Date', ylabel='Actuals', alpha=0.5, stacked=True)
# Plot the employee's group allocations
df_Plot1.iloc[0:plotFinish].reindex(columns=cols).plot.line(ax=ax2, color=df_Plot1.columns.map(colors), ylabel='Allocation', legend=True, linewidth=3)
ax1.legend(title='Actuals', bbox_to_anchor=(1.05,1.0), loc='upper left', ncol=1)
ax2.legend(title='Allocations', bbox_to_anchor=(1.05,0.5), loc='upper left', ncol=1)
fig.suptitle(employee)
</details>
# 答案1
**得分**: 1
你可以获得列的 [`union`](https://pandas.pydata.org/docs/reference/api/pandas.Index.union.html) 和 [`reindex`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.reindex.html):
```python
cols = df1.columns.union(df2.columns)
fig, axes = plt.subplots(ncols=2)
df1.reindex(columns=cols).plot.bar(stacked=True, ax=axes[0])
df2.reindex(columns=cols).plot.line(ax=axes[1])
输出:
或者,创建一个自定义颜色列表,并从列名映射它们:
import matplotlib
cols = df1.columns.union(df2.columns)
cmap = matplotlib.colormaps['Set1']
colors = dict(zip(cols, cmap.colors))
fig, axes = plt.subplots(ncols=2)
df1.plot.bar(stacked=True, color=df1.columns.map(colors), ax=axes[0])
df2.plot.line(color=df2.columns.map(colors), ax=axes[1])
输出:
英文:
You can get the column's union
and reindex
:
cols = df1.columns.union(df2.columns)
fig, axes = plt.subplots(ncols=2)
df1.reindex(columns=cols).plot.bar(stacked=True, ax=axes[0])
df2.reindex(columns=cols).plot.line(ax=axes[1])
Output:
Alternatively, create a custom list of colors and map them from the column names:
import matplotlib
cols = df1.columns.union(df2.columns)
cmap = matplotlib.colormaps['Set1']
colors = dict(zip(cols, cmap.colors))
fig, axes = plt.subplots(ncols=2)
df1.plot.bar(stacked=True, color=df1.columns.map(colors), ax=axes[0])
df2.plot.line(color=df2.columns.map(colors), ax=axes[1])
Output:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论