Reputation: 133
import matplotlib.pyplot as plt
import numpy as np
x1 = [0, 0.02, 0.04, 0.08, 0.12, 0.16, 0.2]
y1 = [0.0005, 0.052, 0.0905, 0.1675, 0.2485, 0.3225, 0.4035]
plt.scatter(x1, y1)
plt.title("y-x scatter")
plt.xlabel("x")
plt.ylabel("y")
plt.show()
but what my goal is an image like this:
How can I rotate the Image like this using matplotlib?
Upvotes: 2
Views: 13212
Reputation: 339102
You cannot rotate the toolbar on the plot without completely rewriting it. That seems a bit too much work so I'll leave that out.
Apart from that there is no big problem rotating all the elements in the plot and exchanging the role of x and y axis. Specifically,
rotation
argument.scatter(y,x)
.ax.invert_xaxis()
ax.yaxis.tick_right()
Compelte code:
import matplotlib.pyplot as plt
x1 = [0, 0.02, 0.04, 0.08, 0.12, 0.16, 0.2]
y1 = [0.0005, 0.052, 0.0905, 0.1675, 0.2485, 0.3225, 0.4035]
fig,ax=plt.subplots(figsize=plt.rcParams["figure.figsize"][::-1])
fig.subplots_adjust(left=0.1, right=0.875, top=0.9,bottom=0.125)
ax.scatter(y1, x1)
ax.set_ylabel("x", rotation=90)
ax.yaxis.tick_right()
ax.yaxis.set_label_position("right")
ax.set_xlabel("y", rotation=180)
ax.invert_xaxis()
plt.setp(ax.get_xticklabels(), rotation=90, va="top", ha="center")
plt.setp(ax.get_yticklabels(), rotation=90, va="center", ha="left")
ax.text(-0.05,0.5,"y-x scatter", ha="center", va="center",
transform=ax.transAxes, rotation=90)
plt.show()
Upvotes: 2
Reputation: 3219
If you don't care about having negative numbers on the scale you can simply: plt.scatter([-y for y in y1], x1)
You'll also need to change the labels on the axes, but that would plot x on the vertical axis and y on horizontal, negative values to go from right to left.
If on the other hand you want to rotate the image, you can save it and rotate to file, rotate with another tool and read that file.
Upvotes: 0