user12
user12

Reputation: 761

how to round-off the value of the x-axis in python line chart

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

Upvotes: 0

Views: 872

Answers (1)

gboffi
gboffi

Reputation: 25023

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

Upvotes: 1

Related Questions