Home > Mobile >  How to specify the accuracy/gap of the axis in matplotlib from panda dataframe
How to specify the accuracy/gap of the axis in matplotlib from panda dataframe

Time:07-20

I am working with longitude and latitude which can be very precise and currently my graph from matplotlib is not capturing that. Code:


HOUSING_PATH = "datasets/housing"
csv_path = os.path.join(HOUSING_PATH, "property5.csv")
housing = pd.read_csv(csv_path)

housing.plot(kind="scatter", x="Longitude", y="Latitude", alpha=0.1)
plt.show()

sample data

0       153.046109
1       153.045566
2       153.046511
3       153.048725
4       153.045861

Name: Longitude, Length: 8378, dtype: float64
0      -27.457026
1      -27.456798
2      -27.456318
3      -27.456882
4      -27.459056

Name: Latitude, Length: 8378, dtype: float64

enter image description here

How can I graph with the appropriate accuracy in the axis?

UPDATE:

enter image description here

CodePudding user response:

you can set the precision level (number of digits to display after point) using the set_major_formatter() function using FormatStrFormatter as below....

housing.plot(kind="scatter", x="Longitude", y="Latitude", alpha=0.1, figsize=(8, 8))

from matplotlib.ticker import FormatStrFormatter
plt.gca().yaxis.set_major_formatter(FormatStrFormatter('%.6f')) #Show 6 digits... plt.gca().xaxis.set_major_formatter(FormatStrFormatter('%.6f'))

plt.show()

Graph

enter image description here

  • Related