Ich versuche, eine Streuungsmatrix zu zeichnen. Ich baue auf das Beispiel in diesem Thread Is there a function to make scatterplot matrices in matplotlib?. Hier habe ich gerade den Code leicht geändert, um die Achse für alle Unterplots sichtbar zu machen. Der modifizierte Code ist unten angegebenRotierender Achsentelegramm für jedes Teilplot
import itertools
import numpy as np
import matplotlib.pyplot as plt
def main():
np.random.seed(1977)
numvars, numdata = 4, 10
data = 10 * np.random.random((numvars, numdata))
fig = scatterplot_matrix(data, ['mpg', 'disp', 'drat', 'wt'],
linestyle='none', marker='o', color='black', mfc='none')
fig.suptitle('Simple Scatterplot Matrix')
plt.show()
def scatterplot_matrix(data, names, **kwargs):
"""Plots a scatterplot matrix of subplots. Each row of "data" is plotted
against other rows, resulting in a nrows by nrows grid of subplots with the
diagonal subplots labeled with "names". Additional keyword arguments are
passed on to matplotlib's "plot" command. Returns the matplotlib figure
object containg the subplot grid."""
numvars, numdata = data.shape
fig, axes = plt.subplots(nrows=numvars, ncols=numvars, figsize=(8,8))
fig.subplots_adjust(hspace=0.05, wspace=0.05)
for ax in axes.flat:
# Hide all ticks and labels
ax.xaxis.set_visible(True)
ax.yaxis.set_visible(True)
# # Set up ticks only on one side for the "edge" subplots...
# if ax.is_first_col():
# ax.yaxis.set_ticks_position('left')
# if ax.is_last_col():
# ax.yaxis.set_ticks_position('right')
# if ax.is_first_row():
# ax.xaxis.set_ticks_position('top')
# if ax.is_last_row():
# ax.xaxis.set_ticks_position('bottom')
# Plot the data.
for i, j in zip(*np.triu_indices_from(axes, k=1)):
for x, y in [(i,j), (j,i)]:
axes[x,y].plot(data[x], data[y], **kwargs)
# Label the diagonal subplots...
for i, label in enumerate(names):
axes[i,i].annotate(label, (0.5, 0.5), xycoords='axes fraction',
ha='center', va='center')
# Turn on the proper x or y axes ticks.
for i, j in zip(range(numvars), itertools.cycle((-1, 0))):
axes[j,i].xaxis.set_visible(True)
axes[i,j].yaxis.set_visible(True)
fig.tight_layout()
plt.xticks(rotation=45)
fig.show()
return fig
main()
Ich kann nicht in der Lage sein scheinen, um die x-Achse Text aller Nebenhandlungen zu drehen. Wie zu sehen ist, habe ich den Trick plt.xticks (rotation = 45) ausprobiert. Aber das scheint die Rotation für den letzten Subplot allein durchzuführen.
+1 Auf einer Randnotiz ist es viel einfacher, nur über "axes.flat" zu iterieren, anstatt über alle i, j-Paare zu fahren. Sie können auch 'plt.setp (ax.get_xticklabels(), rotation = 45)' verwenden anstatt über jede Teilstrichbeschriftung zu iterieren. Das ist nur eine Frage des Stils. –
Stimmen Sie zu, aber die i, j-Iteration war bereits vorhanden und verwendet nur eine Teilmenge aller Achsen. Es ist nicht erforderlich, versteckte Beschriftungen zu drehen. Das "Setp" ist in der Tat eine gute Ergänzung, ich konnte nicht an eine "Axt" Weise denken, es sofort zu tun, das macht den Trick! –