操作Seaborn Jointplot的图例

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

Manipulating the legend for a Seaborn Jointplot

问题

我正在寻求关于如何编辑在使用seaborn jointplot和其他matplotlib散点图图层时,如何编辑图例中符号的颜色的建议。

我的具体问题是:

在给定的数据和当前图表中,如何调整代码以使图例中最后五个点(例如“3”,“4”,“5”,“6”,“8”)的颜色更改为灰色(即#b9b9bd)?

可重现的代码如下,但也可以在Colab Notebook中进行复制和实验。

  1. # 导入包
  2. import matplotlib.pyplot as plt
  3. import seaborn as sns
  4. import pandas as pd
  5. import numpy as np
  6. # 加载mpg数据集
  7. mpg_df = sns.load_dataset("mpg")
  8. mpg_df = (
  9. mpg_df
  10. .astype({"cylinders":"category"})
  11. )
  12. mpg_df["cylinders"] = (
  13. mpg_df["cylinders"]
  14. .cat
  15. .as_ordered()
  16. )
  17. # 确定标记符号
  18. _markers_cylinders = {
  19. 3: "P",
  20. 4: "d",
  21. 5: "v",
  22. 6: "X",
  23. 8: "s"
  24. }
  25. # 为国家确定颜色
  26. _palette_origin = {
  27. "usa": "#fca847",
  28. "japan": "#8aed7b",
  29. "europe": "#7b81ed"
  30. }
  31. kws = {
  32. "s": 225,
  33. "linewidth": 2
  34. }
  35. # 绘制联合图 - 确定图形 - 只需边际分布的内容
  36. jp = sns.jointplot(
  37. data=mpg_df,
  38. x="weight",
  39. y="mpg",
  40. hue="origin",
  41. palette=_palette_origin,
  42. markers=",",
  43. marginal_kws={"fill": True},
  44. color="w",
  45. height=10,
  46. s=1
  47. )
  48. # 作为原始联合图上的图层绘制按原产地和汽缸分类的散点图
  49. origin = ["usa", "japan", "europe"]
  50. for nation in origin:
  51. df = mpg_df[mpg_df["origin"] == nation]
  52. for k, v in _markers_cylinders.items():
  53. jp.ax_joint.scatter(
  54. data=df[df["cylinders"] == k],
  55. x="weight",
  56. y="mpg",
  57. marker=_markers_cylinders[k],
  58. c=_palette_origin[nation],
  59. edgecolor="k",
  60. alpha=0.6,
  61. **kws
  62. )
  63. jp.ax_joint.grid(
  64. color="k",
  65. linestyle=":",
  66. linewidth=0.75
  67. )
  68. han, lab = jp.ax_joint.get_legend_handles_labels()
  69. lab = [
  70. "USA",
  71. "Japan",
  72. "Europe",
  73. "3",
  74. "4",
  75. "5",
  76. "6",
  77. "8"
  78. ]
  79. jp.ax_joint.legend(
  80. han[0:8],
  81. lab[0:8],
  82. title="Origin & Cylinders",
  83. fontsize=15,
  84. bbox_to_anchor=(1.20, 1),
  85. title_fontsize=14,
  86. markerscale=2.5,
  87. shadow=True
  88. )
  89. sns.move_legend(
  90. jp.ax_joint,
  91. loc="upper left",
  92. bbox_to_anchor=(1.20, 1),
  93. markerscale=0.25
  94. )
  95. plt.show()
  96. plt.show()

希望这有助于您更改图例中最后五个点的颜色为灰色(#b9b9bd)。

英文:

I am seeking advice on how to edit the colors of the symbols in my legend when using a seaborn jointplot with several other matplotlib scatterplot layers.

My Specific Question:

> Given the data and the current chart below, how can make an adjustment to the code so that the colors of the latter five points on the legend (e.g., "3", "4", "5", "6", "8") can be changed to gray (i.e., #b9b9bd)?

The reproducible code has been pasted below, but there is a publicly accessible Colab Notebook that can be copied and used for experimentation.

<details>
<summary> Reproducible Example </summary>

  1. # import pacakges
  2. import matplotlib.pyplot as plt
  3. import seaborn as sns
  4. import pandas as pd
  5. import numpy as np
  6. # load the mpg dataset
  7. mpg_df = sns.load_dataset(&quot;mpg&quot;)
  8. mpg_df = (
  9. mpg_df
  10. .astype({&quot;cylinders&quot;:&quot;category&quot;})
  11. )
  12. mpg_df[&quot;cylinders&quot;] = (
  13. mpg_df[&quot;cylinders&quot;]
  14. .cat
  15. .as_ordered()
  16. )
  17. # establish the markers
  18. _markers_cylinders = {
  19. 3:&quot;P&quot;,
  20. 4:&quot;d&quot;,
  21. 5:&quot;v&quot;,
  22. 6:&quot;X&quot;,
  23. 8:&quot;s&quot;
  24. }
  25. # establish colors for countries
  26. _palette_origin = {
  27. &quot;usa&quot;:&quot;#fca847&quot;,
  28. &quot;japan&quot;:&quot;#8aed7b&quot;,
  29. &quot;europe&quot;:&quot;#7b81ed&quot;
  30. }
  31. kws={
  32. &quot;s&quot;: 225,
  33. &quot;linewidth&quot;: 2
  34. }
  35. # plot the jointplot -- establish the figure -- the content of the plot is not needed just the marginal ditributions
  36. jp = sns.jointplot(
  37. data=mpg_df,
  38. x=&quot;weight&quot;,
  39. y=&quot;mpg&quot;,
  40. hue=&quot;origin&quot;,
  41. palette=_palette_origin,
  42. markers=&quot;,&quot;,
  43. marginal_kws={&quot;fill&quot;:True},
  44. color=&quot;w&quot;,
  45. height=10,
  46. s=1
  47. )
  48. # plot scatter by origin and cylinder as layers on the original jointplot
  49. origin = [&quot;usa&quot;, &quot;japan&quot;, &quot;europe&quot;]
  50. for nation in origin:
  51. df = mpg_df[mpg_df[&quot;origin&quot;] == nation]
  52. for k,v in _markers_cylinders.items():
  53. jp.ax_joint.scatter(
  54. data=df[df[&quot;cylinders&quot;]==k],
  55. x=&quot;weight&quot;,
  56. y=&quot;mpg&quot;,
  57. marker=_markers_cylinders[k],
  58. c=_palette_origin[nation],
  59. edgecolor=&quot;k&quot;,
  60. alpha=0.6,
  61. **kws
  62. )
  63. jp.ax_joint.grid(
  64. color=&quot;k&quot;,
  65. linestyle=&quot;:&quot;,
  66. linewidth=0.75
  67. )
  68. han, lab = jp.ax_joint.get_legend_handles_labels()
  69. lab = [
  70. &quot;USA&quot;,
  71. &quot;Japan&quot;,
  72. &quot;Europe&quot;,
  73. &quot;3&quot;,
  74. &quot;4&quot;,
  75. &quot;5&quot;,
  76. &quot;6&quot;,
  77. &quot;8&quot;
  78. ]
  79. jp.ax_joint.legend(
  80. han[0:8],
  81. lab[0:8],
  82. title=&quot;Origin &amp; Cylinders&quot;,
  83. fontsize=15,
  84. bbox_to_anchor=(1.20, 1),
  85. title_fontsize = 14,
  86. markerscale=2.5,
  87. shadow = True
  88. )
  89. sns.move_legend(
  90. jp.ax_joint,
  91. loc=&quot;upper left&quot;,
  92. bbox_to_anchor=(1.20, 1),
  93. markerscale=0.25
  94. )
  95. plt.show()
  96. plt.show()

</details>

操作Seaborn Jointplot的图例

答案1

得分: 1

这可以通过重复使用现有的标记并更改标记的面颜色来完成。

  1. han, lab = jp.ax_joint.get_legend_handles_labels()
  2. new_han = [
  3. handles[0],
  4. handles[1],
  5. handles[2],
  6. Line2D([0], [0], marker='P', markerfacecolor='#b9b9bd', markeredgecolor='black', markersize=14, ls=''),
  7. Line2D([0], [1], marker='d', markerfacecolor='#b9b9bd', markeredgecolor='black', markersize=14, ls=''),
  8. Line2D([0], [2], marker='v', markerfacecolor='#b9b9bd', markeredgecolor='black', markersize=14, ls=''),
  9. Line2D([0], [3], marker='X', markerfacecolor='#b9b9bd', markeredgecolor='black', markersize=14, ls=''),
  10. Line2D([0], [4], marker='s', markerfacecolor='#b9b9bd', markeredgecolor='black', markersize=14, ls='')
  11. ]
  12. jp.ax_joint.legend(
  13. new_han,
  14. lab[0:8],
  15. title="Origin & Cylinders",
  16. fontsize=15,
  17. bbox_to_anchor=(1.20, 1),
  18. title_fontsize=14,
  19. markerscale=2.5,
  20. shadow=True
  21. )

请注意,此代码段中的HTML实体已被还原为普通文本。

英文:

This can be accomplished by reusing the existing handles and then changing the face color of the marker.

  1. han, lab = jp.ax_joint.get_legend_handles_labels()
  2. new_han = [
  3. handles[0],
  4. handles[1],
  5. handles[2],
  6. Line2D([0], [0], marker=&#39;P&#39;, markerfacecolor=&#39;#b9b9bd&#39;, markeredgecolor=&#39;black&#39;, markersize=14, ls=&#39;&#39;),
  7. Line2D([0], [1], marker=&#39;d&#39;, markerfacecolor=&#39;#b9b9bd&#39;, markeredgecolor=&#39;black&#39;, markersize=14, ls=&#39;&#39;),
  8. Line2D([0], [2], marker=&#39;v&#39;, markerfacecolor=&#39;#b9b9bd&#39;, markeredgecolor=&#39;black&#39;, markersize=14, ls=&#39;&#39;),
  9. Line2D([0], [3], marker=&#39;X&#39;, markerfacecolor=&#39;#b9b9bd&#39;, markeredgecolor=&#39;black&#39;, markersize=14, ls=&#39;&#39;),
  10. Line2D([0], [4], marker=&#39;s&#39;, markerfacecolor=&#39;#b9b9bd&#39;, markeredgecolor=&#39;black&#39;, markersize=14, ls=&#39;&#39;)
  11. ]
  12. jp.ax_joint.legend(
  13. new_han,#han[0:8],
  14. lab[0:8],
  15. title=&quot;Origin &amp; Cylinders&quot;,
  16. fontsize=15,
  17. bbox_to_anchor=(1.20, 1),
  18. title_fontsize = 14,
  19. markerscale=2.5,
  20. shadow = True
  21. )

操作Seaborn Jointplot的图例

huangapple
  • 本文由 发表于 2023年2月6日 12:38:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357383.html
匿名

发表评论

匿名网友

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

确定