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

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

Using the same color for multiple plots if each dataset contains a common column

问题

  1. 我有两个数据框,其中包含多列。我将df1绘制为堆叠条形图,并将df2叠加为线图。如果df1中的数据系列存在于df2中,我希望条形段和线的颜色相同,以便进行比较。在下面的例子中,我希望“FO”的颜色相同。
  2. 数据框:

df#1

  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

  1. FO AR

Jan-23 1.0 1.1
Feb-23 1.0 2.2

  1. 这是我的代码:
  1. # 构建数据框列的并集,以便线条和条形系列可以具有相同的颜色,以便进行比较
  2. df_Plot1 = vg.dc_dF_Groups[employee]
  3. cols = df_Plot.columns.union(df_Plot1.columns)
  4. cmap = matplotlib.colormaps['Set1']
  5. colors = dict(zip(cols, cmap.colors))
  6. # 绘制员工的费用
  7. 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)
  8. # 绘制员工的组分配
  9. df_Plot1.iloc[0:plotFinish].reindex(columns=cols).plot.line(ax=ax2, color=df_Plot1.columns.map(colors), ylabel='Allocation', legend=True, linewidth=3)
  10. ax1.legend(title='Actuals', bbox_to_anchor=(1.05,1.0), loc='upper left', ncol=1)
  11. ax2.legend(title='Allocations', bbox_to_anchor=(1.05,0.5), loc='upper left', ncol=1)
  12. fig.suptitle(employee)
  1. <details>
  2. <summary>英文:</summary>
  3. 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.
  4. Data Frames:

df#1

  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

  1. FO AR

Jan-23 1.0 1.1
Feb-23 1.0 2.2

  1. Here&#39;s my code:
  1. # Build a union of the dataframe columns so the line and bar series can have the same color
  2. # for ease of comparison
  3. df_Plot1 = vg.dc_dF_Groups[employee]
  4. cols = df_Plot.columns.union(df_Plot1.columns)
  5. cmap = matplotlib.colormaps[&#39;Set1&#39;]
  6. colors = dict(zip(cols, cmap.colors))
  7. # Plot the employee&#39;s charges
  8. 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)
  9. # Plot the employee&#39;s group allocations
  10. 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)
  11. ax1.legend(title=&#39;Actuals&#39;, bbox_to_anchor=(1.05,1.0), loc=&#39;upper left&#39;, ncol=1)
  12. ax2.legend(title=&#39;Allocations&#39;, bbox_to_anchor=(1.05,0.5), loc=&#39;upper left&#39;, ncol=1)
  13. fig.suptitle(employee)
  1. </details>
  2. # 答案1
  3. **得分**: 1
  4. 你可以获得列的 [`union`](https://pandas.pydata.org/docs/reference/api/pandas.Index.union.html) 和 [`reindex`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.reindex.html):
  5. ```python
  6. cols = df1.columns.union(df2.columns)
  7. fig, axes = plt.subplots(ncols=2)
  8. df1.reindex(columns=cols).plot.bar(stacked=True, ax=axes[0])
  9. df2.reindex(columns=cols).plot.line(ax=axes[1])

输出:

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

或者,创建一个自定义颜色列表,并从列名映射它们:

  1. import matplotlib
  2. cols = df1.columns.union(df2.columns)
  3. cmap = matplotlib.colormaps['Set1']
  4. colors = dict(zip(cols, cmap.colors))
  5. fig, axes = plt.subplots(ncols=2)
  6. df1.plot.bar(stacked=True, color=df1.columns.map(colors), ax=axes[0])
  7. df2.plot.line(color=df2.columns.map(colors), ax=axes[1])

输出:

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

英文:

You can get the column's union and reindex:

  1. cols = df1.columns.union(df2.columns)
  2. fig, axes = plt.subplots(ncols=2)
  3. df1.reindex(columns=cols).plot.bar(stacked=True, ax=axes[0])
  4. df2.reindex(columns=cols).plot.line(ax=axes[1])

Output:

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

Alternatively, create a custom list of colors and map them from the column names:

  1. import matplotlib
  2. cols = df1.columns.union(df2.columns)
  3. cmap = matplotlib.colormaps[&#39;Set1&#39;]
  4. colors = dict(zip(cols, cmap.colors))
  5. fig, axes = plt.subplots(ncols=2)
  6. df1.plot.bar(stacked=True, color=df1.columns.map(colors), ax=axes[0])
  7. 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:

确定