Customize axes in Matplotlib

2024/11/20 11:22:36

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

Answer

You can display subscripts by writing your column names using LaTex:

import pandas as pd
import matplotlib.pyplot as pltdf = 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")

This'll give you:

enter image description here

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

https://en.xdnf.cn/q/119832.html

Related Q&A

how to show the max and min from user input?

Nevermindif xmin == 1:print(ymin)I tried using the max and min but I get a typeerror which is int object not iterable.

To output a string without whitespce

My program:def string_splosion(str):j=1c=len(str)i=0s=while(i<c):s=s+(str[:i+j])i=i+1print sprint("Enter a string:") s=raw_input() string_splosion(s)Sample input:Code Expected output:CCoCo…

Regex replace `a.b` to `a. b`? [duplicate]

This question already has answers here:Python: Replace with regex(2 answers)Closed 6 years ago.I am trying to change all strings of the type a.b to a. b. It should also work if any of the characters ar…

Is there something wrong with this line of Python code?

I tried this line of code, and it kept giving me the SyntaxError.print(/ / - / \ / | * 30, end=\r)^It pointed on the brackets. Any suggestions? Thanks!

TypeError: tuple indices must be integers or slices, not str postgres/python

Hi Im trying to get a timestamp from a row called time in my postgres database, however Im getting the error. Its the second row in the database. The error TypeError: tuple indices must be integers or …

Pandas Python: KeyError Date

I am import into python where it will automatically create a date time object.However I want the first column to be a datetime object in Python. Data looks likeDate,cost 41330.66667,100 41331.66667,101…

How to resample 1 minute data into 15 minute data?

CSV file. df before resample and after applying: df["dateandtime"] = (pd.to_datetime(df.pop("DATE").str.cat(df.pop("TIME"), sep=" "))) df = df.set_index(pd.Datet…

Python Pygame Lighting for Pong

Hey Guys Im writing a little Pong-Game in Pygame and wanted to use a glowing-effect on the Ball and the Bats. But Pygame dosent support this effects and make solid blocks out of it. Is there a way to h…

python - List.remove method not applicable as the function with the map builtin?

Question Can List.remove not be used as the function to apply in the map(function, iterable, ...) builtin? %%timeit import random m = _max = 10000 n = random.randint(1, m)outer = random.sample(populat…

How do I change a sum for string for it to work?

This is my whole program Im working on (with the function not in the right place due to the code needing to be indented) but anyway there is a problem that Im not sure how to fix.How do I change it so …