使用相同的颜色为多个图表着色,如果每个数据集都包含一个共同的列。

huangapple go评论59阅读模式
英文:

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 &amp; line to be the same, for ease of comparison.  How would I do that?  Below, I would want the colors of &#39;FO&#39; 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&#39;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[&#39;Set1&#39;]
colors = dict(zip(cols, cmap.colors))

# Plot the employee&#39;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=&#39;Date&#39;, ylabel=&#39;Actuals&#39;, alpha=0.5, stacked=True)

# Plot the employee&#39;s group allocations
df_Plot1.iloc[0:plotFinish].reindex(columns=cols).plot.line(ax=ax2, color=df_Plot1.columns.map(colors), ylabel=&#39;Allocation&#39;, legend=True, linewidth=3)
ax1.legend(title=&#39;Actuals&#39;, bbox_to_anchor=(1.05,1.0), loc=&#39;upper left&#39;, ncol=1)
ax2.legend(title=&#39;Allocations&#39;, bbox_to_anchor=(1.05,0.5), loc=&#39;upper left&#39;, 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[&#39;Set1&#39;]
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:

使用相同的颜色为多个图表着色,如果每个数据集都包含一个共同的列。

huangapple
  • 本文由 发表于 2023年4月13日 20:14:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76005295.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定