2

I am a python beginner and trying to make a logarithmic chart of our solar system. Almost done, but I would like to add planet names to the plots. Any tips on how to do it?

Here is my code:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

mass = {
    "Mercury": 0.330*(10**24),
    "Venus": 4.87*(10**24),
    "Earth": 5.97*(10**24)

}

radius = {
    "Mercury": (4879/2)*(10**3),
    "Venus": (12104/2)*(10**3),
    "Earth": (12756/2)*(10**3)
}

df_mass = pd.DataFrame.from_dict(mass, orient='index', columns=['kgs'])
mass = pd.to_numeric(df_mass['kgs'])

df_radius = pd.DataFrame.from_dict(radius, orient='index', columns=['m'])
radius = pd.to_numeric(df_radius['m'])

colors = np.random.rand(3)
scalar = 500

plt.xlabel('Mass (kgs)')
plt.ylabel('Radius (m)')
plt.title('Logarithmic chart of solar system planets')
plt.scatter(mass, radius, c=colors, s=scalar)
plt.grid()
plt.xscale("log")
plt.yscale("log")
plt.show()
  • Does this answer your question? [How to put individual tags for a scatter plot](https://stackoverflow.com/questions/5147112/how-to-put-individual-tags-for-a-scatter-plot) – armin Feb 02 '20 at 16:26
  • https://stackoverflow.com/questions/5147112/how-to-put-individual-tags-for-a-scatter-plot This should solve your issue – Trect Feb 02 '20 at 18:28

2 Answers2

0

Use the annotate method.

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

mass = {
    "Mercury": 0.330*(10**24),
    "Venus": 4.87*(10**24),
    "Earth": 5.97*(10**24)

}

radius = {
    "Mercury": (4879/2)*(10**3),
    "Venus": (12104/2)*(10**3),
    "Earth": (12756/2)*(10**3)
}

###Create a list with the names of the planets
names = ["Mercury", "Venus", "Earth"]

df_mass = pd.DataFrame.from_dict(mass, orient='index', columns=['kgs'])
mass = pd.to_numeric(df_mass['kgs'])

df_radius = pd.DataFrame.from_dict(radius, orient='index', columns=['m'])
radius = pd.to_numeric(df_radius['m'])

colors = np.random.rand(3)
scalar = 500

plt.xlabel('Mass (kgs)')
plt.ylabel('Radius (m)')
plt.title('Logarithmic chart of solar system planets')
plt.scatter(mass, radius, c=colors, s=scalar)

### Add the names of the planets to the graph
for index, name in enumerate(names):
  plt.annotate(name, (mass[name], radius[name]))

plt.grid()
plt.xscale("log")
plt.yscale("log")
plt.show()
Daniel Ocando
  • 3,554
  • 2
  • 11
  • 19
0

I hacked together this solution, which uses the canonical Iis dataset as an example to plot against.

 # Load libraries
from sklearn.ensemble import RandomForestClassifier
from sklearn import datasets
import numpy as np
import matplotlib.pyplot as plt

# Load Iris Flower Dataset
# Load data
iris = datasets.load_iris()
X = iris.data
y = iris.target

# Train A Decision Tree Model
# Create decision tree classifer object
clf = RandomForestClassifier(random_state=0, n_jobs=-1)

# Train model
model = clf.fit(X, y)

# View Feature Importance
# Calculate feature importances
importances = model.feature_importances_

# Visualize Feature Importance
# Sort feature importances in descending order
indices = np.argsort(importances)[::-1]

# Rearrange feature names so they match the sorted feature importances
names = [iris.feature_names[i] for i in indices]


import numpy as np
import matplotlib.pyplot as plt

N = 10
data = X
labels = y

plt.subplots_adjust(bottom = 0.1)
plt.scatter(
    data[:, 0], data[:, 1], marker='o', c=data[:, 2], s=data[:, 3] * 1500,
    cmap=plt.get_cmap('Spectral'))

for label, x, y in zip(labels, data[:, 0], data[:, 1]):
    plt.annotate(
        label,
        xy=(x, y), xytext=(-20, 20),
        textcoords='offset points', ha='right', va='bottom',
        bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.5),
        arrowprops=dict(arrowstyle = '->', connectionstyle='arc3,rad=0'))

plt.show()

enter image description here

ASH
  • 20,759
  • 19
  • 87
  • 200