- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我真的很苦恼 matplotlib,尤其是 Axis 设置。我的目标是在一个图中设置 6 个子图,它们都显示不同的数据集但具有相同数量的刻度标签。
我源代码的相关部分如下所示:
graph4.py:
# Import Matolotlib Modules #
import matplotlib as mpl
from matplotlib.figure import Figure
from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
from matplotlib import ticker
import matplotlib.pyplot as plt
mpl.rcParams['font.sans-serif']='Arial' #set font to arial
# Import GTK Modules #
import gtk
#Import System Modules #
import sys
# Import Numpy Modules #
from numpy import genfromtxt
import numpy
# Import Own Modules #
import mysubplot as mysp
class graph4():
weekdays = ['Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag']
def __init__(self, graphview):
#create new Figure
self.figure = Figure(figsize=(100,100), dpi=75)
#create six subplots within self.figure
self.subplot = []
for j in range(6):
self.subplot.append(self.figure.add_subplot(321 + j))
self.__conf_subplots__() #configure title, xlabel, ylabel and grid of all subplots
#to make it look better
self.figure.subplots_adjust(left=0.125, bottom=0.1, right=0.9, top=0.96, wspace=0.2, hspace=0.6)
#Matplotlib <-> GTK
self.canvas = FigureCanvas(self.figure) # a gtk.DrawingArea
self.canvas.set_flags(gtk.HAS_FOCUS|gtk.CAN_FOCUS)
self.canvas.grab_focus()
self.canvas.show()
graphview.pack_start(self.canvas, True, True)
#add labels and grid to all subplots
def __conf_subplots__(self):
index = 0
for i in self.subplot:
mysp.conf_subplot(i, 'Zeit', 'Menge', graph4.weekdays[index], True)
i.plot([], [], 'bo') #empty plot
index +=1
def plot(self, filename_list):
index = 0
for filename in filename_list:
data = genfromtxt(filename, delimiter=',') #load data from filename
if data.size != 0: #only if file isn't empty
if index <= len(self.subplot): #plot every file on a different subplot
mysp.plot(self.subplot[index],data[0:, 1], data[0:, 0])
index +=1
self.canvas.draw()
def clear_plot(self):
#clear axis of all subplots
for i in self.subplot:
i.cla()
self.__conf_subplots__()
mysubplot.py:(辅助模块)
# Import Matplotlib Modules
from matplotlib.axes import Subplot
import matplotlib.dates as md
import matplotlib.pyplot as plt
# Import Own Modules #
import mytime as myt
# Import Numpy Modules #
import numpy as np
def conf_subplot(subplot, xlabel, ylabel, title, grid):
if(xlabel != None):
subplot.set_xlabel(xlabel)
if(ylabel != None):
subplot.set_ylabel(ylabel)
if(title != None):
subplot.set_title(title)
subplot.grid(grid)
#rotate xaxis labels
plt.setp(subplot.get_xticklabels(), rotation=30, fontsize=12)
#display date on xaxis
subplot.xaxis.set_major_formatter(md.DateFormatter('%H:%M:%S'))
subplot.xaxis_date()
def plot(subplot, x, y):
subplot.plot(x, y, 'bo')
我认为解释错误的最好方法是使用屏幕截图。在我开始我的应用程序之后,一切看起来都很好:
如果我双击左侧的“周”条目,将调用 graph4.py 中的方法 clear_plot()
来重置所有子图。然后将文件名列表传递给 graph4.py 中的方法 plot()
。 plot()
方法打开每个文件并在不同的子图中绘制每个数据集。所以在我双击一个条目后,它看起来像:
如您所见,每个子图都有不同数量的 xtick 标签,这在我看来非常难看。因此,我正在寻找一种解决方案来改善这一点。我的第一种方法是使用 xaxis.set_ticklabels()
手动设置刻度标签,这样每个子图都有相同数量的刻度标签。然而,听起来很奇怪,这只适用于某些数据集,我真的不知道为什么。在某些数据集上,一切正常,而在其他数据集上,matplotlib 基本上在做它想做的事情,并显示我没有指定的 xaxis 标签。我也尝试了 FixedLocator()
,但我得到了相同的结果。在某些数据集上它正在工作,而在其他数据集上,matplotlib 使用不同数量的 xtick 标签。
我做错了什么?
编辑:
正如@sgpc 所建议的,我尝试使用 pyplot。我的源代码现在看起来像这样:
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
import matplotlib.dates as md
mpl.rcParams['font.sans-serif']='Arial' #set font to arial
import gtk
import sys
# Import Numpy Modules #
from numpy import genfromtxt
import numpy
# Import Own Modules #
import mysubplot as mysp
class graph2():
weekdays = ['Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag']
def __init__(self, graphview):
self.figure, temp = plt.subplots(ncols=2, nrows=3, sharex = True)
#2d array -> list
self.axes = [ y for x in temp for y in x]
#axis: date
for i in self.axes:
i.xaxis.set_major_formatter(md.DateFormatter('%H:%M:%S'))
i.xaxis_date()
#make space and rotate xtick labels
self.figure.autofmt_xdate()
#Matplotlib <-> GTK
self.canvas = FigureCanvas(self.figure) # a gtk.DrawingArea
self.canvas.set_flags(gtk.HAS_FOCUS|gtk.CAN_FOCUS)
self.canvas.grab_focus()
self.canvas.show()
graphview.pack_start(self.canvas, True, True)
def plot(self, filename_list):
index = 0
for filename in filename_list:
data = genfromtxt(filename, delimiter=',') #get dataset
if data.size != 0: #only if file isn't empty
if index < len(self.axes): #print each dataset on a different subplot
self.axes[index].plot(data[0:, 1], data[0:, 0], 'bo')
index +=1
self.canvas.draw()
#not yet implemented
def clear_plot(self):
pass
如果我绘制一些数据集,我会得到以下输出: http://i.imgur.com/3ngYTNr.png (对不起,我还没有足够的声誉来嵌入图片)
此外,我不确定共享 x Axis 是否是一个真正的好主意,因为每个子图中的 x 值可能不同(例如:在第一个子图中,x 值范围从上午 8:00 - 上午 11:00,在第二个子图中,x 值的范围是晚上 7:00 - 晚上 9:00)。
如果我去掉 sharex = True
,我会得到以下输出:
http://i.imgur.com/rxHeSyJ.png (对不起,我还没有足够的声誉来嵌入图片)
如您所见,输出现在看起来更好了。但是现在,x Axis 上的标签没有更新。我假设那是因为最后的 suplots 是空的。
我的下一次尝试是为每个子图使用一个轴。因此,我做了以下更改:
for i in self.axes:
plt.setp(i.get_xticklabels(), visible=True, rotation = 30) #<-- I added this line...
i.xaxis.set_major_formatter(md.DateFormatter('%H:%M:%S'))
i.xaxis_date()
#self.figure.autofmt_xdate() #<--changed this line
self.figure.subplots_adjust(left=0.125, bottom=0.1, right=0.9, top=0.96, wspace=0.2, hspace=0.6) #<-- and added this line
现在我得到以下输出:
i.imgur.com/TmA1goE.png(抱歉,我还没有足够的声望来嵌入图片)
所以通过这次尝试,我基本上遇到了与 Figure()
和 add_subplot()
相同的问题。
我真的不知道,还有什么我可以尝试让它工作......
最佳答案
我强烈建议您使用 pyplot.subplots()
和 sharex=True
:
fig, axes = subplots(ncols=2, nrows=3, sharex= True)
然后您使用以下方法访问每个轴:
ax = axes[i,j]
你可以策划做:
ax.plot(...)
要控制每个 AxesSubplot
的刻度数,您可以使用:
ax.locator_params(axis='x', nbins=6)
OBS:axis
可以是 'x'
、'y'
或 'both'
关于python - matplotlib 中具有相同数量 xticklabels 的六个子图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17379351/
这个问题在这里已经有了答案: Aligning rotated xticklabels with their respective xticks (6 个答案) 关闭上个月。 如何在 matplot
我在 Matlab 2014b 中有一个简单的绘图。例如: b1 = bar(1:12, rand(12,1)); ax1 = gca; 使用 XTickLabels 属性时弹出如下警告信息: xti
我有一个情节: x = [0 1 2 3]; y = [0 0 1 1]; plot (x, y, 'k'); 我想在 X 轴上使用标签,它有效: set(gca,'XTick',[0:3]) % T
我有一个情节: x = [0 1 2 3]; y = [0 0 1 1]; plot (x, y, 'k'); 我想在 X 轴上使用标签,它有效: set(gca,'XTick',[0:3]) % T
我尝试使用 http://www.mathworks.com/matlabcentral/fileexchange/23841按如下方式执行此操作: data=rand(5); plot(0:pi/4
我正在遵循示例代码 here生成条形图。我只想在最后一个组名“G5”上加一个上标\textdagger(†)。这是我尝试过的(检查 ax.set_xticklabels 行): import nump
在 R2010b 中,我可以通过图形编辑器将刻度标签重置为自动, 但我想以编程方式将它们重置为默认值 我试过了 set(gca,'XTickLabel','auto') 但它在每次滴答时都显示“自动”
我真的很苦恼 matplotlib,尤其是 Axis 设置。我的目标是在一个图中设置 6 个子图,它们都显示不同的数据集但具有相同数量的刻度标签。 我源代码的相关部分如下所示: graph4.py:
如何绘制次网格为 1、主网格为 10 且 xticklabels 增量为 20 的图表? 这是我的示例代码和每 10 个单位增加 xticklabels 的输出: plt.figure(figsize
在 MATLAB 中,可以更改绘图的 ylabel、xlabel、title 等字体名称,例如 ylabel(y_string, 'FontName', 'Palatino') 但是,是否可以更改 X
我正在尝试绘制两个列表中的值。 x 轴值是日期。到目前为止尝试过这些东西 year = [20070102,20070806,20091208,20111109,20120816,20140117,2
我在 matplotlib 中使用 matshow(),生成的图形默认在顶部有 xticklabels。有没有办法将 xticklabels 放在底部? 感谢对此的任何帮助。/M 最佳答案 尝试执行以
所有,我正在使用 Matplotlib 绘制极坐标。但是刻度是重叠的。 有人知道如何避免这种情况吗?谢谢! from pylab import * import matplotlib.pyp
检查下图的 x 轴。我怎样才能将标签向左移动一点,以便它们与各自的刻度线对齐? 我正在使用以下方法旋转标签: ax.set_xticks(xlabels_positions) ax.set_xtick
检查下图的x轴。如何将标签向左移动一点,以便它们与各自的刻度对齐? 我使用以下方式旋转标签: ax.set_xticks(xlabels_positions) ax.set_xticklabels(x
我正在尝试创建一个顶部带有 xticklabel 的 Seaborn 热图(尝试使用条件格式模拟 Excel 表格)。 我使用的代码如下: plt.figure(figsize=[12, 6]) ax
我有一些数据,我正在使用 pd.cut 将它们分箱 import pandas as pd import matplotlib.pyplot as plt garbage = 50*np.random
我正在尝试修改绘图的 x 刻度,但由 matplotlib 自动生成的刻度标签之一不会消失,并且我指定的刻度标签与它重叠。代码是: ax[0].plot((zspace+1.),dndt_vs_z_9
我正在尝试在 matplotlib 中制作条形图。我的情节更复杂,但主要问题是 xlabels 的对齐。 我现在有以下内容: import matplotlib.pyplot as plt impor
以下代码生成一个条形图,其中 xticklabels 以每个条为中心。但是,缩放 x 轴、更改条形数量或更改条形宽度确实会改变标签的位置。是否有处理该行为的通用方法? # This code is a
我是一名优秀的程序员,十分优秀!