0

how to round off the value of the x-axis in this graph as it's very long number 6000000 and not looking good. and also how can I make this graph more good-looking and presentable

here is my code

import pandas as pd
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.ticker as mticker
import numpy as np 
plt.rcParams.update({'font.size': 16})
plt.ticklabel_format(style='plain', axis='x', useOffset=False)
df = pd.read_csv("Z:/a.csv", delimiter=",")
plt.plot(df['A'], df['B'], marker='.', linewidth= 2, )
plt.xlabel('Epochs')
plt.ylabel('MM')
plt.xticks(np.arange(0, 20+1, 2))
plt.show()

My graph looks like this enter image description here

however, I need to round-off y-axis like this enter image description here

user12
  • 761
  • 8
  • 24
  • 1
    Does this answer your question? [Specify format of floats for tick labels](https://stackoverflow.com/questions/29188757/specify-format-of-floats-for-tick-labels) – monk Oct 19 '22 at 04:59
  • there are many threads available but i am not able to adopt it according to my code – user12 Oct 19 '22 at 05:05
  • Use scientific notation like [here](https://stackoverflow.com/questions/11577665/change-x-axes-scale-in-matplotlib) or [here](https://stackoverflow.com/questions/28371674/prevent-scientific-notation) – Redox Oct 19 '22 at 05:43
  • Does this answer your question? [Change x axes scale in matplotlib](https://stackoverflow.com/questions/11577665/change-x-axes-scale-in-matplotlib) – karel Oct 19 '22 at 06:10

1 Answers1

1

enter image description here
Use ax.ticklabel_format

...
fig, ax = plt.subplots()
# set the format BEFORE putting "ink" in the figure
ax.ticklabel_format(axis='y', scilimits=(4,4), useMathText=True)
ax.set_ylim((57800,62200))
...

This is exactly (oh well! almost...) the first diagram in question, but with the y-axis labels as in the second diagram in the question.

In [77]: from numpy import array, nan
    ...: from matplotlib.pyplot import subplots, show
    ...: from matplotlib.ticker import MultipleLocator
    ...: 
    ...: y = 10**4*array((nan,20,4,6,8,6,4,25,65,38,40,18,16,4,30,64,35,38,19,13,4))
    ...: fig, ax = subplots()
    ...: ax.plot(y*200)
    ...: ax.set_xticks(range(0, 20+1, 2));
    ...: ax.yaxis.set_major_locator(MultipleLocator(200000))
    ...: ax.ticklabel_format(axis='y', scilimits=((5,5)), useMathText=True)
    ...: show()

enter image description here

gboffi
  • 22,939
  • 8
  • 54
  • 85
  • the problem i am facing is integrating it with my code... i saw the different solution – user12 Oct 19 '22 at 21:49
  • In which respect do you think my code cannot be integrated with yours? Further, could you please explain what do you mean with _"i saw the different solution"_? I really do not understand... – gboffi Oct 20 '22 at 21:47