Multi-axis x using matplotlib python

WBOY
Release: 2024-02-09 18:12:04
forward
1091 people have browsed it

使用 matplotlib python 进行多轴 x

Question content

My goal is to get something like the picture below:

Currently I try to build it like this:

import matplotlib.pyplot as plt import numpy as np X = ['Class 1','Class 2','Class 3','Class 4', 'Class 5', 'Class 6', 'Class 7'] sE = ['-9,51', '-13,5', '0,193', '9,564', '23,13', '-0,252', '-0,442'] s = ['19,605', '28,388', '1,762', '-4,264', '-24,716', '-26,956', '0,382'] eE = ['-5,364', '-7,954', '-3,756', '-0,184', '1,883', '41,876', '-0,012'] X_axis = np.arange(len(X)) # plt.bar(X_axis, sE, color='red',width = 0.25, edgecolor='black') # plt.bar(X_axis+0.25, s, color='cyan',width = 0.25, edgecolor='black') # plt.bar(X_axis+0.5, eE, color='green',width = 0.25, edgecolor='black') #plt.hist([sE, s, eE], color = ['red', 'cyan', 'green'], edgecolor = 'black', histtype = 'bar') #plt.xticks(X_axis, X) plt.xlabel("Classes") plt.title("Geographical STP A") plt.show()
Copy after login

But we are still a long way from reaching the desired results. I really don't know what to do, can you help me?


Correct answer


To be able to plot, strings should be converted to numbers.

Plotting with multiple bars per x-value is much easier using the pandas (or seaborn) library based on matplotlib for plotting. Your data doesn't have histogram data, you seem to want a bar plot.

Here is some code. Many customizations are possible.

import matplotlib.pyplot as plt import pandas as pd x = ['class 1', 'class 2', 'class 3', 'class 4', 'class 5', 'class 6', 'class 7'] se = ['-9,51', '-13,5', '0,193', '9,564', '23,13', '-0,252', '-0,442'] s = ['19,605', '28,388', '1,762', '-4,264', '-24,716', '-26,956', '0,382'] ee = ['-5,364', '-7,954', '-3,756', '-0,184', '1,883', '41,876', '-0,012'] # organize the data as a pandas dataframe df = pd.dataframe({'class': x, 'se': se, 's': s, 'ee': ee}) # convert strings to numeric df['se'] = df['se'].str.replace(',','.').astype(float) df['s'] = df['s'].str.replace(',','.').astype(float) df['ee'] = df['ee'].str.replace(',','.').astype(float) ax = df.set_index('class').plot(kind='bar') ax.set_title("geographical stp a") ax.tick_params(axis='x', rotation=0) plt.show()
Copy after login

Possible customizations may include:

ax = df.set_index('Class').plot(kind='bar', color=['crimson', 'limegreen', 'dodgerblue']) ax.set_title("Geographical STP A") ax.tick_params(axis='x', rotation=0, length=0) # rotate tick labels horizontally, remove tick mark ax.grid(True, axis='y') # add a grid in the y direction ax.set_xlabel('') # remove superfluous x label for dir in ['top', 'bottom', 'right']: ax.spines[dir].set_visible(False) # remove the border around the plot
Copy after login

ps: The data frame looks like this:

kindsesee标题>1 Class-9.5119.605-5.3642 Class-13.528.388-7.954Level 30.1931.762-3.7564 Class9.564-4.264-0.184Level 523.13-24.7161.883Level 6-0.252-26.95641.8767 Class-0.4420.382-0.012表>

The above is the detailed content of Multi-axis x using matplotlib python. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!