Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

python - Customize axes in 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: enter image description here

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

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...) enter image description here

EDIT: 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.enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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$
$w_2=0.0$",
    "$w_1=0.75$
$w_2=0.25$",
    "$w_1=0.5$
$w_2=0.5$",
    "$w_1=0.25$
$w_2=0.75$",
    "$w_1=0.0$
$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")

This'll give you:

enter image description here

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...