【问题标题】:Customize axes in Matplotlib在 Matplotlib 中自定义轴
【发布时间】:2020-04-21 00:37:05
【问题描述】:

我是 Python、Pandas 和 Matplotlib 的初学者。我想自定义散点图轴上的条目。我有以下数据:

所以在 x 轴上应该有 5 个条目,第一个是 w1=1.0,w2=0.0。 1 和 2 应该是 subscipts 并且 w1 和 w2 应该在彼此下方,就像您在屏幕截图中看到的那样。有没有办法用 pandas 和 matplotlib 做到这一点?

这是数据(没有对应的重量,你可以在截图中看到它们):

Method 1    31.7    32.9    33.7    34.4    35.2
Method 2    44.2    45.4    46.9    48.9    45.5
Method 3    75.6    72.2    69.2    67.4    63.6
Method 4    87.5    83.2    79.5    77.8    72.2
Method 5    88.6    84.1    80.7    79.6    74.5
Method 6    100.0   100.0   100.0   100.0   100.0

这个图表应该和这个类似,除了 x 轴上的描述应该和我上面写的一样(而不是 1,2,3... 有 w1=1.0, w2 =0.0, w1 = 0.75, w2=0.25...)

编辑:这是应用“Ignoring_Gravity”代码后的图。有两件事不对。首先,w在x轴上的顺序(应该从w1=1,w1=0.75,...,w1=0开始)。其次,这些点位于错误的水平位置。它们应该在 x 轴对应条目的正上方。

【问题讨论】:

  • 你能把你的表格粘贴到问题中,而不是截图吗?
  • 嗨 Ignoring_Gravity,我刚刚添加了数据
  • 可以添加列的标题(w1 = , ...)吗?
  • 您好 Ignoring_Gravity,您可以在初始屏幕截图中看到标题。我无法使用stackoverflow的编辑系统将它们包含在原始数据集中,因为它们太长了。
  • 真的很奇怪。我的问题有一个答案(几乎)解决了我的问题,但我再也看不到了。怎么会?我是不是做错了什么?

标签: python pandas matplotlib


【解决方案1】:

您可以通过使用 LaTex 编写列名来显示下标:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(
    {
        0: {
            "Method 1": 31.7,
            "Method 2": 44.2,
            "Method 3": 75.6,
            "Method 4": 87.5,
            "Method 5": 88.6,
            "Method 6": 100.0,
        },
        1: {
            "Method 1": 32.9,
            "Method 2": 45.4,
            "Method 3": 72.2,
            "Method 4": 83.2,
            "Method 5": 84.1,
            "Method 6": 100.0,
        },
        2: {
            "Method 1": 33.7,
            "Method 2": 46.9,
            "Method 3": 69.2,
            "Method 4": 79.5,
            "Method 5": 80.7,
            "Method 6": 100.0,
        },
        3: {
            "Method 1": 34.4,
            "Method 2": 48.9,
            "Method 3": 67.4,
            "Method 4": 77.8,
            "Method 5": 79.6,
            "Method 6": 100.0,
        },
        4: {
            "Method 1": 35.2,
            "Method 2": 45.5,
            "Method 3": 63.6,
            "Method 4": 72.2,
            "Method 5": 74.5,
            "Method 6": 100.0,
        },
    }
)
df.columns = [
    "$w_1=1.0$\n$w_2=0.0$",
    "$w_1=0.75$\n$w_2=0.25$",
    "$w_1=0.5$\n$w_2=0.5$",
    "$w_1=0.25$\n$w_2=0.75$",
    "$w_1=0.0$\n$w_2=1.0$",
]

COLOURS = ['blue', 'green', 'red', 'yellow', 'pink', 'black']

fig, ax = plt.subplots(figsize=(12, 8))
for n, (label, data) in enumerate(df.iterrows()):
    ax.plot(data, marker='o', linestyle='none', label=label, c=COLOURS[n])
ax.grid()
ax.legend(loc="best")

这会给你:

您可以通过更改 COLOURS 对象中的内容来传递不同的颜色。

【讨论】:

  • 感谢 Ignoring_Gravity,这有助于解决订阅者的问题。不幸的是,还有两个更进一步的问题(我昨天在这里发布的)。我上传了在 Jupyter 中使用您的代码时得到的错误图,并在我之前的帖子中添加了一个简短的描述
  • 不知道为什么你使用相同的代码得到与我不同的情节:) 你使用的是什么版本的 pandas 和 matplotlib?可以pip install --upgrade pandas matplotlib吗?
  • 我认为这是一个单独的问题,您可能想就此提出一个新问题,然后在您的环境整理好后再回到这个问题
  • @PeterBe sure - 我已经更新了解决方案。抱歉,拖了这么久,我对回答问题还比较陌生!
  • n 在从0 到6 的一个计数器中,它可以让我们选择相关的颜色。 fig 是 matplotlib 图,ax 是子图 - 请参考user guide
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-21
  • 1970-01-01
  • 2018-07-04
  • 2020-07-22
  • 1970-01-01
  • 2018-12-01
相关资源
最近更新 更多