3

I would like to produce some plot over the frequencies. I want to have an x-axis with superscript notation like in here. In addition I need vertical lines with vertical annotation separate kilo and mega Hz.

import numpy as np
import matplotlib.pyplot as plt
band = np.linspace(0,10**12,100)
y = band

plt.plot(band,y)
plt.xlabel('Frequencies')

plt.vlines(10**3, min(y), max(y),colors = 'black', label = 'kilo Hz')
plt.vlines(10**6, min(y), max(y),colors = 'black', label = 'mega Hz')
plt.legend()
plt.show()

I tried use ticker but can't figure it out how to set up it. I tried to follow some examples but got error like AttributeError: 'Figure' object has no attribute 'ticklabel_format' Already spend quite a lot of time on it and don't know how to move forward.

In general I need the x-axis formatted in the similar way, than if plt.xscale('log') but I want to keep linear scale.

Community
  • 1
  • 1
tomasz74
  • 16,031
  • 10
  • 37
  • 51
  • Can you show use the code that _doesn't_ work? – tacaswell May 13 '13 at 18:53
  • 1
    also, use `axvline` instead of `vlines` for your vertical lines. – tacaswell May 13 '13 at 18:54
  • I tried to use `matplotlib.ticker.Formatter` with different configurations, and I tried quite a lot of lines which doesn't exist anymore. Also for `set_major_formatter` I get errors like `object has no attribute` – tomasz74 May 13 '13 at 19:01
  • 1
    Just a thought; if you are gonna have an x-axis spanning values (atleast) from 10**3 to 10**6 with a _linear_ scale, the distance between 10**5 and 10**6 will be 10 times the distance between 10**4 and 10**5, and 100 times (!!) the distance between 10**3 and 10**4. In the example you are linking to, the x-axis _is_ in fact a log-scale. – sodd May 13 '13 at 20:31
  • but what objects were you trying `set_major_formatter` on? That is a method of `axis` (not `axes`) objects. ex: `yaxis = ax.get_yaxis()` – tacaswell May 13 '13 at 20:40

2 Answers2

5

You can define the tick-marks as strings and assign those:

mport numpy as np
import matplotlib.pyplot as plt
band = np.linspace(0,10**12,100)
y = band

plt.plot(band,y)
plt.xlabel("Frequencies")

plt.vlines(10**3, min(y), max(y),colors = 'black', label = 'kilo Hz')
plt.vlines(10**6, min(y), max(y),colors = 'black', label = 'mega Hz')

string_labels = []
for i in range(0,len(y),10):
    string_labels.append(r"$10^{%02d}$" % (i/10.0))

plt.xticks(np.linspace(0,10**12,10),string_labels)

plt.legend()
plt.show()
Schorsch
  • 7,761
  • 6
  • 39
  • 65
  • Thanks. I really like your solution. It gives what I need. I though it can be done as some kind of formatting from the matplotlib level, but this is also nice. – tomasz74 May 13 '13 at 21:06
0

I'm just shooting in the dark, but looks like xlabel takes a variety of options:

http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.xlabel

When I went to:

http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.text

I noticed that there is a verticalalignment option. Maybe that's what you need?

Christopher Spears
  • 1,105
  • 15
  • 32