overruling data frame index when plotting with matplotlib - python

I want to plot with data frames, but sometimes, I want more control over my x-tick labels and it looks like the data frame index is 'overruling' my code. here is the code:
test_df = pd.DataFrame({'cycles':[0,'b',3,'d','e','f','g'],'me':[100,80,99,100,75,100,90], 'you':[100,80,99,100,75,100,90], 'us':[100,80,99,100,75,100,90]})
f, ax = plt.subplots()
x = test_df['me']
x.index = ['a','b','c','d','e','f','g']
print(x)
for a in ax.get_xticklabels():
a.set_text('me')
print(ax.get_xticklabels()[0])
ax.plot(x)
test_df.plot(x = 'cycles', y = 'me')
any idea on easier ways to easily modify x-tick labels for data frames easily without changing the index of the data frame, but easily just on the fly making the x-ticks whatever I want for any data frame column I want?

You can specify the xticks within DataFrame.plot. This is basically just a dummy to ensure the number of tick labels is correct.
Then just set the tick labels manually after the plot.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'cycles':[0,'b',3,'d','e','f','g'],
'me':[100,80,99,100,75,100,90]})
fig, ax = plt.subplots()
test_df.plot(x='cycles', y='me', ax=ax, xticks=test_df.index)
_ = ax.set_xticklabels(test_df['cycles'])
plt.show()
But you should be a bit hesitant of how the xticks aren't automatically generated. Line plots make sense when your values are ordinal. It doesn't seem obvious to me that 0 should be connected with 'b' anymore than 'e' should be connected to 'f'. In this situation a bar plot makes sense, and not-surprisingly, the xticks are generated without issue.
test_df.plot(x='cycles', y='me', kind='bar', legend=False)

Related

How to change joypy joyplot y-axis labels colors

How do you change the colors of the y-axis labels in a joyplot using joypy package?
Here is a sample code where i can change the color if the x-axis labels, but not the y-axis.
import joypy
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
## DATA
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
new_names = ['SepalLength','SepalWidth','PetalLength','PetalWidth','Name']
iris = pd.read_csv(url, names=new_names, skiprows=0, delimiter=',')
## PLOT
fig, axes = joypy.joyplot(iris)
## X AXIS
plt.tick_params(axis='x', colors='red')
## Y AXIS (NOT WORKING)
plt.tick_params(axis='y', colors='red')
I'm pretty sure the issue is because there are mutliple sub-y-axis's, one for each density plot, and they are actually hidden already.
Not sure how to access the y-axis that is actually shown (I want to change the color of "SepalLength")
Joyplot is using Matplotlib
r-beginners' comment worked for me. If you want to change the colors of all the y-axis labels, you can iterate through them like this:
for ax in axes:
label = ax.get_yticklabels()
ax.set_yticklabels(label, fontdict={'color': 'r'})
This results in a warning that you're not supposed to use set_xticklabels() before fixing the tick positions using set_xticks (see documentation here) but with joypy it didn't result in any errors for me.
Here's another solution that just changes the color of the label directly:
for ax in axes:
label = ax.get_yticklabels()
label[0].set_color('red')

How to use a 3rd dataframe column as x axis ticks/labels in matplotlib scatter

I'm struggling to wrap my head around matplotlib with dataframes today. I see lots of solutions but I'm struggling to relate them to my needs. I think I may need to start over. Let's see what you think.
I have a dataframe (ephem) with 4 columns - Time, Date, Altitude & Azimuth.
I produce a scatter for alt & az using:
chart = plt.scatter(ephem.Azimuth, ephem.Altitude, marker='x', color='black', s=8)
What's the most efficient way to set the values in the Time column as the labels/ticks on the x axis?
So:
the scale/gridlines etc all remain the same
the chart still plots alt and az
the y axis ticks/labels remain as is
only the x axis ticks/labels are changed to the Time column.
Thanks
This isn't by any means the cleanest piece of code but the following works for me:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.scatter(ephem.Azimuth, ephem.Altitude, marker='x', color='black', s=8)
labels = list(ephem.Time)
ax.set_xticklabels(labels)
plt.show()
Here you will explicitly force the set_xticklabels to the dataframe Time column which you have.
In other words, you want to change the x-axis tick labels using a list of values.
labels = ephem.Time.tolist()
# make your plot and before calling plt.show()
# insert the following two lines
ax = plt.gca()
ax.set_xticklabels(labels = labels)
plt.show()

Question on Matplotlib bar plot with a pandas dataframe?

I want to create a bar chart with SALES_COV_TYPE as X and ID_ev as Y. I would like to have both the bars in different colors. Rename X axis values, and also rename the X label in legend to something else as can be seen in the image link. Change Id_ev to 'Visits'.
This is my attempt at it and I'm not satisfied with it.
data = {'SALES_COV_TYPE':[84.0,88.0], 'ID_ev':[2360869,882791]}
df = pd.DataFrame(data)
df.plot(kind='bar',y='ID_ev',x='SALES_COV_TYPE')
ax=plt.gca()
my_xticks = ['CAM','GAM']
ax.set_xticklabels(my_xticks)
ax.set_xlabel('Sales Coverage Type')
ax.set_ylabel('Number of Customer Visits')
Plot
I want to create the bar chart using fig, ax method so that when I create subplots in future this becomes a template for me, but I am not being able to get it done. I would not like to use the pandas wrapper for matplotlib if possible. What do you all suggest?
Thanks!
Do you mean something like this:
data = {'SALES_COV_TYPE':[84.0,88.0], 'ID_ev':[2360869,882791], 'SALES_COV_CLASS':['CAM','GAM']}
df = pd.DataFrame(data)
colors = ['green', 'red']
fig, ax = plt.subplots(1, 1, figsize=(4,3))
ax.bar(df['SALES_COV_CLASS'], df['ID_ev'], color = colors)
ax.set_title('Title')
ax.set_xlabel('Sales Coverage Type')
ax.set_ylabel('Number of Customer Visits')
plt.show()
I find it easier to add a column with the name of the group, that way you don't need to reindex the x axis to remove the empty columns.

seaborn barplot with labels for x values (and no hue)

My dataframe contains two columns, I would like to plot their values in a barplot. Like this:
import seaborn as sns
# load sample data and drop all but two columns
tips = sns.load_dataset("tips")
tips= tips[["day", "total_bill"]]
sns.set(style="whitegrid")
ax = sns.barplot(x="day", y="total_bill", data=tips)
On top of this barplot, I would also like to add a legend with labels for each x value. Seaborn supports this, but as far as I can see, it works only when you specify a hue argument. Each label in the legend then corresponds to a hue value.
Can I create a legend with explanations for the x values?
This might be a confusing question. I don't want to rename the label for the axis or the ticks along the axis. Instead, I would like to have a separate legend with additional explanations. My bars give me some nice space to put this legend and the explanations would be too long to have them as ticks.
Is this what you want:
sns.set(style="whitegrid")
ax = sns.barplot(x="day", y="total_bill", data=tips)
ax.legend(ax.patches, ['1','2','3','Something that I can\'t say'], loc=[1.01,0.5])
Output:

Remove default axis labels in matplotlib subplots

How do I prevent matplotlib from adding default axis labels to my subplot? The simple code below also labels the x and y axes for 0 to 1. This is messing up my plot when I add actual data (the new labels are added according to the data but the 0 to 1 labels are still present).
import matplotlib.pyplot as plt
fig, ax1 = plt.subplots()
ax1 = fig.add_subplot(111)
plt.show(block=True)
The above produces this figure:
So when I include actual data it looks like this, with just this additional line of code (final_wavelengths and final_stellar are numpy arrays of equal length):
ax1.plot(final_wavelengths, final_stellar, 'b', label='stellar')
How can I remove the unwanted default 0-1 labels?
I was pointed to similar issue. for me worked explicit assigning to right axes by using ax = fig.axes[0] (I had a list of 2 different axes and needed for work just the first one)

Categories

Resources