在 Matplotlib 中自定义轴 [英] Customize axes in Matplotlib

查看:38
本文介绍了在 Matplotlib 中自定义轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am a beginner with Python, Pandas and Matplotlib. I would like to customize the entries at the axes of a scatter plot. I have the following data:

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

So on the x-axis there should be 5 entries, with the first one being w1=1.0, w2=0.0. The 1 and 2 should be subscipts and w1 and w2 should be beneath each other, like you can see in the screenshot. Is there a way how to do this with pandas and matplotlib?

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

Here is the data (without the correspoding weight, you can see them in the screenshot):

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 ...)

The diagramm should look similar to this one, except that the description on the x-axis should be as I wrote above (instead of 1,2,3... to have w1=1.0, w2 =0.0, w1 =0.75, w2=0.25...)

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

Here is the figure after applying the code of "Ignoring_Gravity". The are two things wrong. First, the order of the w on the x-axis (is supposed to start from w1=1, w1=0.75, ... , w1=0). Secondly, the points are on the wrong horizontal position. They should be right above the corresponding entries at the x-axis.

推荐答案

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

You can display subscripts by writing your column names using 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 对象中的颜色来传递不同的颜色.

You can pass different colours by changing what's in the COLOURS object.

这篇关于在 Matplotlib 中自定义轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆