[英]How to set x-axis tick mark labels using matplotlib in python?
我只希望在python中使用matplotlib在条形图中为条形图显示x标签,即1992、1993、1994、1995而不是1990、1990.5、1991、1991.5等在x轴上显示,这是默认情况下的操作
我尝试使用这里建议的代码: 在matplotlib中操纵x轴刻度标签 ,建议使用以下代码: ind = range(2,6); plt.xticks(ind,x)
ind = range(2,6); plt.xticks(ind,x)
import pandas as pd
import numpy as np
np.random.seed(12346)
df = pd.DataFrame([np.random.normal(32000,200000,3650),
np.random.normal(43000,100000,3650),
np.random.normal(43500,140000,3650),
np.random.normal(48000,70000,3650)],
index=[1992,1993,1994,1995])
Y = 40000
import math
%matplotlib notebook
plt.figure()
std = df.std(axis = 1)
stderror = std/math.sqrt(df.shape[1])
marginoferror = stderror*1.96
print(df.mean(axis=1).iloc[0] - marginoferror.iloc[0])
def color_def(y,dataframe,mofe):
color = []
for i in range(0,dataframe.shape[0]):
if (dataframe.mean(axis=1).iloc[i] - mofe.iloc[i] > y):
color.append('darkred')
elif (dataframe.mean(axis=1).iloc[i] + mofe.iloc[i] < y):
color.append('darkblue')
else:
color.append('white')
return(color)
x = df.index.values
plt.bar(df.index.values,df.mean(axis=1),yerr=marginoferror,align='center', color = color_def(Y,df,marginoferror),alpha=0.5, edgecolor = 'black',ecolor='black', capsize=10)
plt.axhline(y=Y)
ind = range(2, 6) # the x locations for the groups
plt.xticks( ind, x)
当我使用这两行代码ind = range(2,6); plt.xticks(ind,x)
ind = range(2,6); plt.xticks(ind,x)
,可以在此链接上看到意外的结果: https : ind = range(2,6); plt.xticks(ind,x)
=0
它不是格式良好的条形图,并且没有标有1992、1993、1994和1995的x轴。
当我取出这两行代码时: ind = range(2,6); plt.xticks(ind,x)
ind = range(2,6); plt.xticks(ind,x)
,我确实得到了格式很好的条形图,只是带有笨拙的x轴标签
不确定数字2到5与您的数据有何关系。 您的数据是1992 ... 1995,这是数据框的索引,因此
plt.xticks(df.index.values)
会准确地勾选这些值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.