简体   繁体   English

在x轴上以奇数和负数绘制刻度线-Python

[英]Plotting ticks on the x axis with positive and negative values, in strange order -Python

I have a plot where I need to change the ticks in the xaxis, this way: the x values have to range from 0 to 32 and THEN (CONSECUTIVELY, WITHOUT INTERRUPTION) from -31 to -1. 我有一个图,需要以这种方式更改x轴上的刻度:x值的范围必须从0到32,然后(-连续地,无中断)从-31到-1。

Here is the code to produce such an xaxis, at least as an array: 下面是产生这样的xaxis的代码,至少作为一个数组:

nyquist = int(64/2) +1
xaxis1 = np.arange(1, nyquist)
xaxis2 = np.arange(-(int(nyquist)-1), 0)
xaxis = np.concatenate((xaxis1, xaxis2))

The result for xaxis is: xaxis的结果是:

array([  1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,
        14,  15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26,
        27,  28,  29,  30,  31,  32, -31, -30, -29, -28, -27, -26, -25,
       -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12,
       -11, -10,  -9,  -8,  -7,  -6,  -5,  -4,  -3,  -2,  -1])

Then imagine that the curve to plot is stored in: 然后,假设要绘制的曲线存储在:

curve = np.zeros(64, float)
for k in range(1, nyquist):
    curve[k] = np.exp(-(k**2))
for k in range(int(n/2+1), n):
    curve[k] = curve[n-k]

OK, now I need to create a plot, but with the ticks given by "xaxis" (it's OK if I don't plot all the 64 values, but just 6 of them, for example, but the order must be respected, making explicit the presence of NEGATIVE values in that strange order). 好的,现在我需要创建一个绘图,但是要用“ xaxis”给出的刻度(如果我不绘制所有64个值,则可以,但是仅绘制其中的6个是可以的,但是必须遵守顺序,以这种奇怪的顺序明确显示负值)。 That's what I used to do, but this does not produce what wanted: 那是我曾经做过的,但是并没有产生想要的结果:

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(np.arange(64), curve, 'ro-')

because obviously this would put on the x-axis 64 values ranging from 0 to 63, not what I need. 因为很明显,这会在x轴上放置0到63之间的64值,而不是我所需要的。

How can I do? 我能怎么做?

EDIT: 编辑:

I am providing a piece of runnable code. 我提供了一段可运行的代码。

import matplotlib as plt
import numpy as np

n = 64
nyquist = int(n/2) +1
xaxis1 = np.arange(1, nyquist)
xaxis2 = np.arange(-(int(nyquist)-1), 0)
xaxis = np.concatenate((xaxis1, xaxis2))

curve = np.zeros(n, float)
for k in range(1, nyquist):
    curve[k] = np.exp(-(k**2))
for k in range(int(n/2+1), n):
    curve[k] = curve[n-k]

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(np.arange(64), curve, 'ro-')
plt.savefig('curve.svg')

This only produces the "wrong" plot, the one with only positive x values 这只会产生“错误”的图,只有正x值的图

You can use xticks as: 您可以将xticks用作:

plt.xticks(range(len(xaxis)), xaxis)

You can also rotate the xtick values in case of tick overlapping as: 如果刻度线重叠,您还可以旋转xtick值,如下所示:

plt.xticks(range(len(xaxis)), xaxis, rotation='vertical')

You can create xaxis in this simple way: 您可以通过以下简单方式创建xaxis:

>>> range(1,33)+range(-32,0, 1)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, -32, -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM