2
votes

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.

2
Can you show use the code that doesn't work? - tacaswell
also, use axvline instead of vlines for your vertical lines. - tacaswell
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
Just a thought; if you are gonna have an x-axis spanning values (atleast) from 103 to 106 with a linear scale, the distance between 105 and 106 will be 10 times the distance between 104 and 105, and 100 times (!!) the distance between 103 and 104. In the example you are linking to, the x-axis is in fact a log-scale. - sodd
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

2 Answers

4
votes

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()
0
votes

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?