0

I would like to plot with matplotlib.pyplot. For the ticks at the x-axis I would like to get integer number multiplied by powers of 10. However, what I get is an integer number multiplied by a float and a power of 10, like it is shown in the figure below.

enter image description here

Deactivating this behavior with:

ax.get_xaxis().get_major_formatter().set_useOffset(False)

means that the tick labels will be integers only.

For example, I would like the number 35000 to be 35 and the multiplier on the axis to be 10^3 or 3.5 and the multiplier to be 10^4. Not 25 and the multiplier to be 1.4 * 10^3.


Update: The code that I used to generate the plot:

fig = plt.figure(figsize=(5, 5), dpi=80)
ax = fig.add_subplot(111)

data = read_data(filename)

ax.hist(data, bins=50)

x_median = np.median(data)
x_std = np.std(data)
ax.set_xlim([x_median - 4*x_std, x_median + 4*x_std])

Also, another plot that uses the same code but doesn't have the same issue. enter image description here

Grey
  • 115
  • 9
  • Could you provide the code that you use to generate the plot, along with some sample input data, and the version of matplotlib you are using? – Mad Physicist Oct 30 '17 at 19:24
  • The problem is that this is not reproducible. When using some random data, the plot might look like [○this](https://i.stack.imgur.com/xulQM.png). So you really need to provide a [mcve] of the issue. – ImportanceOfBeingErnest Oct 30 '17 at 20:53
  • Sorry, I am under constrains and I cannot provide any data. My question is simple: How can I deal with the x-axis ticks in a data-independent way? I am not looking for a custom solution that will work with one specific graph and fail to work for the rest. It is not only one or two figures that I have right now. I will ask for permission, and I might be able to share some data. – Grey Oct 31 '17 at 02:23
  • You have misunderstood [mcve]. We do not want your secret data. A [mcve] is meant exactly for the purpose of not having to give someone the actual code or data and still let them reproduce the issue. The problem is that this issue is not reproducible. However in order to find a good solution one would first need to know how to obtain the undesired behaviour to see what causes it and then come up with a solution. So the usual way is to hardcode some data in the code, such that it is runnable and reproduces the issue. – ImportanceOfBeingErnest Oct 31 '17 at 10:11

1 Answers1

0

To me it looks like it is working as intended. Your values do not get multiplied but are offset by 1.007x10^5. In the beginning you say that you want integers multiplied by powers of 10 but how would you represent 10750 in this way? The only way without rounding would be 1075*10^1. It gets clearer in your example in which you allow a float multiplied by powers of 10. You simply want a scientific format. Originally from https://stackoverflow.com/a/6654046/15237183 and with some reading of https://matplotlib.org/stable/api/ticker_api.html#matplotlib.ticker.ScalarFormatter, I suggest you try

from matplotlib.ticker import ScalarFormatter

y_formatter = ScalarFormatter(useOffset=False, useMathText=True)
ax.yaxis.set_major_formatter(y_formatter)

enter image description here

useOffset=False will switch to scientific notation instead of offset notation. While useMathText=True formats the powers as x10 instead of 1e.

WiseKouichi
  • 73
  • 1
  • 5