conquistador
conquistador

Reputation: 693

Plotting two dictionaries on twin Y axis with Matplotlib

I'm trying to plot two dictionaries on twiny() axis but I need to set the dictionary values to negative just to plot it properly and set the all negative labels to positive. Is there a better way to do this?

import random

import matplotlib
import matplotlib.pyplot as plt

plt.style.use('dark_background')
matplotlib.rcParams.update({'font.size': 15, 'font.family': 'monospace', "figure.figsize": (18.8, 8)})

fig, axes = plt.subplots()
ax2 = axes.twiny()

team_a_points = {
    "PLAYER A": random.randint(0, 3000),
    "PLAYER B": random.randint(0, 3000),
    "PLAYER C": random.randint(0, 3000),
    "PLAYER E": random.randint(0, 3000),
    "PLAYER F": random.randint(0, 3000),
}

# VALUES MUST BE POSITIVE
team_b_points = {
    "PLAYER G": -random.randint(0, 3000),
    "PLAYER H": -random.randint(0, 3000),
    "PLAYER I": -random.randint(0, 3000),
    "PLAYER J": -random.randint(0, 3000),
    "PLAYER K": -random.randint(0, 3000),
}
print(team_a_points)

axes.set_ylim([-3000, 3000])
axes.tick_params(labelright=True, axis="y", which="both")
axes.grid(color='w', ls='-.', lw=0.5)

ax2.plot(list(team_a_points.keys()), list(team_a_points.values()))
axes.plot(list(team_b_points.keys()), list(team_b_points.values()))

ax2.tick_params(axis='x', labelrotation=45)
axes.tick_params(axis='x', labelrotation=45)
plt.draw()

# "−" == "-" = False? Why is matplotlib returning a different minus sign?
axes.set_yticklabels(list(label.get_text().replace('−', '') for label in axes.get_yticklabels()))
plt.tight_layout()
plt.savefig("test.png")

Result: enter image description here

Upvotes: 0

Views: 194

Answers (1)

conquistador
conquistador

Reputation: 693

Just us two subplots, invert the other one and remove the spacing between the subplots.

import random

import matplotlib
import matplotlib.pyplot as plt
import string

plt.style.use('dark_background')
matplotlib.rcParams.update({'font.size': 15, 'font.family': 'monospace', "figure.figsize": (18.8, 8)})

team_a_points = {f"{''.join(random.sample(string.ascii_letters + string.digits, 8))}": random.randint(0, 3000) for _ in
                 range(12)}

team_b_points = {f"{''.join(random.sample(string.ascii_letters + string.digits, 8))}": random.randint(0, 3000) for _ in
                 range(12)}

fig, axs = plt.subplots(2)

axis1, axis2 = axs

axis1.xaxis.tick_top()

axis1.set_ylim([0, 3000])
axis2.set_ylim([0, 3000])

axis1.plot(list(team_a_points.keys()), list(team_a_points.values()))
axis2.plot(list(team_b_points.keys()), list(team_b_points.values()))
axis2.invert_yaxis()

axis1.tick_params(axis='x', labelrotation=45, labelright=True)
axis2.tick_params(axis='x', labelrotation=45, labelright=True)

axis1.tick_params(axis='y', labelright=True)
axis2.tick_params(axis='y', labelright=True)

axis1.grid(color='w', ls='-.', lw=0.5)
axis2.grid(color='w', ls='-.', lw=0.5)

plt.tight_layout(w_pad=0.0, h_pad=0.0)
plt.subplots_adjust(hspace=0.0, wspace=0.0)
plt.savefig("test.png")

enter image description here

Upvotes: 1

Related Questions