[英]Preserving the xticks in multiple bar plots in matplotlib
1) I am not able to see the text-based xticks which are stored as list in the variable x. 1)我无法在变量x中看到作为列表存储的基于文本的xticks。 When I have only one single column based bar plot, I can see the xticks as text but not for more.
当我只有一个基于单列的条形图时,我可以将xticks视为文本而不是更多。
2)how can I control the font properties of xticks and the values in y axis? 2)如何控制xticks的字体属性和y轴的值?
Thank you. 谢谢。
import matplotlib.pyplot as plt
import pylab as pl
import numpy as np
#load text and columns into different variables
data = np.genfromtxt('a', names=True, dtype=None, usecols=("X", "N2", "J2", "V2", "asd", "xyz"))
x = data['X']
n = data['N2']
j = data['J2']
v = data['V2']
#make x axis string based labels
r=np.arange(1,25,1.5)
plt.xticks(r,x) #make sure dimension of x and n matches
plt.figure(figsize=(3.2,2), dpi=300, linewidth=3.0)
ax = plt.subplot(111)
ax.bar(r,v,width=0.9,color='red',edgecolor='black', lw=0.5, align='center')
plt.axhline(y=0,linewidth=1.0,color='black') #horizontal line at y=0
plt.axis([0.5,16.5,-0.4,0.20])
ax.bar(r,j,width=0.6,color='green',edgecolor='black', lw=0.5, align='center')
ax.bar(r,n,width=0.3,color='blue',edgecolor='black', lw=0.5, align='center')
plt.axhline(y=0,linewidth=1,color='black') #horizontal line at y=0
plt.axis([0.5,24.5,-0.36,0.15])
plt.savefig('fig',dpi=300,format='png',orientation='landscape')
The way you're doing it, you just need to move the call to plt.xticks(r,x)
to somewhere after you create the figure you're working on. 你正在做的方式,你只需要在创建你正在处理的数字之后将调用
plt.xticks(r,x)
到某个地方。 Otherwise pyplot
will create a new figure for you. 否则
pyplot
将为您创建一个新的数字。
However, I would also consider switching to the more explicit object-oriented interface to matplotlib
. 但是,我还会考虑切换到
matplotlib
更明确的面向对象的接口 。
This way you'd use: 这样你就可以使用:
fig, ax = plt.subplots(1,1) # your only call to plt
ax.bar(r,v,width=0.9,color='red',edgecolor='black', lw=0.5, align='center')
ax.bar(r,j,width=0.6,color='green',edgecolor='black', lw=0.5, align='center')
ax.bar(r,n,width=0.3,color='blue',edgecolor='black', lw=0.5, align='center')
ax.set_xticks(r)
ax.set_xticklabels(x)
ax.axhline(y=0,linewidth=1,color='black')
fig.savefig('fig',dpi=300,format='png',orientation='landscape')
# or use plt.show() to see the figure interactively or inline, depending on backend
# (see Joe Kington's comment below)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.