Python 在Matplotlib中自定义轴

Python 在Matplotlib中自定义轴,python,pandas,matplotlib,Python,Pandas,Matplotlib,我是Python、Pandas和Matplotlib的初学者。我想自定义散点图轴上的条目。我有以下数据: 所以在x轴上应该有5个条目,第一个是w1=1.0,w2=0.0。1和2应该是子类,w1和w2应该在彼此下面,就像你在屏幕截图中看到的那样。有没有办法用熊猫和matplotlib做到这一点 以下是数据(没有相应的权重,您可以在屏幕截图中看到): 该图表应类似于此,除了x轴上的描述应如我上面所述(而不是1,2,3…使w1=1.0,w2=0.0,w1=0.75,w2=0.25…) 编辑:这是应

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

所以在x轴上应该有5个条目,第一个是w1=1.0,w2=0.0。1和2应该是子类,w1和w2应该在彼此下面,就像你在屏幕截图中看到的那样。有没有办法用熊猫和matplotlib做到这一点

以下是数据(没有相应的权重,您可以在屏幕截图中看到):

该图表应类似于此,除了x轴上的描述应如我上面所述(而不是1,2,3…使w1=1.0,w2=0.0,w1=0.75,w2=0.25…)


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

您可以通过使用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")
这将为您提供:


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

您可以将表格粘贴到问题中,而不是放置屏幕截图吗?嗨,忽略重力,我刚刚添加了数据。您可以添加列的标题(w1=,…)?嗨,忽略重力,您可以在初始屏幕截图上看到标题。我无法使用stackoverflow的编辑系统将它们包含在原始数据集中,因为它们太长了。真奇怪。我的问题有一个答案(几乎)解决了我的问题,但我再也看不到了。怎么会?我做错什么了吗?多亏了忽略重力,这有助于解决子委员会的问题。不幸的是,还有两个问题(我昨天在这里发布)。我上传了在Jupyter中使用您的代码时得到的错误绘图,并在我之前的帖子中添加了一个简短的描述。不知道为什么您使用相同的代码得到与我不同的绘图:)您使用的是什么版本的pandas和matplotlib?您能否执行
pip安装--升级matplotlib
?我认为这是一个单独的问题,您可能想问一个关于这个问题的新问题,然后在对环境进行排序后再回到这个问题上来out@PeterBe当然-我已经更新了解决方案。抱歉,这花了这么长时间,我对回答问题还比较陌生
n
只需一个计数器,从
0
6
,它就可以让我们选择相关的颜色
fig
是matplotlib图,
ax
是子图-请参阅
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")