我的目标是得到类似于下图的东西:

enter image description here

目前,我试着这样建造它:

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

但我们距离达到预期的结果还有很长的路要走.我真的不知道怎么做,你能帮我吗?

推荐答案

为了能够绘图,字符串应转换为数字.

使用构建在matplotlib上进行绘图的Pandas(或Seborn)库,可以更轻松地绘制每个X值具有多个条形图.您的数据没有直方图数据,您似乎想要条形图.

以下是一些代码.可以进行许多定制.

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

pandas bar plot

可能的定制包括:

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

customized bar plot with pandas and matplotlib

PS:这张照片看起来像:

Class sE s eE
Class 1 -9.51 19.605 -5.364
Class 2 -13.5 28.388 -7.954
Class 3 0.193 1.762 -3.756
Class 4 9.564 -4.264 -0.184
Class 5 23.13 -24.716 1.883
Class 6 -0.252 -26.956 41.876
Class 7 -0.442 0.382 -0.012

Python相关问答推荐

Gekko解算器错误results.json未找到,无法找出原因

从流程获取定期更新

Python 枕头上的图像背景变黑

将大小为n*512的数组绘制到另一个大小为n*256的数组的PC组件

在Python中,什么表达相当于0x1.0p-53?

如何使用Selenium访问svg对象内部的元素

从管道将Python应用程序部署到Azure Web应用程序,不包括需求包

我必须将Sigmoid函数与r2值的两种类型的数据集(每种6个数据集)进行匹配,然后绘制匹配函数的求导.我会犯错

Pandas 第二小值有条件

@Property方法上的inspect.getmembers出现意外行为,引发异常

删除所有列值,但判断是否存在任何二元组

为什么符号没有按顺序添加?

如何获取TFIDF Transformer中的值?

如何在Python脚本中附加一个Google tab(已经打开)

在vscode上使用Python虚拟环境时((env))

Django REST Framework:无法正确地将值注释到多对多模型,不断得到错误字段名称字段对模型无效'<><>

Django RawSQL注释字段

处理具有多个独立头的CSV文件

寻找Regex模式返回与我当前函数类似的结果

人口全部乱序 - Python—Matplotlib—映射