0

I am new to python and NetworkX and I'm facing trouble naming my ticks on the y-axis. I tried using plt.yticks() and ax.set_ylabels() but it does not label all the ticks.

This is the code:

fig, ax = plt.subplots(figsize=(10, 5))
plt.title('Transportation Network')
#nx.draw_networkx_labels(GR, pos_dict)
nx.draw(g, pos=node_positions, edge_color=edge_colors, node_size=40, node_color='black', ax=ax)
plt.xlabel('Time ( 1 epoch= 15 mins)')
plt.ylabel("Cities")
ax.set_axis_on()
ax.tick_params(left=True, bottom=True, labelleft=True, labelbottom=True)
ax.set_yticklabels(['C','B','A'])
plt.locator_params(axis="y", nbins=3)

I want it to look like this with A, B, and C on the 3 ticks in the y-axis.

enter image description here

  • 1
    `ax.tick_params(..., labelleft=True)` and `ax.set_yticklabels(['C','B','A'])` – JohanC Nov 18 '21 at 15:33
  • Thank you for your reply Johan but I got the following error: ValueError: Ellipsis is not a valid value for axis; supported values are 'x', 'y', 'both' – Romio Rodriguez Nov 18 '21 at 15:39
  • Thank you for your help! I tried this code but it only labeled the last two ticks A and B respectively, leaving the first tick as it is without any labels. I edited the question to show that. ax.tick_params(left=True, bottom=True, labelleft=True, labelbottom=True) ax.set_yticklabels(['C','B','A']) – Romio Rodriguez Nov 18 '21 at 15:49
  • 1
    Before `ax.set_yticklabels(['C','B','A'])` you could call `ax.set_yticks([0,1,2])` to mark the correct positions. And leave out `plt.locator_params` – JohanC Nov 18 '21 at 15:58
  • THANK YOU! However, I get [Text(0, 0, 'A'), Text(0, 0, 'B'), Text(0, 0, 'C')], on top of the graph. How can I get rid of that? – Romio Rodriguez Nov 18 '21 at 16:01
  • 2
    Use `plt.show()` at the end – JohanC Nov 18 '21 at 16:02

0 Answers0