2

I am creating some plots using matplotlib.

For the plots I am using style='sci' with scilimits=(0,0)

Here is the code:

for key in urbs_values.keys():
    # y-Axis (values)
    u[key] = np.array(urbs_values[key])
    o[key] = np.array(oemof_values[key])

    # draw plots
    plt.plot(i, u[key], label='urbs_'+str(key), linestyle='None', marker='x')
    plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 0))
    plt.plot(i, o[key], label='oemof_'+str(key), linestyle='None', marker='.')
    plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 0))

This is usually consistent, but sometimes I am getting values on the y-axis in the format x.x and sometimes I am getting x.xx, and I don't really find it elegant.

Is there a way to force matplotlib to give me always y values in a format like x.x via dynamically scale the scitific notation?

Here is an example plot that I don't like:

hamburg

oakca
  • 1,408
  • 1
  • 18
  • 40
  • You should replace the word "comma" by "decimal" through out your question because it is not a comma and can be confusing for people – Sheldore Mar 27 '19 at 10:16
  • @Bazingaa done. – oakca Mar 27 '19 at 10:18
  • 1
    Possible duplicate of [How to force the Y axis to only use integers in Matplotlib?](https://stackoverflow.com/questions/12050393/how-to-force-the-y-axis-to-only-use-integers-in-matplotlib) – DavidG Mar 27 '19 at 10:18
  • @DavidG not really because I don't wanna change y values into integers. – oakca Mar 27 '19 at 10:19
  • Oh I see now. It's slightly more complicated than I thought. I'll retract the dupe vote – DavidG Mar 27 '19 at 10:27
  • @oakca Please don't reply answers by editing them. Instead, update the question instead and post a comment on the answer. I've copy-pasted the markdown of your edit into your question. Please remember this next time. :) – TrebledJ Mar 27 '19 at 11:04
  • The envisionned algorithm isn't too clear. Say you have a label 1.25e1. Would that become 125e-1? So what exactly determines which exponent to show? – ImportanceOfBeingErnest Mar 27 '19 at 12:24
  • it should be 1.2e1 it should not show 1.25e1 – oakca Mar 27 '19 at 12:44

1 Answers1

0

You can set your limits dynamically depending on the max value in your data:

def grabMaxExponent(values, precision=1):
    """Given a list of numericals, returns the exponent of the max
    value in scientific notation, adjusted to have no sig figs in
    decimal places.

    e.g. 190 > 1.9E+02 > 19E+01
    returns exponent (1)"""

    # Grab max value and convert to scientific notation
    value = format(max(values), f"5.{precision}E")

    # Pull exponent
    a,m = value.split('E+')
    a,b = a.split('.')
    a,b,m = map(int, (a,b,m))

    # If significant figures non-zero, increase exponent to bring them before decimal point
    if b > 0:
        m-=precision

    return m

m = grabMaxExponent(y)

# Set scilimits to m
plt.ticklabel_format(axis='y', style='sci', scilimits=(m,m))

https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.ticklabel_format.html

iacob
  • 20,084
  • 6
  • 92
  • 119
  • gonna check soon :) ty – oakca Mar 27 '19 at 11:06
  • aight I checked it but it was also not consistent so I changed the question. Sorry about that. your answer was creating sometimes integers on the y-axis with 4 digits, and sometimes with 1 or 2 etc... – oakca Mar 27 '19 at 11:48
  • @oakca the number of digits output depends on your data and what you set the value of `precision` to. – iacob Mar 27 '19 at 11:51