Issue
I'm plotting two graphs in python where X and Y axis labels correspond to point values. Due to close data points, labels overlap, impairing readability.
My goal is to maintain the format where each axis label corresponds to a precise point, while ensuring labels don't overlap.
Despite trials with label rotation, font changes and solutions from similar questions, I'm unable to resolve the issue.
Any sugestions for managing overlapping labels without changing the overall label-formatting? A little teadious perhaps, is there a way to move specific overlapping numbers in the axis labels?
import matplotlib.pyplot as plt
import pandas as pd
# Given Data
data = {'v': [0.97, 0.915, 0.892, 0.847, 0.715, 0.472, 0.248, 0.1, 0.053, 0.015],
'i': [0, 0.000915, 0.00270303, 0.00847, 0.021666667, 0.0472, 0.075151515, 0.1, 0.112765957, 0.15],
'p': [0, 0.000837225, 0.002411103, 0.00717409, 0.015491667, 0.0222784, 0.018637576, 0.01, 0.005976596, 0.00225]}
df = pd.DataFrame(data)
# IV-curve Plot
plt.figure(figsize=(8,5))
plt.scatter(df['v'], df['i'], marker='o')
plt.plot(df['v'], df['i'], linestyle='-', marker='o', color='b')
plt.xticks(df['v'],rotation=45,)
plt.yticks(df['i'],rotation=45,)
plt.xlabel('Voltage (V)')
plt.ylabel('Current (I)')
plt.title('IV-Curve')
plt.grid(True)
plt.show()
# Power Curve Plot
plt.figure(figsize=(8,5))
plt.scatter(df['v'], df['p'], marker='o')
plt.plot(df['v'], df['p'], linestyle='-', marker='o', color='b')
plt.xticks(df['v'],rotation=45)
plt.yticks(df['p'],rotation=45)
plt.xlabel('Voltage (V)')
plt.ylabel('Power (P)')
plt.title('Power-Curve')
plt.grid(True)
plt.show()
My ideal expected output/behaviour for two label ticks that are to close to eachother would be to keep all the ticks, and move those that are overlapping from eachother. So the label ticks that are moved are some distance off from the corresponding gridline, but still included. Since I am not familiar with the extent of the possibilities, the best/only solution might be to just remove overlapping ticks and keep one.
Solution
I've written a function that drops ticks that are too close together.
I think matplotlib
might support your requested functionality via its tick locators, in which case that could be a neater solution that the approach below.
import numpy as np
def remove_close_ticks(axes=None,
too_close_x=np.inf,
too_close_y=np.inf,
keep_final_tick=True):
#Ge the current axes if none are supplied
if axes is None: axes = plt.gca()
#Deal with x ticks, then y ticks
for axis in ['x', 'y']:
#Are we modifying the x or y axis
if axis == 'x':
setter = axes.set_xticks
min_distance = too_close_x
else:
setter = axes.set_yticks
min_distance = too_close_y
#Get the tick values for this axis
#And the distance between consecutive ticks
ticks = axes.properties()[axis + 'ticks']
diff = np.abs(np.diff(ticks))
#Keep ticks that are more than the min distance
new_ticks = ticks[np.argwhere(diff > min_distance).ravel()]
#Always keep the final tick if requested
if keep_final_tick: new_ticks = np.append(new_ticks, ticks[-1])
setter(new_ticks)
Usage:
#Decide what is too close for each axis
too_close_v = 0.03
too_close_i = 0.01
too_close_p = 0.005
# IV-curve Plot
.
. <your code>
.
remove_close_ticks(too_close_x=too_close_v, too_close_y=too_close_i)
plt.show() #optional
# Power Curve Plot
.
. <your code>
.
remove_close_ticks(too_close_x=too_close_v, too_close_y=too_close_p)
plt.show() #optional
Answered By - some3128
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.