有没有办法通过轴的分数在图中定位文字?无论x和y的区别如何,我希望所有图表的文本位于相同的位置。这个功能在ax.annotate()中,但我需要加入额外的'xy'参数,这使我的代码更难阅读。
// #include <iostream>
// #include <iomanip>
// using namespace std;
// int main()
// {
// int number; //number of beverages
// float cost; //cost of beverage
// int beverage; //beverage type
// bool validBeverage;
// cout << fixed << showpoint << setprecision(2);
// do
// {
// cout << endl << endl;
// cout << "Hot Beverage Menu" << endl << endl;
// cout << "1: Coffee $1.00" << endl;
// cout << "2: Tea $ .75" << endl;
// cout << "3: Hot Chocolate $1.25" << endl;
// cout << "4: Cappuccino $2.50" << endl << endl << endl;
// cout << "Enter the beverage 1,2,3, or 4 you desire" << endl;
// cout << "Enter 5 to exit the program" << endl << endl;
// cin >> beverage;
// switch (beverage)
// {
// case 1: validBeverage = true;
// break;
// case 2: validBeverage = true;
// break;
// case 3: validBeverage = true;
// break;
// case 4: validBeverage = true;
// break;
// default:validBeverage = false;
// }
// if (validBeverage == true)
// {
// cout << "How many cups would you like?" << endl;
// cin >> number;
// }
// switch (beverage)
// {
// case 1: cost = number * 1.0;
// cout << "The total cost is $ " << cost << endl;
// break;
// case 2: cost = number * .75;
// cout << "The total cost is $ " << cost << endl;
// break;
// case 3: cost = number * 1.25;
// cout << "The total cost is $ " << cost << endl;
// break;
// case 4: cost = number * 2.50;
// cout << "The total cost is $ " << cost << endl;
// break;
// case 5: cout << "Thank you. Please come again." << endl;
// break;
// default:cout << "You entered an invalid selection.\n";
// cout << "Try again please" << endl;
// }
// } while (beverage != 5);
// return 0;
// }
答案 0 :(得分:2)
您可以使用transform
关键字:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(np.arange(10),12*np.arange(10))
ax.text(0.4, 0.7, 'Correct Position', transform=ax.transAxes)
plt.show()