I'm trying to have two y-axes with the same x-axis.
This is what I have tried. But the suicide rates are not showing up on the graph.
I'm new to this, so I was wondering if anyone could spot why its not showing.
The picture is supposed to look like this with suicide rates in red and trust in blue with country as the x-axis
def suicidevstrustcountryplot(dat):
# Does income index change trust for female led countries?
# dat.plot(x ='Country', y='Income', kind = 'line')
# plt.show()
# create figure and axis objects with subplots()
fig,ax = plt.subplots()
# make a plot
ax.plot(dat.Country, dat.Trust, color="red", marker="o")
# set x-axis label
ax.set_xlabel("Country",fontsize=14)
for label in ax.get_xticklabels():
label.set_rotation(90)
label.set_ha('right')
# set y-axis label
ax.set_ylabel("Trust",color="red",fontsize=14)
# twin object for two different y-axis on the sample plot
ax2=ax.twinx()
# make a plot with different y-axis using second axis object
ax2.plot(dat.Country, dat.Trust,color="blue",marker="o")
ax2.set_ylabel("Suicide rate",color="blue",fontsize=14)
plt.show()
# save the plot as a file
fig.savefig('two_different_y_axis_for_single_python_plot_with_twinx.jpg',
format='jpeg',
dpi=100,
bbox_inches='tight')
suicidevstrustcountryplot(Femaletrust)
suicidevstrustcountryplot.suicidevstrustcountryplot.sort_values(ascending=False)[:10].plot(kind='scatter' ,title='Country')
Related
I have the following code, which almost does what I need it to do. I am graphing the importance of each feature for two different models on the same graph for comparison. I can't seem to get them to show side by side as two separate bars. I am fairly new to python and brand new to this forum. here is the code:
def plot_importances1(model1, feature_names1, label1, model2=None,feature_names2=None, label2=None):
if model2 is None:
importances1 = model1.feature_importances_
indices1 = np.argsort(importances1)
plt.figure(figsize=(8, 8)) # Set figure size
# plot the first list of feature importances as a horizontal bar chart
plt.barh(range(len(indices1)), importances1[indices1], color="violet", align="center", label=label1)
# set the y-axis tick labels to be the feature names
plt.yticks(range(len(indices1)), [feature_names1[i] for i in indices1])
else:
importances1 = model1.feature_importances_
indices1 = np.argsort(importances1)
importances2 = model2.feature_importances_
indices2 = np.argsort(importances2)
plt.figure(figsize=(8, 8)) # Set figure size
# plot the first list of feature importances as a horizontal bar chart
plt.barh(range(len(indices1)), importances1[indices1], color="violet", align="center", label=label1)
# plot the second list of feature importances as a horizontal bar chart
plt.barh(range(len(indices2)), importances2[indices2], color="orange", align="center", label=label2)
# set the y-axis tick labels to be the feature names
plt.yticks(range(len(indices1)), [feature_names1[i] for i in indices1])
# add a title and x- and y-axis labels
plt.title("Feature Importances")
plt.xlabel("Relative Importance")
plt.ylabel("Feature")
# add a legend to the plot
plt.legend()
# set the tick locations and labels for the first bar graph
plt.gca().tick_params(axis='x', which='both', length=0)
plt.gca().xaxis.set_ticks_position('top')
plt.gca().xaxis.set_label_position('top')
# set the tick locations and labels for the second bar graph
plt.twinx()
plt.gca().tick_params(axis='x', which='both', length=0)
plt.gca().xaxis.set_ticks_position('bottom')
plt.gca().xaxis.set_label_position('bottom')
plt.show()
Then I call the function:
plot_importances1(
dTree_treat_out,
list(X1_train),
"Outliers present",
dTree,
list(X_train),
"No outliers",
)
The two bars are both showing, but I can't get them to separate completely and I am getting this error:
Output for the code
I have ran several version of this, including one that does not return the matplotlib error. The problem with the other function definitions that I have is that the bars are stacked and I can't see both of them. If I knew how to make one less opaque? I am super stuck. I so not want them stacked, I need the first one to be its own graph with the second one NEXT to it, not overlaying or stacked on top, similar to the image I uploaded, but the bars need to be completely separated.
Any input to fix this issue will be greatly appreciated.
I working on this plot and I would like to increase the ticks on the X-axis to be a bit more, but I'm stuck on it. I can't find a good example that uses Pandas plot to do this.
I only got 8 ticks on the X-axis, I would like to double it, at least. How do I get this done?
With ax.xaxis.set_major_locator(MonthLocator()) I get more ticks, but then the text is overlapping, and I can't get it to rotate.
ax.set_xticklabels(ax.get_xticks(), rotation = 50) did nothing.
# Place in DataFrame
df_avg = pd.DataFrame(pd.read_sql_query(query_avg, con))
df_total = pd.DataFrame(pd.read_sql_query(query_total, con))
con.close()
# Plot data from the DB
ax = df_avg.plot(x='dag', y='day_avg', figsize=(25, 5))
ax2 = df_avg.plot(x='dag', y='avg_temp', secondary_y=True, ax=ax)
# Set Labels
ax.set_xlabel('Time', size=12)
ax.set_ylabel('Avg amount earn (€)', size=12)
ax2.set_ylabel('Avg temp (°C)', size=12)
I have the following code to print out columns from a pandas dataframe as two histograms:
df = pd.read_csv('fairview_Procedure_combined.csv')
ax = df.hist(column=['precision', 'recall'], bins=25, grid=False, figsize=(12,8), color='#86bf91', zorder=2, rwidth=0.9)
ax = ax[0]
for x in ax:
# Despine
x.spines['right'].set_visible(False)
x.spines['top'].set_visible(False)
x.spines['left'].set_visible(False)
# Switch off ticks
x.tick_params(axis="both", which="both", bottom="off", top="off", labelbottom="on", left="off", right="off", labelleft="on")
# Draw horizontal axis lines
vals = x.get_yticks()
for tick in vals:
x.axhline(y=tick, linestyle='dashed', alpha=0.4, color='#eeeeee', zorder=1)
# Remove title
x.set_title("")
# Set x-axis label
x.set_xlabel("test", labelpad=20, weight='bold', size=12)
# Set y-axis label
x.set_ylabel("count", labelpad=20, weight='bold', size=12)
# Format y-axis label
x.yaxis.set_major_formatter(StrMethodFormatter('{x:,g}'))
which gives the attached output:
I would like however to have different labels on the x-axis (in particular, those listed in my column list, that is, precision and recall)
Also, I have a grouping column (semantic_type) I would like to use to generate a bunch of paired graphs, but when I pass the by keyword in my hist method to group the histograms by semantic_type, I get an error of color kwarg must have one color per data set. 18 data sets and 1 colors were provided)
I figured it out using subplots... piece of cake.
I am trying to plot the below dataset as barplot cum pointplot using seaborn.
But the time-stamp in the x-axis labels shows additional zeroes at the end as shown below
The code I use is
import matplotlib.pyplot as plt
import seaborn as sns
fig, ax1 = plt.subplots()
# Plot the barplot
sns.barplot(x='Date', y=y_value, hue='Sentiment', data=mergedData1, ax=ax1)
# Assign y axis label for bar plot
ax1.set_ylabel('No of Feeds')
# Position the legen on the right side outside the box
plt.legend(loc=2, bbox_to_anchor=(1.1, 1), ncol=1)
# Create a dual axis
ax2 = ax1.twinx()
# Plot the ponitplot
sns.pointplot(x='Date', y='meanTRP', data=mergedData1, ax=ax2, color='r')
# Assign y axis label for point plot
ax2.set_ylabel('TRP')
# Hide the grid for secondary axis
ax2.grid(False)
# Give a chart title
plt.title(source+' Social Media Feeds & TRP for the show '+show)
# Automatically align the x axis labels
fig.autofmt_xdate()
fig.tight_layout()
Not sure what is going wrong. Please help me with this. Thanks
Easiest solution is to split the text at the letter "T" as the rest is probably not needed.
ax.set_xticklabels([t.get_text().split("T")[0] for t in ax.get_xticklabels()])
You can still have more control over date format with this code:
ax.set_xticklabels([pd.to_datetime(tm).strftime('%d-%m-%Y') for tm in ax.get_xticklabels()])
Looking to add in vertical space between plotted graphs to allow a X-Axis label to show:
Each graph needs to have space to show the day, currently the last 2 graphs are the only one's that show simply because the graphs are overlapping it.
Also curious if I could actually remove the notch labels for the X-Axis for the graphs above the one's marked Thursday/Friday, i.e. the bottom X-axis is the only one that shows. Same for the Y-Axis, but only the graphs on the left having the scale shown.
*Unfortunately I can't post an image to show this since I don't have enough rep.
Code snippet:
import mathlib.pyplot as pyplot
fig = pyplot.figure()
ax1 = fig.add_subplot(4,2,1)
ax1.set_yscale('log')
ax2 = fig.add_subplot(4,2,2, sharex=ax1, sharey=ax1)
ax3 = fig.add_subplot(4,2,3, sharex=ax2, sharey=ax2)
ax4 = fig.add_subplot(4,2,4, sharex=ax3, sharey=ax3)
ax5 = fig.add_subplot(4,2,5, sharex=ax4, sharey=ax4)
ax6 = fig.add_subplot(4,2,6, sharex=ax5, sharey=ax5)
ax7 = fig.add_subplot(4,2,7, sharex=ax6, sharey=ax6)
ax1.plot(no_dict["Saturday"],'k.-',label='Saturday')
ax1.set_xlabel('Saturday')
ax1.axis([0,24,0,10000])
pyplot.suptitle('Title')
pyplot.xlabel('Hour in 24 Hour Format')
ax2.plot(no_dict["Sunday"],'b.-',label='Sunday')
ax2.set_xlabel('Sunday')
...
Use subplots_adjust. In your case this looks good:
fig.subplots_adjust(hspace=.5)
to remove the tick labels do this:
ax1.set_xticklabels([])
Similar for the yticklabels. However, you cannot share the x-axis with the plots that do have tick labels.
To change the spacing around a certain subplot, instead of all of them, you can adjust the position of the axes of that subplot using:
bbox=plt.gca().get_position()
offset=-.03
plt.gca().set_position([bbox.x0, bbox.y0 + offset, bbox.x1-bbox.x0, bbox.y1 - bbox.y0])
If offset < 0, the subplot is moved down. If offset > 0, the subplot is moved up.
Note that the subplot will disappear if offset is so big that the new position of the subplot overlaps with another subplot.