- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试绘制两个列表中的值。 x 轴值是日期。到目前为止尝试过这些东西
year = [20070102,20070806,20091208,20111109,20120816,20140117,20140813]
yvalues = [-0.5,-0.5,-0.75,-0.75,-0.5,-1.25,-1.25]
dates = [datetime.datetime.strptime(str(int(date)),'%Y%m%d') for date in year]
fig, axes = plt.subplots(1, 1, figsize=(16, 9), dpi=100)
line1, = axes.plot(dates,yvalues, lw=2, marker='*', color='r')
axes.legend([line1],['VALUES'],loc=1)
axes.grid()
axes.set_xlabel('YEAR')
axes.set_ylabel('VALUES')
yticks = [-2.0,-1.75,-1.5,-1.25,-1.0,-0.75,-0.5,-0.25,0.0]
axes.set_yticks(yticks)
axes.set_yticklabels(["$%.2f$" % y for y in yticks]);
axes.set_xticks(dates)
#axes.set_xticklabels()
这个数字看起来不错。但无法正确标记 x 轴刻度。我试图用 2007-01-02 或 2007 年一月之类的东西来标记 x 轴。
提前致谢
最佳答案
您可以使用 DateFormater
控制日期格式:
import matplotlib.dates as mdates
xfmt = mdates.DateFormatter('%Y-%m-%d')
axes.xaxis.set_major_formatter(xfmt)
import datetime as DT
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
year = [20070102,20070806,20091208,20111109,20120816,20140117,20140813]
yvalues = [-0.5,-0.5,-0.75,-0.75,-0.5,-1.25,-1.25]
dates = [DT.datetime.strptime(str(int(date)),'%Y%m%d') for date in year]
fig, axes = plt.subplots(1, 1, figsize=(16, 9), dpi=100)
line1, = axes.plot(dates,yvalues, lw=2, marker='*', color='r')
axes.legend([line1],['VALUES'],loc=1)
axes.grid()
axes.set_xlabel('YEAR')
axes.set_ylabel('VALUES')
yticks = [-2.0,-1.75,-1.5,-1.25,-1.0,-0.75,-0.5,-0.25,0.0]
axes.set_yticks(yticks)
axes.set_yticklabels(["$%.2f$" % y for y in yticks]);
xfmt = mdates.DateFormatter('%Y-%m-%d')
axes.xaxis.set_major_formatter(xfmt)
axes.set_xticks(dates)
plt.xticks(rotation=25)
plt.show()
产量
关于python - 如何在 matplotlib 中设置日期的 xticklabels,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28385184/
这个问题在这里已经有了答案: 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
我是一名优秀的程序员,十分优秀!